style: 优化了不同数据库同步数据的代码,和数据接入的功能优化
将后台同步数据功能完善了一下,使其不再使用sql,而使用jdbc封装的方法进行查询所需数据,数据接入在添加或修改时,会根据所选择的数据库类型进行后台默认添加对应的连接驱动jdbcDrivermaster
parent
0689fdb194
commit
7f0e928cd5
|
@ -66,6 +66,11 @@ public class DataSourceController extends BaseController
|
||||||
return dataSourceService.dataAssetList(dataSource);
|
return dataSourceService.dataAssetList(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/Statistics")
|
||||||
|
public Result statistics(){
|
||||||
|
return dataSourceService.statistics();
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/AssetModelList")
|
@PostMapping("/AssetModelList")
|
||||||
public Result assetModelList(@RequestBody DataAsset dataAsset)
|
public Result assetModelList(@RequestBody DataAsset dataAsset)
|
||||||
{
|
{
|
||||||
|
|
|
@ -93,11 +93,22 @@ public class DataSource extends BaseEntity
|
||||||
/** 数据来源名称 */
|
/** 数据来源名称 */
|
||||||
@Excel(name = "数据来源名称")
|
@Excel(name = "数据来源名称")
|
||||||
private String systemName;
|
private String systemName;
|
||||||
|
/** 连接驱动名称 */
|
||||||
|
@Excel(name = "连接驱动名称")
|
||||||
|
private String jdbcDriver;
|
||||||
|
|
||||||
/** 模式名称 */
|
/** 模式名称 */
|
||||||
@Excel(name = "数据来源名称")
|
@Excel(name = "数据来源名称")
|
||||||
private String modeName;
|
private String modeName;
|
||||||
|
|
||||||
|
public String getJdbcDriver() {
|
||||||
|
return jdbcDriver;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJdbcDriver(String jdbcDriver) {
|
||||||
|
this.jdbcDriver = jdbcDriver;
|
||||||
|
}
|
||||||
|
|
||||||
public String getModeName() {
|
public String getModeName() {
|
||||||
return modeName;
|
return modeName;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.muyu.etl.domain.custom;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName Statistics
|
||||||
|
* @Description 描述
|
||||||
|
* @Author Xin.Yao
|
||||||
|
* @Date 2024/4/23 20:43
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Statistics {
|
||||||
|
private Long dataAsset;
|
||||||
|
private Long assetModel;
|
||||||
|
private Long dataModel;
|
||||||
|
}
|
|
@ -62,4 +62,6 @@ public interface DataAssetMapper
|
||||||
public int deleteDataAssetByIds(Long[] ids);
|
public int deleteDataAssetByIds(Long[] ids);
|
||||||
|
|
||||||
void batchInsert(@Param("dataAssets") ArrayList<DataAsset> dataAssets);
|
void batchInsert(@Param("dataAssets") ArrayList<DataAsset> dataAssets);
|
||||||
|
|
||||||
|
List<DataAsset> selectDataAssetBatchId(@Param("longs") List<Long> longs);
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,4 +73,6 @@ public interface IDataSourceService
|
||||||
Result dataAssetList(DataSource dataSource);
|
Result dataAssetList(DataSource dataSource);
|
||||||
|
|
||||||
Result assetModelList(DataAsset dataAsset);
|
Result assetModelList(DataAsset dataAsset);
|
||||||
|
|
||||||
|
Result statistics();
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,7 @@ import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.common.core.utils.DateUtils;
|
import com.muyu.common.core.utils.DateUtils;
|
||||||
import com.muyu.common.security.utils.SecurityUtils;
|
import com.muyu.common.security.utils.SecurityUtils;
|
||||||
import com.muyu.etl.domain.*;
|
import com.muyu.etl.domain.*;
|
||||||
import com.muyu.etl.domain.custom.AssetsModule;
|
import com.muyu.etl.domain.custom.*;
|
||||||
import com.muyu.etl.domain.custom.TableAssets;
|
|
||||||
import com.muyu.etl.domain.custom.TableDetail;
|
|
||||||
import com.muyu.etl.domain.custom.VTClass;
|
|
||||||
import com.muyu.etl.mapper.AssetModelMapper;
|
import com.muyu.etl.mapper.AssetModelMapper;
|
||||||
import com.muyu.etl.mapper.DataAssetMapper;
|
import com.muyu.etl.mapper.DataAssetMapper;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -116,6 +113,11 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
{
|
{
|
||||||
dataSource.setCreateTime(DateUtils.getNowDate());
|
dataSource.setCreateTime(DateUtils.getNowDate());
|
||||||
dataSource.setCreateBy(SecurityUtils.getUsername());
|
dataSource.setCreateBy(SecurityUtils.getUsername());
|
||||||
|
if ("MySql".equals(dataSource.getType())){
|
||||||
|
dataSource.setJdbcDriver("com.mysql.cj.jdbc.Driver");
|
||||||
|
}else{
|
||||||
|
dataSource.setJdbcDriver("org.postgresql.Driver");
|
||||||
|
}
|
||||||
return dataSourceMapper.insertDataSource(dataSource);
|
return dataSourceMapper.insertDataSource(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,6 +132,11 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
{
|
{
|
||||||
dataSource.setUpdateTime(DateUtils.getNowDate());
|
dataSource.setUpdateTime(DateUtils.getNowDate());
|
||||||
dataSource.setUpdateBy(SecurityUtils.getUsername());
|
dataSource.setUpdateBy(SecurityUtils.getUsername());
|
||||||
|
if ("MySql".equals(dataSource.getType())){
|
||||||
|
dataSource.setJdbcDriver("com.mysql.cj.jdbc.Driver");
|
||||||
|
}else{
|
||||||
|
dataSource.setJdbcDriver("org.postgresql.Driver");
|
||||||
|
}
|
||||||
return dataSourceMapper.updateDataSource(dataSource);
|
return dataSourceMapper.updateDataSource(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,62 +166,37 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result testConnection(DataSource dataSource) {
|
public Result testConnection(DataSource dataSource) {
|
||||||
if (dataSource.getType().equals("MySql")){
|
String jdbcUrl = "jdbc:"+dataSource.getType().toLowerCase()+"://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
||||||
String user = dataSource.getUsername();
|
|
||||||
String password = dataSource.getPassword();
|
|
||||||
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
|
|
||||||
String jdbcUrl = "jdbc:mysql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
if (dataSource.getConnectionParam()!=null && dataSource.getConnectionParam()!=""){
|
if (dataSource.getConnectionParam()!=null && dataSource.getConnectionParam()!=""){
|
||||||
jdbcUrl = jdbcUrl+"?"+dataSource.getConnectionParam();
|
jdbcUrl = jdbcUrl+"?"+dataSource.getConnectionParam();
|
||||||
}
|
}
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
try {
|
try {
|
||||||
Class.forName(jdbcDriver);
|
Class.forName(dataSource.getJdbcDriver());
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
return Result.error("连接失败");
|
return Result.error("连接失败");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
conn = DriverManager.getConnection(jdbcUrl, user, password);
|
conn = DriverManager.getConnection(jdbcUrl, dataSource.getUsername(), dataSource.getPassword());
|
||||||
conn.close();
|
conn.close();
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
return Result.error("连接失败");
|
return Result.error("连接失败");
|
||||||
}
|
}
|
||||||
}else{
|
|
||||||
String user = dataSource.getUsername();
|
|
||||||
String password = dataSource.getPassword();
|
|
||||||
String jdbcDriver = "org.postgresql.Driver";
|
|
||||||
String jdbcUrl = "jdbc:postgresql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
if (dataSource.getConnectionParam()!=null && dataSource.getConnectionParam()!=""){
|
|
||||||
jdbcUrl = jdbcUrl+"?"+dataSource.getConnectionParam();
|
|
||||||
}
|
|
||||||
Connection conn = null;
|
|
||||||
try {
|
|
||||||
Class.forName(jdbcDriver);
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
return Result.error("连接失败");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
conn = DriverManager.getConnection(jdbcUrl, user, password);
|
|
||||||
conn.close();
|
|
||||||
}catch (Exception e){
|
|
||||||
return Result.error("连接失败");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result.success("连接成功");
|
return Result.success("连接成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
public AssetsModule getStructure(DataSource dataSource){
|
public AssetsModule getStructure(DataSource dataSource){
|
||||||
String user = dataSource.getUsername();
|
String jdbcUrl = "jdbc:"+dataSource.getType().toLowerCase()+"://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
||||||
String password = dataSource.getPassword();
|
if (dataSource.getConnectionParam()!=null && dataSource.getConnectionParam()!=""){
|
||||||
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
|
jdbcUrl = jdbcUrl+"?"+dataSource.getConnectionParam();
|
||||||
String jdbcUrl = "jdbc:mysql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
}
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
List<Map<String, VTClass>> kvtList = new ArrayList<>();
|
List<Map<String, VTClass>> kvtList = new ArrayList<>();
|
||||||
HashMap<String, String> map = new HashMap<>();
|
HashMap<String, String> map = new HashMap<>();
|
||||||
try {
|
try {
|
||||||
Class.forName(jdbcDriver);
|
Class.forName(dataSource.getJdbcDriver());
|
||||||
conn = DriverManager.getConnection(jdbcUrl, user, password);
|
conn = DriverManager.getConnection(jdbcUrl, dataSource.getUsername(), dataSource.getPassword());
|
||||||
} catch (SQLException | ClassNotFoundException e) {
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -224,10 +206,6 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
ResultSetMetaData rsd = resultSet.getMetaData();
|
ResultSetMetaData rsd = resultSet.getMetaData();
|
||||||
for(int i = 1; i <= rsd.getColumnCount(); i++) {
|
for(int i = 1; i <= rsd.getColumnCount(); i++) {
|
||||||
String substring = rsd.getColumnClassName(i).substring(rsd.getColumnClassName(i).indexOf("java.lang.") + 10);
|
String substring = rsd.getColumnClassName(i).substring(rsd.getColumnClassName(i).indexOf("java.lang.") + 10);
|
||||||
System.out.print("java类型:"+substring);
|
|
||||||
System.out.print(" 数据库类型:"+rsd.getColumnTypeName(i));
|
|
||||||
System.out.print(" 字段名称:"+rsd.getColumnName(i));
|
|
||||||
System.out.println();
|
|
||||||
map.put(rsd.getColumnName(i),substring);
|
map.put(rsd.getColumnName(i),substring);
|
||||||
}
|
}
|
||||||
int columnCount = rsd.getColumnCount();
|
int columnCount = rsd.getColumnCount();
|
||||||
|
@ -258,25 +236,19 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String,String> getTypeMap(DataSource dataSource, String tableName){
|
public Map<String,String> getTypeMap(DataSource dataSource, String tableName){
|
||||||
String user = dataSource.getUsername();
|
|
||||||
String password = dataSource.getPassword();
|
|
||||||
String jdbcDriver="";
|
|
||||||
String jdbcUrl="";
|
String jdbcUrl="";
|
||||||
String sql="";
|
String sql="";
|
||||||
|
jdbcUrl = "jdbc:"+dataSource.getType().toLowerCase()+"://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
||||||
if (dataSource.getType().equals("MySql")){
|
if (dataSource.getType().equals("MySql")){
|
||||||
jdbcDriver = "com.mysql.cj.jdbc.Driver";
|
|
||||||
jdbcUrl = "jdbc:mysql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
sql = "select * from "+tableName;
|
sql = "select * from "+tableName;
|
||||||
}else{
|
}else{
|
||||||
jdbcDriver = "org.postgresql.Driver";
|
|
||||||
jdbcUrl = "jdbc:postgresql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
sql = "select * from "+dataSource.getModeName()+"."+tableName;
|
sql = "select * from "+dataSource.getModeName()+"."+tableName;
|
||||||
}
|
}
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
Map<String, String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
try {
|
try {
|
||||||
Class.forName(jdbcDriver);
|
Class.forName(dataSource.getJdbcDriver());
|
||||||
conn = DriverManager.getConnection(jdbcUrl, user, password);
|
conn = DriverManager.getConnection(jdbcUrl, dataSource.getUsername(), dataSource.getPassword());
|
||||||
} catch (SQLException | ClassNotFoundException e) {
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -294,7 +266,7 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
substring = rsd.getColumnClassName(i);
|
substring = rsd.getColumnClassName(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
map.put(rsd.getColumnName(i).toUpperCase(),substring);
|
map.put(rsd.getColumnName(i),substring);
|
||||||
}
|
}
|
||||||
pst.close();
|
pst.close();
|
||||||
pst=null;
|
pst=null;
|
||||||
|
@ -305,153 +277,64 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<AssetModel> getTableAssets(DataSource dataSource, String tableName){
|
public List<AssetModel> getTableAssets(DataSource dataSource, String tableName){
|
||||||
String user = dataSource.getUsername();
|
|
||||||
String password = dataSource.getPassword();
|
|
||||||
String jdbcDriver="";
|
|
||||||
String jdbcUrl="";
|
String jdbcUrl="";
|
||||||
String sql="";
|
jdbcUrl = "jdbc:"+dataSource.getType().toLowerCase()+"://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
||||||
if (dataSource.getType().equals("MySql")){
|
|
||||||
jdbcDriver = "com.mysql.cj.jdbc.Driver";
|
|
||||||
jdbcUrl = "jdbc:mysql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
sql="SELECT COLUMN_NAME,DATA_TYPE,IS_NULLABLE,COLUMN_KEY,COLUMN_DEFAULT,COLUMN_COMMENT,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '"+dataSource.getDatabaseName()+"' AND TABLE_NAME = '"+tableName+"'";
|
|
||||||
}else{
|
|
||||||
jdbcDriver = "org.postgresql.Driver";
|
|
||||||
jdbcUrl = "jdbc:postgresql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
sql="SELECT \n" +
|
|
||||||
" a.attname AS \"COLUMN_NAME\",\n" +
|
|
||||||
" coalesce(cc.description, '') AS \"COLUMN_COMMENT\",\n" +
|
|
||||||
" EXISTS (\n" +
|
|
||||||
" SELECT 1\n" +
|
|
||||||
" FROM pg_index i\n" +
|
|
||||||
" JOIN pg_attribute ia ON ia.attrelid = i.indrelid AND ia.attnum = ANY(i.indkey)\n" +
|
|
||||||
" WHERE i.indrelid = a.attrelid AND ia.attname = a.attname AND i.indisprimary\n" +
|
|
||||||
" ) AS \"COLUMN_KEY\",\n" +
|
|
||||||
" format_type(a.atttypid, a.atttypmod) AS \"DATA_TYPE\",\n" +
|
|
||||||
" CASE \n" +
|
|
||||||
" WHEN strpos(format_type(a.atttypid, a.atttypmod), '(') > 0 THEN \n" +
|
|
||||||
" split_part(substring(format_type(a.atttypid, a.atttypmod) FROM '\\((.*)\\)')::text, ',', 1)::int\n" +
|
|
||||||
" ELSE \n" +
|
|
||||||
" NULL\n" +
|
|
||||||
" END AS \"NUMERIC_PRECISION\",\n" +
|
|
||||||
" CASE \n" +
|
|
||||||
" WHEN strpos(format_type(a.atttypid, a.atttypmod), ',') > 0 THEN \n" +
|
|
||||||
" split_part(substring(format_type(a.atttypid, a.atttypmod) FROM '\\((.*)\\)')::text, ',', 2)::int\n" +
|
|
||||||
" ELSE \n" +
|
|
||||||
" NULL\n" +
|
|
||||||
" END AS \"NUMERIC_SCALE\",\n" +
|
|
||||||
" COALESCE(pg_get_expr(ad.adbin, ad.adrelid), '') AS \"COLUMN_DEFAULT\",\n" +
|
|
||||||
" NOT a.attnotnull AS \"IS_NULLABLE\"\n" +
|
|
||||||
"FROM \n" +
|
|
||||||
" pg_catalog.pg_attribute a\n" +
|
|
||||||
"JOIN \n" +
|
|
||||||
" pg_catalog.pg_class c ON a.attrelid = c.oid\n" +
|
|
||||||
"JOIN \n" +
|
|
||||||
" pg_catalog.pg_namespace n ON c.relnamespace = n.oid\n" +
|
|
||||||
"LEFT JOIN \n" +
|
|
||||||
" pg_catalog.pg_description cc ON cc.objoid = a.attrelid AND cc.objsubid = a.attnum\n" +
|
|
||||||
"LEFT JOIN \n" +
|
|
||||||
" pg_catalog.pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum\n" +
|
|
||||||
"WHERE \n" +
|
|
||||||
" n.nspname = '"+dataSource.getModeName()+"' \n" +
|
|
||||||
" AND c.relname = '"+tableName+"' \n" +
|
|
||||||
" AND a.attnum > 0 \n" +
|
|
||||||
" AND NOT a.attisdropped \n" +
|
|
||||||
"ORDER BY \n" +
|
|
||||||
" a.attnum ";
|
|
||||||
}
|
|
||||||
|
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
List<AssetModel> assetModels = new ArrayList<>();
|
List<AssetModel> assetModels = new ArrayList<>();
|
||||||
Map<String, String> typeMap = getTypeMap(dataSource, tableName);
|
Map<String, String> typeMap = getTypeMap(dataSource, tableName);
|
||||||
try {
|
try {
|
||||||
Class.forName(jdbcDriver);
|
Class.forName(dataSource.getJdbcDriver());
|
||||||
conn = DriverManager.getConnection(jdbcUrl, user, password);
|
conn = DriverManager.getConnection(jdbcUrl, dataSource.getUsername(), dataSource.getPassword());
|
||||||
} catch (SQLException | ClassNotFoundException e) {
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Class.forName(jdbcDriver);
|
DatabaseMetaData metaData = conn.getMetaData();
|
||||||
conn = DriverManager.getConnection(jdbcUrl, user, password);
|
ResultSet columnsRS = metaData.getColumns(dataSource.getDatabaseName(),dataSource.getModeName(), tableName, "%");
|
||||||
} catch (SQLException | ClassNotFoundException e) {
|
ResultSet primaryKeyRS = metaData.getPrimaryKeys(dataSource.getDatabaseName(), dataSource.getModeName(), tableName);
|
||||||
throw new RuntimeException(e);
|
String primaryKeyColumnName ="";
|
||||||
|
while (primaryKeyRS.next()) {
|
||||||
|
primaryKeyColumnName = primaryKeyRS.getString("COLUMN_NAME");
|
||||||
}
|
}
|
||||||
try {
|
while (columnsRS.next()) {
|
||||||
PreparedStatement pst = conn.prepareStatement(sql);
|
|
||||||
ResultSet resultSet = pst.executeQuery();
|
|
||||||
ResultSetMetaData rsd = resultSet.getMetaData();
|
|
||||||
while (resultSet.next()) {
|
|
||||||
AssetModel assetModel = new AssetModel();
|
AssetModel assetModel = new AssetModel();
|
||||||
|
String columnName = columnsRS.getString("COLUMN_NAME");
|
||||||
assetModel.setComment(resultSet.getString("COLUMN_COMMENT"));
|
String typeName = columnsRS.getString("TYPE_NAME");
|
||||||
assetModel.setName(resultSet.getString("COLUMN_NAME"));
|
int columnSize = columnsRS.getInt("COLUMN_SIZE");
|
||||||
assetModel.setType(resultSet.getString("DATA_TYPE"));
|
int decimalDigits = columnsRS.getInt("DECIMAL_DIGITS");
|
||||||
if (dataSource.getType().equals("MySql")){
|
String isNullable = columnsRS.getString("IS_NULLABLE");
|
||||||
if (resultSet.getString("CHARACTER_MAXIMUM_LENGTH")!=null){
|
String columnDef = columnsRS.getString("COLUMN_DEF");
|
||||||
assetModel.setLength(resultSet.getString("CHARACTER_MAXIMUM_LENGTH"));
|
String remarks = columnsRS.getString("REMARKS");
|
||||||
}else if (resultSet.getString("NUMERIC_PRECISION")!=null){
|
|
||||||
assetModel.setLength(resultSet.getString("NUMERIC_PRECISION"));
|
|
||||||
}else{
|
|
||||||
assetModel.setLength("-");
|
|
||||||
}
|
|
||||||
//是否主键
|
|
||||||
if ("PRI".equals(resultSet.getString("COLUMN_KEY"))){
|
|
||||||
assetModel.setIsPrimaryKey("Y");
|
|
||||||
}else{
|
|
||||||
assetModel.setIsPrimaryKey("N");
|
|
||||||
}
|
|
||||||
//是否不可为空
|
|
||||||
if (resultSet.getString("IS_NULLABLE").equals("NO")){
|
|
||||||
assetModel.setIsNull("N");
|
|
||||||
}else{
|
|
||||||
assetModel.setIsNull("Y");
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
if (resultSet.getString("NUMERIC_PRECISION")!=null){
|
|
||||||
assetModel.setLength(resultSet.getString("NUMERIC_PRECISION"));
|
|
||||||
}else{
|
|
||||||
if (!assetModel.getType().contains("time")&&!assetModel.getType().contains("date")){
|
|
||||||
assetModel.setLength("10");
|
|
||||||
}else{
|
|
||||||
assetModel.setLength("-");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
//是否主键
|
|
||||||
if ("t".equals(resultSet.getString("COLUMN_KEY"))){
|
|
||||||
assetModel.setIsPrimaryKey("Y");
|
|
||||||
}else{
|
|
||||||
assetModel.setIsPrimaryKey("N");
|
|
||||||
}
|
|
||||||
//是否不可为空
|
|
||||||
if (resultSet.getString("IS_NULLABLE").equals("t")){
|
|
||||||
assetModel.setIsNull("N");
|
|
||||||
}else{
|
|
||||||
assetModel.setIsNull("Y");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (resultSet.getString("NUMERIC_SCALE")!=null){
|
|
||||||
assetModel.setDecimalPlaces(resultSet.getString("NUMERIC_SCALE"));
|
|
||||||
}else{
|
|
||||||
assetModel.setDecimalPlaces("-");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//是否有默认值
|
|
||||||
if (resultSet.getString("COLUMN_DEFAULT")!=null){
|
|
||||||
assetModel.setDefaultValue(resultSet.getString("COLUMN_DEFAULT"));
|
|
||||||
}else{
|
|
||||||
assetModel.setDefaultValue("-");
|
|
||||||
}
|
|
||||||
assetModel.setIsDict("");
|
|
||||||
assetModel.setDictKey("");
|
|
||||||
|
|
||||||
assetModel.setCreateBy(SecurityUtils.getUsername());
|
|
||||||
assetModel.setCreateTime(new Date());
|
assetModel.setCreateTime(new Date());
|
||||||
assetModel.setMappingType(typeMap.get(assetModel.getName().toUpperCase()));
|
assetModel.setCreateBy(SecurityUtils.getUsername());
|
||||||
|
assetModel.setLength(String.valueOf(columnSize));
|
||||||
|
assetModel.setDecimalPlaces(String.valueOf(decimalDigits));
|
||||||
|
assetModel.setName(columnName);
|
||||||
|
assetModel.setType(typeName);
|
||||||
|
assetModel.setMappingType(typeMap.get(columnName));
|
||||||
|
assetModel.setDictKey("");
|
||||||
|
assetModel.setIsDict("");
|
||||||
|
assetModel.setIsNull(isNullable.substring(0,1));
|
||||||
|
if (columnName.equals(primaryKeyColumnName)){
|
||||||
|
assetModel.setIsPrimaryKey("Y");
|
||||||
|
}else{
|
||||||
|
assetModel.setIsPrimaryKey("N");
|
||||||
|
}
|
||||||
|
if (remarks==null || remarks==""){
|
||||||
|
assetModel.setComment("-");
|
||||||
|
}else{
|
||||||
|
assetModel.setComment(remarks);
|
||||||
|
}
|
||||||
|
if (columnDef==null || columnDef==""){
|
||||||
|
assetModel.setDefaultValue("-");
|
||||||
|
}else{
|
||||||
|
assetModel.setDefaultValue(columnDef);
|
||||||
|
}
|
||||||
assetModels.add(assetModel);
|
assetModels.add(assetModel);
|
||||||
}
|
}
|
||||||
pst.close();
|
columnsRS.close();
|
||||||
|
primaryKeyRS.close();
|
||||||
|
conn.close();
|
||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -459,16 +342,13 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
}
|
}
|
||||||
|
|
||||||
public AssetsModule getAssets(DataSource dataSource){
|
public AssetsModule getAssets(DataSource dataSource){
|
||||||
String user = dataSource.getUsername();
|
String jdbcUrl = "jdbc:"+dataSource.getType().toLowerCase()+"://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
||||||
String password = dataSource.getPassword();
|
|
||||||
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
|
|
||||||
String jdbcUrl = "jdbc:mysql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
HashMap<String, String> map = new HashMap<>();
|
HashMap<String, String> map = new HashMap<>();
|
||||||
List<AssetModel> assetModels = getTableAssets(dataSource,dataSource.getTableName());
|
List<AssetModel> assetModels = getTableAssets(dataSource,dataSource.getTableName());
|
||||||
try {
|
try {
|
||||||
Class.forName(jdbcDriver);
|
Class.forName(dataSource.getJdbcDriver());
|
||||||
conn = DriverManager.getConnection(jdbcUrl, user, password);
|
conn = DriverManager.getConnection(jdbcUrl, dataSource.getUsername(), dataSource.getPassword());
|
||||||
} catch (SQLException | ClassNotFoundException e) {
|
} catch (SQLException | ClassNotFoundException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -507,18 +387,12 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Result synchronousData(DataSource dataSource) {
|
public Result synchronousData(DataSource dataSource) {
|
||||||
String user = dataSource.getUsername();
|
|
||||||
String password = dataSource.getPassword();
|
|
||||||
String jdbcDriver ="";
|
|
||||||
String jdbcUrl = "";
|
String jdbcUrl = "";
|
||||||
String sql="";
|
String sql="";
|
||||||
|
jdbcUrl = "jdbc:"+dataSource.getType().toLowerCase()+"://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
||||||
if (dataSource.getType().equals("MySql")){
|
if (dataSource.getType().equals("MySql")){
|
||||||
jdbcDriver = "com.mysql.cj.jdbc.Driver";
|
|
||||||
jdbcUrl = "jdbc:mysql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
sql="SELECT TABLE_NAME t_name,TABLE_COMMENT table_comment,TABLE_ROWS table_rows,(SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE TABLE_SCHEMA = '"+dataSource.getDatabaseName()+"' and TABLE_NAME=t_name) fields FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='"+dataSource.getDatabaseName()+"'";
|
sql="SELECT TABLE_NAME t_name,TABLE_COMMENT table_comment,TABLE_ROWS table_rows,(SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE TABLE_SCHEMA = '"+dataSource.getDatabaseName()+"' and TABLE_NAME=t_name) fields FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='"+dataSource.getDatabaseName()+"'";
|
||||||
}else{
|
}else{
|
||||||
jdbcDriver = "org.postgresql.Driver";
|
|
||||||
jdbcUrl = "jdbc:postgresql://"+dataSource.getLinkAddress()+":"+dataSource.getPort()+"/"+dataSource.getDatabaseName();
|
|
||||||
sql="SELECT \n" +
|
sql="SELECT \n" +
|
||||||
" c.relname AS t_name,\n" +
|
" c.relname AS t_name,\n" +
|
||||||
" pgd.description AS table_comment,\n" +
|
" pgd.description AS table_comment,\n" +
|
||||||
|
@ -543,14 +417,18 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
HashMap<String, String> map = new HashMap<>();
|
HashMap<String, String> map = new HashMap<>();
|
||||||
ArrayList<DataAsset> dataAssets = new ArrayList<>();
|
ArrayList<DataAsset> dataAssets = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
Class.forName(jdbcDriver);
|
Class.forName(dataSource.getJdbcDriver());
|
||||||
conn = DriverManager.getConnection(jdbcUrl, user, password);
|
conn = DriverManager.getConnection(jdbcUrl, dataSource.getUsername(), dataSource.getPassword());
|
||||||
PreparedStatement ps = conn.prepareStatement(sql);
|
PreparedStatement ps = conn.prepareStatement(sql);
|
||||||
ResultSet resultSet = ps.executeQuery();
|
ResultSet resultSet = ps.executeQuery();
|
||||||
while (resultSet.next()){
|
while (resultSet.next()){
|
||||||
DataAsset dataAsset = new DataAsset();
|
DataAsset dataAsset = new DataAsset();
|
||||||
dataAsset.setTableName(resultSet.getString("t_name"));
|
dataAsset.setTableName(resultSet.getString("t_name"));
|
||||||
|
if (resultSet.getString("table_comment")==null || "".equals(resultSet.getString("table_comment"))){
|
||||||
|
dataAsset.setTableComment("-");
|
||||||
|
}else{
|
||||||
dataAsset.setTableComment(resultSet.getString("table_comment"));
|
dataAsset.setTableComment(resultSet.getString("table_comment"));
|
||||||
|
}
|
||||||
dataAsset.setTableCount(Long.valueOf(resultSet.getString("table_rows")));
|
dataAsset.setTableCount(Long.valueOf(resultSet.getString("table_rows")));
|
||||||
dataAsset.setFields(Long.valueOf(resultSet.getString("fields")));
|
dataAsset.setFields(Long.valueOf(resultSet.getString("fields")));
|
||||||
dataAsset.setDataSourceId(dataSource.getId());
|
dataAsset.setDataSourceId(dataSource.getId());
|
||||||
|
@ -585,4 +463,22 @@ public class DataSourceServiceImpl implements IDataSourceService
|
||||||
List<AssetModel> assetModels = assetModelMapper.selectAssetModelList(assetModel);
|
List<AssetModel> assetModels = assetModelMapper.selectAssetModelList(assetModel);
|
||||||
return Result.success(assetModels);
|
return Result.success(assetModels);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result statistics() {
|
||||||
|
//查询所有数据源
|
||||||
|
List<DataSource> dataSources = dataSourceMapper.selectDataSourceList(new DataSource());
|
||||||
|
//获取所有数据源的主键
|
||||||
|
List<Long> longs = dataSources.stream().map(dataSource -> dataSource.getId()).toList();
|
||||||
|
List<DataAsset> dataAssetList=dataAssetMapper.selectDataAssetBatchId(longs);
|
||||||
|
Statistics statistics = new Statistics();
|
||||||
|
statistics.setDataAsset(Long.valueOf(dataSources.size()));
|
||||||
|
long sum1;
|
||||||
|
long sum2=0;
|
||||||
|
sum1 = dataAssetList.stream().mapToLong(dataAsset -> Long.valueOf(dataAsset.getFields())).sum();
|
||||||
|
sum2 = dataAssetList.stream().mapToLong(dataAsset -> Long.valueOf(dataAsset.getTableCount())).sum();
|
||||||
|
statistics.setAssetModel(sum1);
|
||||||
|
statistics.setDataModel(sum2);
|
||||||
|
return Result.success(statistics);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,6 +37,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
<include refid="selectDataAssetVo"/>
|
<include refid="selectDataAssetVo"/>
|
||||||
where id = #{id}
|
where id = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="selectDataAssetBatchId" resultType="com.muyu.etl.domain.DataAsset">
|
||||||
|
select * from data_asset where data_source_id
|
||||||
|
in (
|
||||||
|
<foreach collection="longs" item="id" separator=",">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
)
|
||||||
|
</select>
|
||||||
|
|
||||||
<insert id="insertDataAsset" parameterType="com.muyu.etl.domain.DataAsset" useGeneratedKeys="true" keyProperty="id">
|
<insert id="insertDataAsset" parameterType="com.muyu.etl.domain.DataAsset" useGeneratedKeys="true" keyProperty="id">
|
||||||
insert into data_asset
|
insert into data_asset
|
||||||
|
|
|
@ -25,10 +25,11 @@
|
||||||
<result property="type" column="type" />
|
<result property="type" column="type" />
|
||||||
<result property="systemName" column="system_name" />
|
<result property="systemName" column="system_name" />
|
||||||
<result property="modeName" column="mode_name" />
|
<result property="modeName" column="mode_name" />
|
||||||
|
<result property="jdbcDriver" column="jdbc_driver" />
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
<sql id="selectDataSourceVo">
|
<sql id="selectDataSourceVo">
|
||||||
select id, data_source_name, link_address, port, database_name, username, password, connection_param, init_num, max_num, max_wait_time, max_wait_size, create_by, create_time, update_by, update_time, remark, type, system_name,mode_name from data_source
|
select id, data_source_name, link_address, port, database_name, username, password, connection_param, init_num, max_num, max_wait_time, max_wait_size, create_by, create_time, update_by, update_time, remark, type, system_name,mode_name,jdbc_driver from data_source
|
||||||
</sql>
|
</sql>
|
||||||
|
|
||||||
<select id="selectDataSourceList" parameterType="com.muyu.etl.domain.DataSource" resultMap="DataSourceResult">
|
<select id="selectDataSourceList" parameterType="com.muyu.etl.domain.DataSource" resultMap="DataSourceResult">
|
||||||
|
@ -48,6 +49,7 @@
|
||||||
<if test="type != null and type != ''"> and type = #{type}</if>
|
<if test="type != null and type != ''"> and type = #{type}</if>
|
||||||
<if test="systemName != null and systemName != ''"> and system_name like concat('%', #{systemName}, '%')</if>
|
<if test="systemName != null and systemName != ''"> and system_name like concat('%', #{systemName}, '%')</if>
|
||||||
<if test="modeName != null and modeName != ''"> and mode_name like concat('%', #{modeName}, '%')</if>
|
<if test="modeName != null and modeName != ''"> and mode_name like concat('%', #{modeName}, '%')</if>
|
||||||
|
<if test="jdbcDriver != null and jdbcDriver != ''"> and jdbc_driver like concat('%', #{jdbcDriver}, '%')</if>
|
||||||
</where>
|
</where>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
@ -78,6 +80,7 @@
|
||||||
<if test="type != null">type,</if>
|
<if test="type != null">type,</if>
|
||||||
<if test="systemName != null">system_name,</if>
|
<if test="systemName != null">system_name,</if>
|
||||||
<if test="modeName != null">mode_name,</if>
|
<if test="modeName != null">mode_name,</if>
|
||||||
|
<if test="jdbcDriver != null">jdbc_driver,</if>
|
||||||
</trim>
|
</trim>
|
||||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||||
<if test="dataSourceName != null">#{dataSourceName},</if>
|
<if test="dataSourceName != null">#{dataSourceName},</if>
|
||||||
|
@ -99,6 +102,7 @@
|
||||||
<if test="type != null">#{type},</if>
|
<if test="type != null">#{type},</if>
|
||||||
<if test="systemName != null">#{systemName},</if>
|
<if test="systemName != null">#{systemName},</if>
|
||||||
<if test="modeName != null">#{modeName},</if>
|
<if test="modeName != null">#{modeName},</if>
|
||||||
|
<if test="jdbcDriver != null">#{jdbcDriver},</if>
|
||||||
</trim>
|
</trim>
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
@ -124,6 +128,7 @@
|
||||||
<if test="type != null">type = #{type},</if>
|
<if test="type != null">type = #{type},</if>
|
||||||
<if test="systemName != null">system_name = #{systemName},</if>
|
<if test="systemName != null">system_name = #{systemName},</if>
|
||||||
<if test="modeName != null">mode_name = #{modeName},</if>
|
<if test="modeName != null">mode_name = #{modeName},</if>
|
||||||
|
<if test="jdbcDriver != null">jdbc_driver = #{jdbcDriver},</if>
|
||||||
</trim>
|
</trim>
|
||||||
where id = #{id}
|
where id = #{id}
|
||||||
</update>
|
</update>
|
||||||
|
|
Loading…
Reference in New Issue