fix():重构数据同步方法
parent
b9a19fbdd9
commit
54817d5629
|
@ -6,6 +6,7 @@ import com.muyu.common.security.annotation.EnableMyFeignClients;
|
|||
import com.muyu.common.swagger.annotation.EnableCustomSwagger2;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
/**
|
||||
* 系统模块
|
||||
|
@ -16,6 +17,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||
@EnableCustomSwagger2
|
||||
@EnableMyFeignClients
|
||||
@SpringBootApplication
|
||||
|
||||
public class MuYuDataSourceApplication {
|
||||
public static void main (String[] args) {
|
||||
SpringApplication.run(MuYuDataSourceApplication.class, args);
|
||||
|
|
|
@ -1,34 +1,40 @@
|
|||
package com.muyu.data.source.service.impl;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.common.core.utils.ObjUtils;
|
||||
import com.muyu.data.source.domain.AssetDataSource;
|
||||
import com.muyu.data.source.domain.Children;
|
||||
import com.muyu.data.source.domain.DataSource;
|
||||
import com.muyu.data.source.domain.DatabaseType;
|
||||
import com.muyu.data.source.domain.TableData;
|
||||
import com.muyu.data.source.domain.req.ShowTableReq;
|
||||
import com.muyu.data.source.domain.resp.ColumnResp;
|
||||
import com.muyu.data.source.domain.resp.CountResp;
|
||||
import com.muyu.data.source.domain.resp.Table;
|
||||
import com.muyu.data.source.mapper.DataSourceMapper;
|
||||
import com.muyu.data.source.service.AssetDataSourceService;
|
||||
import com.muyu.data.source.service.ChildrenService;
|
||||
import com.muyu.data.source.service.DataSourceService;
|
||||
import com.muyu.data.source.service.DatabaseTypeService;
|
||||
import com.muyu.data.source.service.TableDataService;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import com.muyu.common.core.utils.ObjUtils;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.muyu.data.source.mapper.DataSourceMapper;
|
||||
import com.muyu.data.source.domain.DataSource;
|
||||
import com.muyu.data.source.service.DataSourceService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
/**
|
||||
|
@ -54,6 +60,7 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
@Autowired
|
||||
private DatabaseTypeService databaseTypeService;
|
||||
|
||||
private static Integer version = 1;
|
||||
/**
|
||||
* 查询数据源列表
|
||||
*
|
||||
|
@ -172,31 +179,74 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
eq(DataSource::getName, assetDataSource.getName());
|
||||
eq(DataSource::getDatabaseName, assetDataSource.getDatabaseName());
|
||||
}});
|
||||
DatabaseType databaseType = databaseTypeService.getOne(new LambdaQueryWrapper<DatabaseType>() {{
|
||||
eq(DatabaseType::getDatabaseName, dataSource.getDataType());
|
||||
}});
|
||||
//数据库类型为mysql
|
||||
if ("mysql".equals(dataSource.getDataType())) {
|
||||
//根据id查询表描述
|
||||
// 根据id查询表描述
|
||||
List<Children> childrenList = childrenService.list(new LambdaQueryWrapper<Children>() {{
|
||||
eq(Children::getAssetId, assetDataSource.getId());
|
||||
}});
|
||||
//获取表描述的集合
|
||||
List<Table> tableList = dataSourceMapper.selectTable(assetDataSource.getDatabaseName());
|
||||
//判断是否存在
|
||||
if (childrenList == null || childrenList.size() == 0) {
|
||||
tableList.forEach(table -> {
|
||||
Children children = Children.builder()
|
||||
.name(table.getTableName())
|
||||
.annotation(table.getTableComment())
|
||||
.dataTotal(table.getTableRows())
|
||||
.type("dataTable")
|
||||
.assetId(assetDataSource.getId())
|
||||
.build();
|
||||
//添加到数据库
|
||||
childrenService.save(children);
|
||||
String jdbcUrl = databaseType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
|
||||
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||
try {
|
||||
Class.forName(databaseType.getDriverManager());
|
||||
|
||||
});
|
||||
Connection conn = DriverManager.getConnection(jdbcUrl, dataSource.getUser(),
|
||||
dataSource.getPassword());
|
||||
DatabaseMetaData metaData = conn.getMetaData();
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet rs = statement.executeQuery(
|
||||
"select * from information_schema.tables where TABLE_SCHEMA = " +
|
||||
"'" + dataSource.getDatabaseName() + "'");
|
||||
ResultSet tableRet = metaData.getTables(conn.getCatalog(), "%", "%", new String[]{"TABLE"});
|
||||
while (rs.next()) {
|
||||
String tableName = rs.getString("TABLE_NAME");
|
||||
String tableComment = rs.getString("TABLE_COMMENT");
|
||||
;
|
||||
|
||||
Children children = Children.builder()
|
||||
.name(tableName)
|
||||
.annotation(tableComment)
|
||||
.dataTotal(Long.valueOf(rs.getRow()))
|
||||
.type("dataTable")
|
||||
.build();
|
||||
children.setAssetId(assetDataSource.getId());
|
||||
childrenService.save(children);
|
||||
}
|
||||
conn.close();
|
||||
rs.close();
|
||||
tableRet.close();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
//获取表描述的集合
|
||||
// List<Table> tableList = dataSourceMapper.selectTable(assetDataSource.getDatabaseName());
|
||||
//判断是否存在
|
||||
// if (childrenList == null || childrenList.size() == 0) {
|
||||
// tableList.forEach(table -> {
|
||||
// Children children = Children.builder()
|
||||
// .name(table.getTableName())
|
||||
// .annotation(table.getTableComment())
|
||||
// .dataTotal(table.getTableRows())
|
||||
// .type("dataTable")
|
||||
// .assetId(assetDataSource.getId())
|
||||
// .build();
|
||||
// //添加到数据库
|
||||
// childrenService.save(children);
|
||||
|
||||
// });
|
||||
|
||||
//返回表描述
|
||||
return childrenList;
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -204,15 +254,7 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
|
||||
@Override
|
||||
public void addTbleDate(ShowTableReq showTableReq) {
|
||||
// 查询数据源对象
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// if (version == 1){
|
||||
DataSource dataSource = this.getOne(new LambdaQueryWrapper<>() {{
|
||||
eq(DataSource::getName, showTableReq.getAssetStructure().getName());
|
||||
eq(DataSource::getDatabaseName, showTableReq.getAssetStructure().getDatabaseName());
|
||||
|
@ -229,36 +271,60 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
});
|
||||
String jdbcUrl = dataType.getUrlPre() + dataSource.getHost() + ":" + dataSource.getPort() + "/"
|
||||
+ dataSource.getDatabaseName() + "?" + dataSource.getConnectionParam();
|
||||
List<ColumnResp> columnRespList = dataSourceMapper.selectColumn(dataSource.getDatabaseName(),
|
||||
showTableReq.getTableName());
|
||||
|
||||
List<TableData> tableDataList = tableDataService.list(new LambdaQueryWrapper<>() {{
|
||||
eq(TableData::getChildrenId, children.getId());
|
||||
}});
|
||||
|
||||
if (tableDataList == null || tableDataList.size() == 0) {
|
||||
columnRespList.forEach(columnResp -> {
|
||||
String javaType = getJavaType(dataType.getDriverManager(), jdbcUrl, dataSource.getUser(),
|
||||
dataSource.getPassword(), showTableReq.getTableName(), columnResp.getColumnName());
|
||||
try {
|
||||
Class.forName(dataType.getDriverManager());
|
||||
Connection connection = DriverManager.getConnection(jdbcUrl, dataSource.getUser(), dataSource.getPassword());
|
||||
Statement statement = connection.createStatement();
|
||||
String sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS where table_schema ='" + dataSource.getDatabaseName() + "' AND table_name = '" + showTableReq.getTableName() + "'";
|
||||
ResultSet rs = statement.executeQuery(sql);
|
||||
while (rs.next()) {
|
||||
String columnName = rs.getString("COLUMN_NAME");
|
||||
String columnComment = rs.getString("COLUMN_COMMENT");
|
||||
String columnKey = rs.getString("COLUMN_KEY");
|
||||
String columnType = rs.getString("DATA_TYPE");
|
||||
String javaType = getJavaType(dataType.getDriverManager(), jdbcUrl, dataSource.getUser(), dataSource.getPassword(), showTableReq.getTableName(), columnName);
|
||||
String type = rs.getString("COLUMN_TYPE");
|
||||
int length = 0;
|
||||
Pattern pattern = Pattern.compile("\\d+");
|
||||
Matcher matcher = pattern.matcher(type);
|
||||
if (matcher.find()) {
|
||||
String numberString = matcher.group();
|
||||
length = Integer.parseInt(numberString);
|
||||
}
|
||||
int numericScale = rs.getInt("NUMERIC_SCALE");
|
||||
String isNullable = rs.getString("IS_NULLABLE");
|
||||
String columnDefault = rs.getString("COLUMN_DEFAULT");
|
||||
TableData tableData = TableData.builder()
|
||||
.name(columnResp.getColumnName())
|
||||
.comment(columnResp.getColumnComment())
|
||||
.isPrimaryKey(columnResp.getColumnKey().equals("PRI") ? "Y" : "N")
|
||||
.type(columnResp.getColumnType())
|
||||
.name(columnName)
|
||||
.comment(columnComment)
|
||||
.isPrimaryKey(columnKey.equals("PRI") ? "Y" : "N")
|
||||
.type(columnType)
|
||||
.mappingType(javaType)
|
||||
.length(columnResp.getLength())
|
||||
.decimalPlaces(columnResp.getNumericScale())
|
||||
.isNull(columnResp.getIsNullable().equals("YES") ? "Y" : "N")
|
||||
.defaultValue(columnResp.getColumnDefault())
|
||||
.length(Long.valueOf(length))
|
||||
.decimalPlaces(Long.valueOf(numericScale))
|
||||
.isNull(isNullable.equals("YES") ? "Y" : "N")
|
||||
.defaultValue(columnDefault)
|
||||
.isDict("N")
|
||||
.dictKey(null)
|
||||
.childrenId(children.getId())
|
||||
.build();
|
||||
tableDataService.save(tableData);
|
||||
});
|
||||
}
|
||||
|
||||
connection.close();
|
||||
rs.close();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// version ++;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CountResp selectTableDataCount() {
|
||||
|
@ -298,7 +364,9 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
|
|||
.build();
|
||||
}
|
||||
|
||||
public static String getJavaType(String driveClass, String url, String username, String password, String tableName, String columnName) {
|
||||
|
||||
public static String getJavaType(String driveClass, String url, String username, String password, String tableName,
|
||||
String columnName) {
|
||||
Connection connection = buildConnection(driveClass, url, username, password);
|
||||
PreparedStatement pst = null;
|
||||
String javaType = null;
|
||||
|
|
Loading…
Reference in New Issue