Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
holmes
Commits
beadaebf
Commit
beadaebf
authored
Jun 18, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
数据库同步脚本编写
parent
addf9934
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
833 additions
and
0 deletions
+833
-0
pom.xml
pom.xml
+8
-0
K8sConfig.java
src/main/java/cn/qg/holmes/config/K8sConfig.java
+16
-0
DbSyncController.java
...java/cn/qg/holmes/controller/effect/DbSyncController.java
+44
-0
DatabaseSyncService.java
...java/cn/qg/holmes/service/effect/DatabaseSyncService.java
+12
-0
DatabaseSyncServiceImpl.java
...g/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
+419
-0
SqlExecutionUtils.java
src/main/java/cn/qg/holmes/utils/SqlExecutionUtils.java
+316
-0
KubeConfig.yml
src/main/resources/KubeConfig.yml
+18
-0
No files found.
pom.xml
View file @
beadaebf
...
@@ -148,6 +148,14 @@
...
@@ -148,6 +148,14 @@
<version>
4.1.1
</version>
<version>
4.1.1
</version>
</dependency>
</dependency>
<!-- https://mvnrepository.com/artifact/io.fabric8/kubernetes-client -->
<dependency>
<groupId>
io.fabric8
</groupId>
<artifactId>
kubernetes-client
</artifactId>
<version>
5.4.1
</version>
</dependency>
</dependencies>
</dependencies>
<build>
<build>
...
...
src/main/java/cn/qg/holmes/config/K8sConfig.java
0 → 100644
View file @
beadaebf
package
cn
.
qg
.
holmes
.
config
;
import
io.fabric8.kubernetes.client.Config
;
import
io.fabric8.kubernetes.client.DefaultKubernetesClient
;
import
io.fabric8.kubernetes.client.KubernetesClient
;
import
java.io.IOException
;
public
class
K8sConfig
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
Config
config
=
Config
.
fromKubeconfig
(
"classpath/KubeConfig.yml"
);
KubernetesClient
kubernetesClient
=
new
DefaultKubernetesClient
(
config
);
System
.
out
.
println
(
kubernetesClient
.
pods
().
inNamespace
(
"fe"
).
list
());
}
}
src/main/java/cn/qg/holmes/controller/effect/DbSyncController.java
0 → 100644
View file @
beadaebf
package
cn
.
qg
.
holmes
.
controller
.
effect
;
import
cn.qg.holmes.common.JsonResult
;
import
cn.qg.holmes.service.effect.DatabaseSyncService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.*
;
@CrossOrigin
@RestController
@RequestMapping
(
"/db"
)
public
class
DbSyncController
{
@Autowired
DatabaseSyncService
databaseSyncService
;
@Value
(
"${dbsync.mysql.ip}"
)
private
String
ip
;
@Value
(
"${dbsync.mysql.port}"
)
private
String
port
;
@Value
(
"${dbsync.mysql.username}"
)
private
String
username
;
@Value
(
"${dbsync.mysql.password}"
)
private
String
password
;
@GetMapping
(
"/sync/one"
)
public
JsonResult
syncSingleDatabase
(
@RequestParam
String
namespace
,
@RequestParam
String
dbName
)
{
databaseSyncService
.
getDbInfoFromSource
(
ip
,
port
,
username
,
password
,
dbName
);
databaseSyncService
.
syncDbToDest
(
"172.17.5.24"
,
"31024"
,
"qa"
,
"qatest"
,
dbName
);
return
JsonResult
.
buildSuccessResult
(
null
);
}
/**
* 获取数据库列表
* @return
*/
@GetMapping
(
"/sync/list"
)
public
JsonResult
getDatabaseList
()
{
return
JsonResult
.
buildSuccessResult
(
databaseSyncService
.
getDatabaseList
(
ip
,
port
,
username
,
password
));
}
}
src/main/java/cn/qg/holmes/service/effect/DatabaseSyncService.java
0 → 100644
View file @
beadaebf
package
cn
.
qg
.
holmes
.
service
.
effect
;
import
java.util.List
;
public
interface
DatabaseSyncService
{
boolean
getDbInfoFromSource
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
);
boolean
syncDbToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
);
List
<
Object
>
getDatabaseList
(
String
ip
,
String
port
,
String
username
,
String
password
);
}
\ No newline at end of file
src/main/java/cn/qg/holmes/service/effect/impl/DatabaseSyncServiceImpl.java
0 → 100644
View file @
beadaebf
package
cn
.
qg
.
holmes
.
service
.
effect
.
impl
;
import
cn.qg.holmes.service.effect.DatabaseSyncService
;
import
cn.qg.holmes.utils.RedisUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.math.BigDecimal
;
import
java.sql.*
;
import
java.util.ArrayList
;
import
java.util.List
;
@Slf4j
@Service
public
class
DatabaseSyncServiceImpl
implements
DatabaseSyncService
{
@Autowired
RedisUtils
redisUtils
;
private
final
String
dbSyncPrefix
=
"dbsync:"
;
/**
* 从中间库获取数据库相关信息
* @param ip mysql ip
* @param port mysql port
* @param username mysql用户名
* @param password mysql密码
* @param dbName 数据库名
* @return
*/
@Override
public
boolean
getDbInfoFromSource
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
String
url
=
"jdbc:mysql://"
+
ip
+
":"
+
port
+
"/"
+
dbName
+
"?zeroDateTimeBehavior=convertToNull"
;
String
createTableKey
=
dbSyncPrefix
+
dbName
+
":create"
;
String
insertTableKey
=
dbSyncPrefix
+
dbName
+
":insert"
;
String
dbRedisKey
=
dbSyncPrefix
+
dbName
;
Connection
connection
=
null
;
PreparedStatement
preparedStatement
=
null
;
String
dbRedisValue
=
""
;
String
insertRedisValue
=
""
;
List
<
Object
>
createTableRedisValue
=
new
ArrayList
<>();
List
<
String
>
columnNameList
=
new
ArrayList
<>();
List
<
String
>
columnTypeList
=
new
ArrayList
<>();
List
<
String
>
commentList
=
new
ArrayList
<>();
try
{
Class
.
forName
(
driver
);
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
if
(!
redisUtils
.
hasKey
(
dbRedisKey
))
{
String
dbSql
=
String
.
format
(
"SHOW CREATE DATABASE %s"
,
dbName
);
preparedStatement
=
connection
.
prepareStatement
(
dbSql
);
ResultSet
dbResultSet
=
preparedStatement
.
executeQuery
();
while
(
dbResultSet
.
next
())
{
dbRedisValue
=
dbResultSet
.
getString
(
2
);
}
// 设置建库语句的redis值
redisUtils
.
set
(
dbRedisKey
,
dbRedisValue
,
43200
);
}
if
(!
redisUtils
.
hasKey
(
createTableKey
))
{
DatabaseMetaData
databaseMetaData
=
connection
.
getMetaData
();
// 获取所有表名
ResultSet
tableResultSet
=
databaseMetaData
.
getTables
(
null
,
null
,
null
,
new
String
[]{
"TABLE"
});
while
(
tableResultSet
.
next
())
{
String
tableName
=
tableResultSet
.
getString
(
"TABLE_NAME"
);
// 获取所有建表语句
String
sql
=
String
.
format
(
"SHOW CREATE TABLE %s"
,
tableName
);
preparedStatement
=
connection
.
prepareStatement
(
sql
);
ResultSet
pResultSet
=
preparedStatement
.
executeQuery
();
while
(
pResultSet
.
next
())
{
log
.
info
(
"获取建表语句:{}"
,
pResultSet
.
getString
(
2
));
createTableRedisValue
.
add
(
pResultSet
.
getString
(
2
));
}
if
(!
redisUtils
.
hasKey
(
insertTableKey
))
{
ResultSet
columnResultSet
=
databaseMetaData
.
getColumns
(
null
,
"%"
,
tableName
,
"%"
);
while
(
columnResultSet
.
next
())
{
// 字段名称
String
columnName
=
columnResultSet
.
getString
(
"COLUMN_NAME"
);
// 数据类型
String
columnType
=
columnResultSet
.
getString
(
"TYPE_NAME"
);
// 备注
String
remarks
=
columnResultSet
.
getString
(
"REMARKS"
);
columnNameList
.
add
(
columnName
);
columnTypeList
.
add
(
columnType
);
commentList
.
add
(
remarks
);
}
String
columnArrayStr
=
null
;
for
(
String
column
:
columnNameList
)
{
if
(
null
==
columnArrayStr
)
{
columnArrayStr
=
"`"
+
column
+
"`"
;
}
else
{
columnArrayStr
=
columnArrayStr
+
","
+
"`"
+
column
+
"`"
;
}
}
String
selectSQL
=
String
.
format
(
"select %s from %s"
,
columnArrayStr
,
tableName
);
preparedStatement
=
connection
.
prepareStatement
(
selectSQL
);
ResultSet
selectResultSet
=
preparedStatement
.
executeQuery
();
while
(
selectResultSet
.
next
())
{
String
rowValues
=
getRowValues
(
selectResultSet
,
columnNameList
.
size
(),
columnTypeList
);
String
insertSql
=
String
.
format
(
"insert into %s (%s) values(%s);"
,
tableName
,
columnArrayStr
,
rowValues
);
log
.
info
(
insertSql
);
insertSql
=
insertSql
.
replaceAll
(
"\n"
,
"<br/>"
);
insertSql
=
insertSql
+
"\n"
;
insertRedisValue
+=
insertSql
;
}
columnNameList
.
clear
();
columnTypeList
.
clear
();
commentList
.
clear
();
}
}
redisUtils
.
lSet
(
createTableKey
,
createTableRedisValue
,
43200
);
redisUtils
.
set
(
insertTableKey
,
insertRedisValue
,
43200
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
if
(
connection
!=
null
)
{
connection
.
close
();
}
if
(
preparedStatement
!=
null
)
{
preparedStatement
.
close
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
false
;
}
@Override
public
boolean
syncDbToDest
(
String
ip
,
String
port
,
String
username
,
String
password
,
String
dbName
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
String
url
=
"jdbc:mysql://"
+
ip
+
":"
+
port
;
String
createTableKey
=
dbSyncPrefix
+
dbName
+
":create"
;
String
insertTableKey
=
dbSyncPrefix
+
dbName
+
":insert"
;
String
dbRedisKey
=
dbSyncPrefix
+
dbName
;
Connection
connection
=
null
;
Connection
newConnection
=
null
;
PreparedStatement
preparedStatement
=
null
;
try
{
Class
.
forName
(
driver
);
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
// 删除原有数据库
String
dropDbSql
=
String
.
format
(
"DROP DATABASE IF EXISTS %s"
,
dbName
);
log
.
info
(
"删除原有数据库:{}"
,
dropDbSql
);
preparedStatement
=
connection
.
prepareStatement
(
dropDbSql
);
preparedStatement
.
execute
();
// 重新创建数据库
String
createDbSql
=
redisUtils
.
get
(
dbRedisKey
).
toString
();
log
.
info
(
"创建数据库:{}"
,
createDbSql
);
preparedStatement
=
connection
.
prepareStatement
(
createDbSql
);
preparedStatement
.
execute
();
connection
.
close
();
// 重新设置连接,并指定数据库
newConnection
=
DriverManager
.
getConnection
(
url
+
"/"
+
dbName
,
username
,
password
);
newConnection
.
setAutoCommit
(
true
);
// 从redis中获取要同步的表结构
List
<
Object
>
createTableRedisValue
=
redisUtils
.
lGet
(
createTableKey
,
0
,
redisUtils
.
lGetListSize
(
createTableKey
));
for
(
Object
sql:
createTableRedisValue
)
{
preparedStatement
=
newConnection
.
prepareStatement
(
sql
.
toString
());
preparedStatement
.
execute
();
}
// 从redis中同步表数据
String
insertTableRedisValue
=
redisUtils
.
get
(
insertTableKey
).
toString
();
preparedStatement
=
newConnection
.
prepareStatement
(
insertTableRedisValue
);
preparedStatement
.
execute
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
if
(
connection
!=
null
)
{
connection
.
close
();
}
if
(
newConnection
!=
null
)
{
newConnection
.
close
();
}
if
(
preparedStatement
!=
null
)
{
preparedStatement
.
close
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
return
false
;
}
/**
* 获取数据库列表
* @param ip 同步库ip
* @param port 同步库端口号
* @param username 同步库用户名
* @param password 同步库密码
* @return
*/
@Override
public
List
<
Object
>
getDatabaseList
(
String
ip
,
String
port
,
String
username
,
String
password
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
String
url
=
"jdbc:mysql://"
+
ip
+
":"
+
port
;
List
<
Object
>
databaseList
=
new
ArrayList
<>();
String
dbListRedisKey
=
dbSyncPrefix
+
":db:list"
;
if
(
redisUtils
.
hasKey
(
dbListRedisKey
))
{
databaseList
=
redisUtils
.
lGet
(
dbListRedisKey
,
0
,
redisUtils
.
lGetListSize
(
dbListRedisKey
));
log
.
info
(
"从redis获取到的数据库列表为:{}"
,
databaseList
);
}
else
{
Connection
connection
=
null
;
try
{
Class
.
forName
(
driver
);
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
connection
.
setAutoCommit
(
true
);
String
showDatabasesSql
=
"SHOW DATABASES"
;
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
showDatabasesSql
);
ResultSet
dbListResultSet
=
preparedStatement
.
executeQuery
();
while
(
dbListResultSet
.
next
())
{
databaseList
.
add
(
dbListResultSet
.
getString
(
1
));
}
preparedStatement
.
close
();
// 设置数据库列表缓存,缓存6个小时
log
.
info
(
"从同步库获取到的数据库列表为:{}"
,
databaseList
);
redisUtils
.
lSet
(
dbListRedisKey
,
databaseList
,
21600
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
Exception
e
)
{
connection
=
null
;
}
}
}
return
databaseList
;
}
/**
* 获取表数据一行的所有值
* @param rs
* @param size
* @param columnTypeList
* @return
*/
private
String
getRowValues
(
ResultSet
rs
,
int
size
,
List
<
String
>
columnTypeList
)
{
try
{
String
rowValues
=
null
;
for
(
int
i
=
1
;
i
<=
size
;
i
++)
{
String
columnValue
=
null
;
// 获取字段值
columnValue
=
getValue
(
rs
,
i
,
columnTypeList
.
get
(
i
-
1
));
// 如果是空值不添加单引号
if
(
null
!=
columnValue
)
{
columnValue
=
"'"
+
columnValue
+
"'"
;
}
// 拼接字段值
if
(
null
==
rowValues
)
{
rowValues
=
columnValue
;
}
else
{
rowValues
=
rowValues
+
","
+
columnValue
;
}
}
return
rowValues
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
"获取表数据一行的所有值异常"
);
return
null
;
}
}
/**
* 根据类型获取字段值
* @return
* @author 2018年9月6日 上午11:16:00
*/
private
String
getValue
(
ResultSet
resultSet
,
Integer
index
,
String
columnType
)
{
try
{
if
(
"int"
.
equals
(
columnType
)
||
"INT"
.
equals
(
columnType
))
{
// 整数
Object
intValue
=
resultSet
.
getObject
(
index
);
if
(
null
==
intValue
)
{
return
null
;
}
return
intValue
+
""
;
}
else
if
(
"bigint"
.
equals
(
columnType
)
||
"BIGINT"
.
equals
(
columnType
))
{
// 长整形
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"smallint"
.
equals
(
columnType
)
||
"SMALLINT"
.
equals
(
columnType
))
{
// 整数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"tinyint"
.
equals
(
columnType
)
||
"TINYINT"
.
equals
(
columnType
))
{
// 整数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"mediumint"
.
equals
(
columnType
)
||
"MEDIUMINT"
.
equals
(
columnType
))
{
// 长整形
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"integer"
.
equals
(
columnType
)
||
"INTEGER"
.
equals
(
columnType
))
{
// 整数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"float"
.
equals
(
columnType
)
||
"FLOAT"
.
equals
(
columnType
))
{
// 浮点数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"double"
.
equals
(
columnType
)
||
"DOUBLE"
.
equals
(
columnType
))
{
// 浮点数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"decimal"
.
equals
(
columnType
)
||
"DECIMAL"
.
equals
(
columnType
))
{
// 浮点数-金额类型
BigDecimal
value
=
resultSet
.
getBigDecimal
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
.
toString
();
}
else
if
(
"char"
.
equals
(
columnType
)
||
"CHAR"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"varchar"
.
equals
(
columnType
)
||
"VARCHAR"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"tinytext"
.
equals
(
columnType
)
||
"TINYTEXT"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"text"
.
equals
(
columnType
)
||
"TEXT"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"mediumtext"
.
equals
(
columnType
)
||
"MEDIUMTEXT"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"longtext"
.
equals
(
columnType
)
||
"LONGTEXT"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"year"
.
equals
(
columnType
)
||
"YEAR"
.
equals
(
columnType
))
{
// 时间类型:范围 1901/2155 格式 YYYY
String
year
=
resultSet
.
getString
(
index
);
if
(
null
==
year
)
{
return
null
;
}
// 只需要年的字符即可,
return
year
.
substring
(
0
,
4
);
}
else
if
(
"date"
.
equals
(
columnType
)
||
"DATE"
.
equals
(
columnType
))
{
// 时间类型:范围 '1000-01-01'--'9999-12-31' 格式 YYYY-MM-DD
return
resultSet
.
getString
(
index
);
}
else
if
(
"time"
.
equals
(
columnType
)
||
"TIME"
.
equals
(
columnType
))
{
// 时间类型:范围 '-838:59:59'到'838:59:59' 格式 HH:MM:SS
return
resultSet
.
getString
(
index
);
}
else
if
(
"datetime"
.
equals
(
columnType
)
||
"DATETIME"
.
equals
(
columnType
))
{
// 时间类型:范围 '1000-01-01 00:00:00'--'9999-12-31 23:59:59' 格式 YYYY-MM-DD HH:MM:SS
return
resultSet
.
getString
(
index
);
}
else
if
(
"timestamp"
.
equals
(
columnType
)
||
"TIMESTAMP"
.
equals
(
columnType
))
{
// 时间类型:范围 1970-01-01 00:00:00/2037 年某时 格式 YYYYMMDD HHMMSS 混合日期和时间值,时间戳
return
resultSet
.
getString
(
index
);
}
else
{
return
null
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
err
.
println
(
"获取数据库类型值异常"
);
return
null
;
}
}
}
src/main/java/cn/qg/holmes/utils/SqlExecutionUtils.java
0 → 100644
View file @
beadaebf
package
cn
.
qg
.
holmes
.
utils
;
import
org.apache.ibatis.jdbc.ScriptRunner
;
import
java.math.BigDecimal
;
import
java.sql.*
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
SqlExecutionUtils
{
public
static
void
executeSqlFileByMysql
(
String
host
,
String
port
,
String
username
,
String
password
,
String
dbName
)
{
String
driver
=
"com.mysql.jdbc.Driver"
;
String
url
=
"jdbc:mysql://"
+
host
+
":"
+
port
+
"/"
+
dbName
+
"?zeroDateTimeBehavior=convertToNull"
;
Connection
connection
=
null
;
List
<
String
>
columnNameList
=
new
ArrayList
<>();
List
<
String
>
columnTypeList
=
new
ArrayList
<>();
List
<
String
>
commentList
=
new
ArrayList
<>();
String
dbBackupInfo
=
""
;
try
{
Class
.
forName
(
driver
);
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
connection
.
setAutoCommit
(
false
);
// runner.runScript(new InputStreamReader(new FileInputStream(sqlFilePath), "utf-8"));
// 获取创建数据库的语句
DatabaseMetaData
databaseMetaData
=
connection
.
getMetaData
();
// String dbSql = String.format("SHOW CREATE DATABASE %s", dbName);
// PreparedStatement preparedStatement1 = connection.prepareStatement(dbSql);
// ResultSet dResultSet = preparedStatement1.executeQuery();
// while (dResultSet.next()) {
// System.out.println(dResultSet.getString(2));
// }
// preparedStatement1.close();
// String createDbSql = "CREATE DATABASE `kdsp` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */";
// PreparedStatement preparedStatement = connection.prepareStatement(createDbSql);
// preparedStatement.execute();
// preparedStatement.close();
// List<String> databaseList = new ArrayList<>();
// String showDatabasesSql = "SHOW DATABASES";
// PreparedStatement preparedStatement = connection.prepareStatement(showDatabasesSql);
// ResultSet dbListResultSet = preparedStatement.executeQuery();
// while (dbListResultSet.next()) {
// databaseList.add(dbListResultSet.getString(1));
// System.out.println(dbListResultSet.getString(1));
// }
// 获取所有表名
ResultSet
tableResultSet
=
databaseMetaData
.
getTables
(
null
,
null
,
null
,
new
String
[]{
"TABLE"
});
while
(
tableResultSet
.
next
())
{
String
tableName
=
tableResultSet
.
getString
(
"TABLE_NAME"
);
System
.
out
.
println
(
"表名:"
+
tableName
);
// 获取所有建表语句
String
createTableSql
=
""
;
String
sql
=
String
.
format
(
"SHOW CREATE TABLE %s"
,
tableName
);
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
sql
);
ResultSet
pResultSet
=
preparedStatement
.
executeQuery
();
while
(
pResultSet
.
next
())
{
// System.out.println(pResultSet.getString(2));
createTableSql
=
pResultSet
.
getString
(
2
);
}
preparedStatement
.
close
();
// String querySql = String.format("SELECT * FROM %s", tableName);
// PreparedStatement preparedStatement1 = connection.prepareStatement(querySql);
// ResultSetMetaData resultSetMetaData = preparedStatement1.getMetaData();
// int size = resultSetMetaData.getColumnCount();
// for (int i = 0; i < size; i++) {
// columnNameList.add(resultSetMetaData.getColumnName(i + 1));
// columnTypeList.add(resultSetMetaData.getColumnTypeName(i + 1));
// }
// 获取行
ResultSet
columnResultSet
=
databaseMetaData
.
getColumns
(
null
,
"%"
,
tableName
,
"%"
);
while
(
columnResultSet
.
next
())
{
String
columnName
=
columnResultSet
.
getString
(
"COLUMN_NAME"
);
// 数据类型
String
columnType
=
columnResultSet
.
getString
(
"TYPE_NAME"
);
// 字段长度
int
datasize
=
columnResultSet
.
getInt
(
"COLUMN_SIZE"
);
// 小数部分位数
int
digits
=
columnResultSet
.
getInt
(
"DECIMAL_DIGITS"
);
// 是否可为空 1代表可空 0代表不可为空
int
nullable
=
columnResultSet
.
getInt
(
"NULLABLE"
);
// 描述
String
remarks
=
columnResultSet
.
getString
(
"REMARKS"
);
columnNameList
.
add
(
columnName
);
columnTypeList
.
add
(
columnType
);
commentList
.
add
(
remarks
);
// System.out.println(columnName + " " + columnType + " " + datasize + " " + digits + " " + nullable + " " + remarks);
}
System
.
out
.
println
(
columnNameList
);
System
.
out
.
println
(
columnTypeList
);
System
.
out
.
println
(
commentList
);
String
columnArrayStr
=
null
;
for
(
String
column
:
columnNameList
)
{
if
(
null
==
columnArrayStr
)
{
columnArrayStr
=
"`"
+
column
+
"`"
;
}
else
{
columnArrayStr
=
columnArrayStr
+
","
+
"`"
+
column
+
"`"
;
}
}
String
selectSQL
=
String
.
format
(
"select %s from %s"
,
columnArrayStr
,
tableName
);
System
.
out
.
println
(
selectSQL
);
PreparedStatement
selectPstmt
=
connection
.
prepareStatement
(
selectSQL
);
ResultSet
selResultSet
=
selectPstmt
.
executeQuery
();
while
(
selResultSet
.
next
())
{
String
rowValues
=
getRowValues
(
selResultSet
,
columnNameList
.
size
(),
columnTypeList
);
String
insertSql
=
String
.
format
(
"insert into %s (%s) values(%s);"
,
tableName
,
columnArrayStr
,
rowValues
);
// System.out.println(insertSql);
insertSql
=
insertSql
.
replaceAll
(
"\n"
,
"<br/>"
);
insertSql
=
insertSql
+
"\n"
;
dbBackupInfo
+=
insertSql
;
}
columnNameList
.
clear
();
columnTypeList
.
clear
();
commentList
.
clear
();
selectPstmt
.
close
();
}
System
.
out
.
println
(
dbBackupInfo
);
connection
.
commit
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
close
(
connection
);
}
}
private
static
void
close
(
Connection
connection
)
{
try
{
if
(
connection
!=
null
)
{
connection
.
close
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
static
String
getRowValues
(
ResultSet
rs
,
int
size
,
List
<
String
>
columnTypeList
)
{
try
{
String
rowValues
=
null
;
for
(
int
i
=
1
;
i
<=
size
;
i
++)
{
String
columnValue
=
null
;
// 获取字段值
columnValue
=
getValue
(
rs
,
i
,
columnTypeList
.
get
(
i
-
1
));
// 如果是空值不添加单引号
if
(
null
!=
columnValue
)
{
columnValue
=
"'"
+
columnValue
+
"'"
;
}
// 拼接字段值
if
(
null
==
rowValues
)
{
rowValues
=
columnValue
;
}
else
{
rowValues
=
rowValues
+
","
+
columnValue
;
}
}
return
rowValues
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
out
.
println
(
"获取表数据一行的所有值异常"
);
return
null
;
}
}
/**
*
* <p>
* 根据类型获取字段值
* </p>
*
* @return
* @author 2018年9月6日 上午11:16:00
*/
private
static
String
getValue
(
ResultSet
resultSet
,
Integer
index
,
String
columnType
)
{
try
{
if
(
"int"
.
equals
(
columnType
)
||
"INT"
.
equals
(
columnType
))
{
// 整数
Object
intValue
=
resultSet
.
getObject
(
index
);
if
(
null
==
intValue
)
{
return
null
;
}
return
intValue
+
""
;
}
else
if
(
"bigint"
.
equals
(
columnType
)
||
"BIGINT"
.
equals
(
columnType
))
{
// 长整形
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"smallint"
.
equals
(
columnType
)
||
"SMALLINT"
.
equals
(
columnType
))
{
// 整数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"tinyint"
.
equals
(
columnType
)
||
"TINYINT"
.
equals
(
columnType
))
{
// 整数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"mediumint"
.
equals
(
columnType
)
||
"MEDIUMINT"
.
equals
(
columnType
))
{
// 长整形
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"integer"
.
equals
(
columnType
)
||
"INTEGER"
.
equals
(
columnType
))
{
// 整数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"float"
.
equals
(
columnType
)
||
"FLOAT"
.
equals
(
columnType
))
{
// 浮点数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"double"
.
equals
(
columnType
)
||
"DOUBLE"
.
equals
(
columnType
))
{
// 浮点数
Object
value
=
resultSet
.
getObject
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
+
""
;
}
else
if
(
"decimal"
.
equals
(
columnType
)
||
"DECIMAL"
.
equals
(
columnType
))
{
// 浮点数-金额类型
BigDecimal
value
=
resultSet
.
getBigDecimal
(
index
);
if
(
null
==
value
)
{
return
null
;
}
return
value
.
toString
();
}
else
if
(
"char"
.
equals
(
columnType
)
||
"CHAR"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"varchar"
.
equals
(
columnType
)
||
"VARCHAR"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"tinytext"
.
equals
(
columnType
)
||
"TINYTEXT"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"text"
.
equals
(
columnType
)
||
"TEXT"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"mediumtext"
.
equals
(
columnType
)
||
"MEDIUMTEXT"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"longtext"
.
equals
(
columnType
)
||
"LONGTEXT"
.
equals
(
columnType
))
{
// 字符串类型
String
value
=
resultSet
.
getString
(
index
);
return
value
;
}
else
if
(
"year"
.
equals
(
columnType
)
||
"YEAR"
.
equals
(
columnType
))
{
// 时间类型:范围 1901/2155 格式 YYYY
String
year
=
resultSet
.
getString
(
index
);
if
(
null
==
year
)
{
return
null
;
}
// 只需要年的字符即可,
return
year
.
substring
(
0
,
4
);
}
else
if
(
"date"
.
equals
(
columnType
)
||
"DATE"
.
equals
(
columnType
))
{
// 时间类型:范围 '1000-01-01'--'9999-12-31' 格式 YYYY-MM-DD
return
resultSet
.
getString
(
index
);
}
else
if
(
"time"
.
equals
(
columnType
)
||
"TIME"
.
equals
(
columnType
))
{
// 时间类型:范围 '-838:59:59'到'838:59:59' 格式 HH:MM:SS
return
resultSet
.
getString
(
index
);
}
else
if
(
"datetime"
.
equals
(
columnType
)
||
"DATETIME"
.
equals
(
columnType
))
{
// 时间类型:范围 '1000-01-01 00:00:00'--'9999-12-31 23:59:59' 格式 YYYY-MM-DD HH:MM:SS
return
resultSet
.
getString
(
index
);
}
else
if
(
"timestamp"
.
equals
(
columnType
)
||
"TIMESTAMP"
.
equals
(
columnType
))
{
// 时间类型:范围 1970-01-01 00:00:00/2037 年某时 格式 YYYYMMDD HHMMSS 混合日期和时间值,时间戳
return
resultSet
.
getString
(
index
);
}
else
{
return
null
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
System
.
err
.
println
(
"获取数据库类型值异常"
);
return
null
;
}
}
public
static
void
main
(
String
[]
args
)
{
executeSqlFileByMysql
(
"172.30.5.35"
,
"3308"
,
"xyqb_query"
,
"Uiwb@o^fR&nHOa5t"
,
"vcc_talos"
);
// executeSqlFileByMysql("172.17.5.24", "31024", "qa", "qatest", "kdsp");
}
}
src/main/resources/KubeConfig.yml
0 → 100644
View file @
beadaebf
apiVersion
:
v1
clusters
:
-
cluster
:
certificate-authority-data
:
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURaRENDQWt5Z0F3SUJBZ0lJQWlFbzhnb1ZhZVl3RFFZSktvWklodmNOQVFFTEJRQXdVREVMTUFrR0ExVUUKQmhNQ1EwNHhLakFSQmdOVkJBb1RDblJsYm1ObGJuUjVkVzR3RlFZRFZRUUtFdzV6ZVhOMFpXMDZiV0Z6ZEdWeQpjekVWTUJNR0ExVUVBeE1NWTJ4ekxXRmpabmcwY0hacU1CNFhEVEU1TURFd016QXpNRFExTWxvWERUTTVNREV3Ck16QXpNRFExTWxvd1VERUxNQWtHQTFVRUJoTUNRMDR4S2pBUkJnTlZCQW9UQ25SbGJtTmxiblI1ZFc0d0ZRWUQKVlFRS0V3NXplWE4wWlcwNmJXRnpkR1Z5Y3pFVk1CTUdBMVVFQXhNTVkyeHpMV0ZqWm5nMGNIWnFNSUlCSWpBTgpCZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUE1UnVyS0hSMG5GMlAyNndlU01haittbXltdytsCkEvODFyNUl3QVFZaXoxSDRRT3IrVFdiZjd0Z2JsWXNOemJpSk5qTk56VkRiQ2RidkhZaHhLNlVjemJDOGJocXEKakE4Y29tcCtkbENlNm13VEJKNzRwM08rSlduNFJOY0QreHJ4dXJ6aE9GRFZra1RrRWNqMDZXT25oMk9Dc0VaQwpCeVczWlBYQ2p1ZG8wNUprTUhXaEUvcERmSWEyYWIxNmRZeGZ2SWhzdjhYcWdRMm92T0RvOEg5aVBmWkMvVkl4CmpoUWtPem1SbmRlRXgvWlpiWTlvMW9PbWwxcjl3ZnRXWklzWTJYbTY0SzRrVDg3YWZURi9xaXZ4OEdXOXBwRHkKb3JZVlhnVVFKVkRHMmIyWXdGYldYdlB3SzZZYlNVOUdydUplUnFmdklLaSt3S3RxQzBMZ1pZckxVd0lEQVFBQgpvMEl3UURBT0JnTlZIUThCQWY4RUJBTUNBb1F3SFFZRFZSMGxCQll3RkFZSUt3WUJCUVVIQXdJR0NDc0dBUVVGCkJ3TUJNQThHQTFVZEV3RUIvd1FGTUFNQkFmOHdEUVlKS29aSWh2Y05BUUVMQlFBRGdnRUJBRi9jYzF0RGZUSFEKeDVyRmpyUEt5YW5xOWJWb2YySlorMnhiaHp1b2sxU2ljYlNwdExia0h4VDh6cmlVeHNQM3djbFluSVFtN25XNgo5VEl6S1hJOUgra3ZFbDNQQXZyU1VVRnFOSytLWlBEZFpQdnNvcFZ1dDNtc3ZQeWRnNGRlODgzd0EwSmQ1dWl1CjNKV0xBYm1hekhZT01taEQyRW9yc1VpWTZ3NXVJNy9rMld6bUgzelVGd250WDRNMjFYR3JrZm5IUUVhVHQvaHQKR3JsUWpQVzgwNzFwSm9UaVgxNURzd2VNTS9KZEtMV3FTaHVJdWY3YXNqcFUrSWRtQjU3U0g3NER0eTNEaTU4QwpHVldBQjEzc3BtZGpIbGhDeTlRZGxpQzhkUzVGai80SFRIdDJHYTBDREZLRVk0TnF3SGUvK0hqNmRWOTJrcmJHCmdaNkZRa2FId1ZrPQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
server
:
https://cls-acfx4pvj.ccs.tencent-cloud.com
name
:
local
contexts
:
-
context
:
cluster
:
local
user
:
admin
name
:
cls-acfx4pvj-context-default
current-context
:
cls-acfx4pvj-context-default
kind
:
Config
preferences
:
{}
users
:
-
name
:
admin
user
:
token
:
BIsuBv18csrBHVXClYZ4ln3izUYJt9Xa
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment