Commit 2b3d632c authored by sml's avatar sml

冲突解决

parents 3665a344 fef4fdb3
alter table dmp_develop_task add SOURCE_DB_ID varchar(64) default NULL COMMENT'源数据库ID';
alter table dmp_develop_task add SOURCE_DB_NAME varchar(64) default NULL COMMENT'源数据库名称';
alter table dmp_develop_task add SOURCE_TABLE_NAME varchar(64) default NULL COMMENT'源数据表名称';
alter table dmp_develop_task add TARGET_DB_NAME varchar(64) default NULL COMMENT'目标数据库名称';
alter table dmp_develop_task add TARGET_TABLE_NAME varchar(64) default NULL COMMENT'目标数据表名称';
\ No newline at end of file
package com.jz.agent.client;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
public interface DmpDsAgentClient {
//连接
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds);
//获取数据库表
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds);
//获取表字段名
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName);
//获取表数据预览
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName);
}
package com.jz.agent.client;
import com.jz.agent.client.service.*;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
public class DmpDsAgentClientBuilder {
public static DmpDsAgentClient build(DmpAgentDatasourceInfo ds) {
switch (ds.getDatasourceType()) {
case "MYSQL":
return new DmpMysqlAgentClientService();
case "SQLSERVER":
return new DmpSqlServerAgentClientService();
case "POSTGRESQL":
return new DmpPostgreSqlAgentClientService();
case "ORACLE":
return new DmpOracleAgentClientService();
case "DM":
return null;
case "DB2":
return new DmpDB2AgentClientService();
case "HIVE":
return new DmpHiveAgentClientService();
case "IMPALA":
return null;
case "KUDU":
return new DmpKuduAgentClientService();
// return SpringUtils.getBean(DmpKuduAgentClientService.class);
case "HDFS":
return new DmpHdfsAgentClientService();
case "FTP":
return new DmpFtpAgentClientService();
case "ELASTICSEARCH":
return new DmpElasticsearchAgentClientService();
case "MONGODB":
return null;
case "MEMCACHE":
return null;
case "REDIS":
return null;
case "HBASE":
return null;
case "LOGHUB":
return null;
case "KAFKA":
return null;
case "ROCKETMQ":
return null;
case "DEFAULT":
return new DmpHiveAgentClientService();
default:
break;
}
return null;
}
}
package com.jz.agent.client.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.agent.utils.JDBCUtils;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.agent.ResultCode;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DmpDB2AgentClientService implements DmpDsAgentClient {
private Gson gson = new Gson();
@SuppressWarnings("unused")
private static Logger logger = LoggerFactory.getLogger(DmpDB2AgentClientService.class);
@SuppressWarnings("unused")
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
Connection conn = null;
try {
Class.forName(ds.getDriverClassName());
conn = DriverManager.getConnection(
ds.getJdbcUrl(),
ds.getUserName(),
ds.getPassword()
);
conn.close();
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(true));
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "DB2连通性异常:" + e.getMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
List result = new ArrayList();
String sql = "select * from information_schema.tables where table_schema = '" + this.getTargetDBName(ds) + "'";
Connection conn = null;
ResultSet rs;
PreparedStatement ps;
try {
conn = JDBCUtils.getConnections(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
result.add(rs.getString("TABLE_NAME").toLowerCase());
}
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "DB2获取表列表异常:" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "DB2获取表列表异常:" + e.getMessage());
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(result));
}
private String getTargetDBName(DmpAgentDatasourceInfo ds) {
String url = ds.getJdbcUrl();
if (url != null && !"".equals(url.trim())) {
String[] arr1 = url.split("/");
String ls = arr1[arr1.length - 1];
String[] arr2 = ls.split("\\?");
return arr2[0];
} else {
return "";
}
}
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String sql = "select * from information_schema.columns where table_name = '" + tableName + "'";
Connection conn = null;
ResultSet rs;
PreparedStatement ps;
try {
conn = JDBCUtils.getConnections(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Map map = new HashMap();
String colName = rs.getString("COLUMN_NAME");
String dbType = rs.getString("DATA_TYPE");
String remarks = rs.getString("COLUMN_COMMENT");
Integer size = 0;
Integer characterLength = (int) rs.getDouble("CHARACTER_MAXIMUM_LENGTH");
Integer columnSize = (int) rs.getDouble("NUMERIC_PRECISION");
Integer dicimalDigits = (int) rs.getDouble("NUMERIC_SCALE");
if (characterLength != null) {
size = characterLength;
} else {
size = columnSize;
}
map.put("name", colName.toLowerCase());
map.put("type", dbType.toUpperCase());
map.put("size", size);
map.put("scale", dicimalDigits);
map.put("comment", remarks);
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "DB2获取表字段信息异常:" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "DB2获取表字段信息异常:" + e.getMessage());
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName) {
Map<String, Object> map = new HashMap<String, Object>();
Connection conn = null;
ResultSet rs;
PreparedStatement ps;
List<String> headerList = new ArrayList<String>();
try {
conn = JDBCUtils.getConnections(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
String sql = "select * from " + tableName + " limit 5";
String sql2 = "select * from information_schema.columns where table_name='" + tableName + "'";
ps = conn.prepareStatement(sql2);
rs = ps.executeQuery();
while (rs.next()) {
String colName = rs.getString("COLUMN_NAME");
headerList.add(colName);
}
map.put("header", headerList);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
List<List<Object>> resultList = new ArrayList<List<Object>>();
while (rs.next()) {
List<Object> row = new ArrayList<Object>();
for (int i = 1; i <= headerList.size(); i++) {
row.add(rs.getObject(i));
}
resultList.add(row);
}
map.put("result", resultList);
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "DB2数据预览异常:" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "DB2数据预览异常:" + e.getMessage());
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(map));
}
}
package com.jz.agent.client.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.agent.ResultCode;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DmpElasticsearchAgentClientService implements DmpDsAgentClient {
private Gson gson = new Gson();
@SuppressWarnings("unused")
private static Logger logger = LoggerFactory.getLogger(DmpElasticsearchAgentClientService.class);
@SuppressWarnings("unused")
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(false));
}
@SuppressWarnings({"unchecked", "rawtypes"})
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
List<String> list = new ArrayList<String>();
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
@SuppressWarnings({"rawtypes", "unchecked", "unused"})
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName) {
Map<String, Object> map = new HashMap<String, Object>();
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(map));
}
}
package com.jz.agent.client.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.agent.utils.JDBCUtils;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.agent.ResultCode;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DmpHiveAgentClientService implements DmpDsAgentClient {
private Gson gson = new Gson();
private static Logger logger = LoggerFactory.getLogger(DmpHiveAgentClientService.class);
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
Connection connection = null;
String jdbcUrl = ds.getJdbcUrl();
try {
jdbcUrl += securityConnect(ds);
Class.forName(ds.getDriverClassName());
connection = DriverManager.getConnection(jdbcUrl);
connection.close();
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(true));
} catch (Exception e) {
logger.error("HIVE连通性异常", e);
return new DmpAgentResult(ResultCode.EXCEPTION, "HIVE连通性异常:" + e.getMessage());
} finally {
JDBCUtils.disconnect(connection, null, null);
}
}
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
List<String> list = new ArrayList();
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String jdbcUrl = ds.getJdbcUrl();
String sql = "show tables";
try {
jdbcUrl += securityConnect(ds);
Class.forName(ds.getDriverClassName());
connection = DriverManager.getConnection(jdbcUrl);
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
list.add(rs.getString("tab_name"));
}
} catch (Exception e) {
logger.error("HIVE获取表列表异常", e);
return new DmpAgentResult(ResultCode.EXCEPTION, "HIVE获取表列表异常:" + e.getMessage());
} finally {
JDBCUtils.disconnect(connection, rs, ps);
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName) {
List<Map<String, Object>> list = new ArrayList();
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String jdbcUrl = ds.getJdbcUrl();
String sql = "DESCRIBE " + tableName;
try {
jdbcUrl += securityConnect(ds);
Class.forName(ds.getDriverClassName());
connection = DriverManager.getConnection(jdbcUrl);
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Map<String, Object> m = new HashMap<String, Object>();
String col_name = rs.getString("col_name");
String data_type = rs.getString("data_type");
if(!"".equals(col_name) && data_type != null) {
m.put("name", rs.getString("col_name").toLowerCase());
m.put("type", rs.getString("data_type").toUpperCase());
m.put("comment", rs.getString("comment"));
list.add(m);
}
}
} catch (Exception e) {
logger.error("HIVE获取表属性异常", e);
return new DmpAgentResult(ResultCode.EXCEPTION, "HIVE获取表属性异常:" + e.getMessage());
} finally {
JDBCUtils.disconnect(connection, rs, ps);
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName) {
Map<String, Object> map = new HashMap();
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(map));
}
/**
* 安全连接并返回jdbc连接url的安全请求后缀
*
* @param ds
* @return jdbc连接url的安全请求后缀
*/
private String securityConnect(DmpAgentDatasourceInfo ds) throws IOException {
String jdbcUrlSuffix = "";
// 与kudu大体无异,不同于安全校验
if ("true".equalsIgnoreCase(ds.getKerberosIsenable())) {
String principal = ds.getAccessId();
String hiveKeytabPath = ds.getAccessKey(); // D:/keytab/hive.keytab
String hiveUser = ds.getEndpoint(); // hive@CRNETTEST.COM
jdbcUrlSuffix = ";principal=" + principal;
System.setProperty("java.security.krb5.conf", ds.getKerberosKrb5Conf());
System.setProperty("java.security.auth.login.config", ds.getKerberosJaasConf());
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "kerberos");
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab(hiveUser, hiveKeytabPath);
}
return jdbcUrlSuffix;
}
public static void main(String[] args) {
Connection connection = null;
ResultSet rs = null;
PreparedStatement ps = null;
String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver";
String CONNECT_URL = "jdbc:hive2://10.0.53.177:10000/tmp;principal=hive/jtjzvdra116@CRNETTEST.COM";
// String sql = "show tables";
String sql = "DESCRIBE rzf_report_rzfyinliu_yp";
try {
System.setProperty("java.security.krb5.conf", "D:/keytab/krb5.conf");
System.setProperty("java.security.auth.login.config", "D:/keytab/jaas.conf");
Configuration conf = new Configuration();
conf.set("hadoop.security.authentication", "Kerberos");
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab("hive@CRNETTEST.COM", "D:/keytab/hive.keytab");
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(CONNECT_URL);
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
// System.out.println(rs.getString("tab_name"));
System.out.print(rs.getString("col_name").toLowerCase() + " = ");
System.out.print(rs.getString("data_type").toUpperCase() + " = ");
System.out.println(rs.getString("comment") + " = ");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.disconnect(connection, rs, ps);
}
}
}
package com.jz.agent.client.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.agent.config.DmpDsAgentConstants;
import com.jz.agent.utils.JDBCUtils;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.agent.ResultCode;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import org.apache.hadoop.security.UserGroupInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.PrivilegedAction;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("unused")
public class DmpImpalaAgentClientService implements DmpDsAgentClient {
private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
private Gson gson = new Gson();
private static Logger logger = LoggerFactory.getLogger(DmpImpalaAgentClientService.class);
static {
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
protected String CONNECTION_URL;
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
/*boolean flag = false;
Connection connection = null;
try {
connection = getConnection(ds);
if(connection != null) flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(connection != null)
JDBCUtils.disconnect(connection, null, null);
}
return flag;*/
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(true));
}
private Connection getConnection(DmpAgentDatasourceInfo ds) {
Connection connection = null;
ResultSet rs = null;
PreparedStatement ps = null;
CONNECTION_URL = ds.getJdbcUrl();
try {
if(DmpDsAgentConstants.IS_ENABLED_KERBEROS) {
UserGroupInformation loginUser = JDBCUtils.initKerberosENV4IMPALA();
CONNECTION_URL = CONNECTION_URL + ";AuthMech=1;KrbRealm="
+ DmpDsAgentConstants.KERBEROS_KEYTAB_ACCOUNT_IMPALA
+ ";KrbHostFQDN=" + ds.getAccessId() + ";KrbServiceName=impala";
connection = (Connection) loginUser.doAs(new PrivilegedAction<Object>(){
public Object run() {
Connection connection = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(CONNECTION_URL);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.disconnect(connection, rs, ps);
}
return connection;
}
});
} else {
connection = DriverManager.getConnection(CONNECTION_URL);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(connection != null)
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return connection;
}
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
String sql = "show tables"; // like '*likename*'
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
List<String> list = new ArrayList<String>();
try {
conn = getConnection(ds);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
list.add(rs.getString(1).toLowerCase());
}
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "Impala获取表列表异常:"+e.getMessage());
} finally {
if(rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName) {
String sql = "desc " + tableName;
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
conn = getConnection(ds);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Map<String, Object> row = new HashMap<String, Object>();
row.put("name", rs.getString(1).toLowerCase());
row.put("type", rs.getString(2).toUpperCase());
row.put("comment", rs.getString(3));
list.add(row);
}
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "Impala获取表字段信息异常:"+e.getMessage());
} finally {
if(rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
@SuppressWarnings("resource")
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName) {
Map<String, Object> map = new HashMap<String, Object>();
String sql = "select * from " + tableName + " limit 5";
String sql1 = "desc " + tableName;
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
conn = getConnection(ds);
ps = conn.prepareStatement(sql1);
rs = ps.executeQuery();
List<String> headerList = new ArrayList<String>();
while (rs.next()) {
headerList.add(rs.getString(1).toLowerCase());
}
map.put("header", headerList);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
List<List<Object>> resultList = new ArrayList<List<Object>>();
while (rs.next()) {
List<Object> row = new ArrayList<Object>();
for (int i = 1; i <= headerList.size(); i++) {
row.add(rs.getObject(i));
}
resultList.add(row);
}
map.put("result", resultList);
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "IMPALA数据预览异常:"+e.getMessage());
} finally {
if(rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(map));
}
}
package com.jz.agent.client.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.agent.utils.JDBCUtils;
import com.jz.common.utils.StringUtils;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.agent.ResultCode;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DmpMysqlAgentClientService implements DmpDsAgentClient {
private Gson gson = new Gson();
@SuppressWarnings("unused")
private static Logger logger = LoggerFactory.getLogger(DmpMysqlAgentClientService.class);
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
Connection conn = null;
try {
Class.forName(ds.getDriverClassName());
conn = DriverManager.getConnection(
ds.getJdbcUrl(),
ds.getUserName(),
ds.getPassword()
);
conn.close();
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(true));
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "MYSQL连通性异常:"+e.getMessage());
} finally {
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
List result = new ArrayList();
String sql = "select * from information_schema.tables where table_schema = '"+this.getTargetDBname(ds)+"'";
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnections(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
result.add(rs.getString("TABLE_NAME").toLowerCase());
}
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "MYSQL获取表列表异常:"+e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "MYSQL获取表列表异常:"+e.getMessage());
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(result));
}
private String getTargetDBname(DmpAgentDatasourceInfo ds) {
String url = ds.getJdbcUrl();
if(url != null && !"".equals(url.trim())) {
String[] arr1 = url.split("/");
String ls = arr1[arr1.length-1];
String[] arr2 = ls.split("\\?");
return arr2[0];
} else {
return "";
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
String dbName = "";
if (StringUtils.hasLength(ds.getDbName()))
dbName = " table_schema = '" + ds.getDbName() + "' and ";
String sql = "select * from information_schema.columns where " + dbName + "table_name = '" + tableName + "'";
Connection conn = null;
ResultSet rs;
PreparedStatement ps;
try {
conn = JDBCUtils.getConnections(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Map map = new HashMap();
String colName = rs.getString("COLUMN_NAME");
String dbType = rs.getString("DATA_TYPE");
String remarks = rs.getString("COLUMN_COMMENT");
Integer size= 0;
Integer characterLength = (int) rs.getDouble("CHARACTER_MAXIMUM_LENGTH");
Integer columnSize = (int) rs.getDouble("NUMERIC_PRECISION");
Integer dicimalDigits = (int) rs.getDouble("NUMERIC_SCALE");
if(characterLength != null) {
size = characterLength;
} else {
size = columnSize;
}
map.put("name", colName.toLowerCase());
map.put("type", dbType.toUpperCase());
map.put("size", size);
map.put("scale", dicimalDigits);
map.put("comment", remarks);
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "MYSQL获取表字段信息异常:"+e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "MYSQL获取表字段信息异常:"+e.getMessage());
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName) {
Map<String, Object> map = new HashMap<String, Object>();
Connection conn = null;
ResultSet rs;
PreparedStatement ps;
List<String> headerList = new ArrayList<String>();
try {
conn = JDBCUtils.getConnections(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
String sql = "select * from " + tableName + " limit 5";
String sql2 = "select * from information_schema.columns where table_name = '"+tableName+"'";
ps = conn.prepareStatement(sql2);
rs = ps.executeQuery();
while (rs.next()) {
String colName = rs.getString("COLUMN_NAME");
headerList.add(colName);
}
map.put("header", headerList);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
List<List<Object>> resultList = new ArrayList<List<Object>>();
while (rs.next()) {
List<Object> row = new ArrayList<Object>();
for(int i = 1; i <= headerList.size(); i++) {
row.add(rs.getObject(i));
}
resultList.add(row);
}
map.put("result", resultList);
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "MYSQL数据预览异常:"+e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "MYSQL数据预览异常:"+e.getMessage());
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(map));
}
}
package com.jz.agent.client.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.agent.utils.DbInfoUtil;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.agent.ResultCode;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class DmpOracleAgentClientService implements DmpDsAgentClient {
private Gson gson = new Gson();
private static Logger logger = LoggerFactory.getLogger(DmpOracleAgentClientService.class);
@SuppressWarnings("unused")
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
Connection conn = null;
try {
Class.forName(ds.getDriverClassName());
conn = DriverManager.getConnection(
ds.getJdbcUrl(),
ds.getUserName(),
ds.getPassword()
);
conn.close();
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(true));
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "oracle连通性异常:"+e.getMessage());
} finally {
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
List<String> list = DbInfoUtil.getTableNameList(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
@SuppressWarnings({ "rawtypes" })
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName) {
List list = DbInfoUtil.getTableInfo(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword(), tableName);
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
@SuppressWarnings("unchecked")
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName) {
Map<String, Object> map = DbInfoUtil.getTableLimitData(ds.getDriverClassName(), ds.getJdbcUrl(), ds.getUserName(), ds.getPassword(), tableName);
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(map));
}
}
package com.jz.agent.client.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.agent.ResultCode;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DmpPostgreSqlAgentClientService implements DmpDsAgentClient {
private Gson gson = new Gson();
private static Logger logger = LoggerFactory.getLogger(DmpPostgreSqlAgentClientService.class);
@SuppressWarnings("unused")
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
Connection conn = null;
try {
Class.forName(ds.getDriverClassName());
conn = DriverManager.getConnection(ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(true));
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "POSTGRESQL连通性异常:"+e.getMessage());
} finally {
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
String sql = "select relname as tabname from pg_class where relkind='r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<String> list = new ArrayList<String>();
try {
Class.forName("org.postgresql.Driver");// "org.postgresql.Driver"
conn = DriverManager.getConnection(ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
// "jdbc:postgresql://192.168.204.102:5432/test", "postgres", "postgres"
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
list.add(rs.getString(1).toLowerCase());
}
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "POSTGRESQL获取表列表异常:"+e.getMessage());
} finally {
if(rs != null)
try {
rs.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName) {
String sql = "SELECT col_description(a.attrelid,a.attnum) as comment,format_type(a.atttypid,a.atttypmod) as type,a.attname as name, a.attnotnull as notnull FROM pg_class as c,pg_attribute as a where c.relname = '"
+ tableName + "' and a.attrelid = c.oid and a.attnum>0";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
try {
Class.forName(ds.getDriverClassName());// "org.postgresql.Driver"
conn = DriverManager.getConnection(ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
// "jdbc:postgresql://192.168.204.102:5432/test", "postgres", "postgres"
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Map<String, Object> row = new HashMap<String, Object>();
String type = rs.getString(2).toUpperCase();
if(StringUtils.isNotBlank(type) && type.indexOf("(") > -1) {
int idx1 = type.indexOf("(");
int idx2 = type.indexOf(")");
String t = type.substring(0, idx1);
String ts = type.substring(idx1+1, idx2);
if(ts.indexOf(",") > -1) {
String[] strs = ts.split(",");
row.put("size", new Integer(strs[0]));
row.put("scale", new Integer(strs[1]));
} else {
row.put("size", new Integer(ts));
row.put("scale", 0);
}
type = t;
}
row.put("name", rs.getString(3).toLowerCase());
row.put("type", type);
row.put("comment", rs.getString(1));
list.add(row);
}
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "POSTGRESQL获取表字段信息异常:"+e.getMessage());
} finally {
if(rs != null)
try {
rs.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(list));
}
@SuppressWarnings("resource")
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName) {
Map<String, Object> map = new HashMap<String, Object>();
String sql = "select * from " + tableName + " limit 5";
String sql1 = "SELECT col_description(a.attrelid,a.attnum) as comment,format_type(a.atttypid,a.atttypmod) as type,a.attname as name, a.attnotnull as notnull FROM pg_class as c,pg_attribute as a where c.relname = '"
+ tableName + "' and a.attrelid = c.oid and a.attnum>0";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(ds.getDriverClassName());// "org.postgresql.Driver"
conn = DriverManager.getConnection(ds.getJdbcUrl(), ds.getUserName(), ds.getPassword());
// "jdbc:postgresql://192.168.204.102:5432/test", "postgres", "postgres"
ps = conn.prepareStatement(sql1);
rs = ps.executeQuery();
List<String> headerList = new ArrayList<String>();
while (rs.next()) {
headerList.add(rs.getString(3).toLowerCase());
}
map.put("header", headerList);
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
List<List<Object>> resultList = new ArrayList<List<Object>>();
while (rs.next()) {
List<Object> row = new ArrayList<Object>();
for (int i = 1; i <= headerList.size(); i++) {
row.add(rs.getObject(i));
}
resultList.add(row);
}
map.put("result", resultList);
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "POSTGRESQL数据预览异常:"+e.getMessage());
} catch (Exception e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "POSTGRESQL数据预览异常:"+e.getMessage());
} finally {
if(rs != null)
try {
rs.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
if(ps != null)
try {
ps.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(map));
}
}
package com.jz.agent.client.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.agent.ResultCode;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DmpSqlServerAgentClientService implements DmpDsAgentClient {
private Gson gson = new Gson();
@Override
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
// 连接数据库
String jdbcUrl = ds.getJdbcUrl();
String userName = ds.getUserName();
String passwd = ds.getPassword();
Connection conn = null;
try {
conn = DriverManager.getConnection(jdbcUrl, userName, passwd);
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(true));
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "SQLSERVER连通性异常:" + e.getMessage());
} finally {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Override
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
// 连接数据库
String jdbcUrl = ds.getJdbcUrl();
String userName = ds.getUserName();
String passwd = ds.getPassword();
Connection conn = null;
List<String> tableNameList = new ArrayList<>();
try {
conn = DriverManager.getConnection(jdbcUrl, userName, passwd);
final DatabaseMetaData metaData = conn.getMetaData();
final ResultSet res = metaData.getTables(conn.getCatalog(), null, null, new String[]{"TABLE"});
while (res.next()) {
String schema = res.getString("TABLE_SCHEM");
tableNameList.add(schema+"."+res.getString("TABLE_NAME").toLowerCase());
// if ("dbo".equals(schema)) {
// tableNameList.add(res.getString("TABLE_NAME").toLowerCase());
// }
}
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "SQLSERVER连通性异常:" + e.getMessage());
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(tableNameList));
}
@Override
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName) {
List<Map<String, Object>> columnMapList = new ArrayList<>();
// 连接数据库
String jdbcUrl = ds.getJdbcUrl();
String userName = ds.getUserName();
String passwd = ds.getPassword();
Connection conn = null;
System.out.println("tableName=======:"+tableName);
String[] stableName = tableName.split("\\.");
System.out.println(stableName[0]);
String s = stableName[stableName.length-1];
try {
conn = DriverManager.getConnection(jdbcUrl, userName, passwd);
final DatabaseMetaData metaData = conn.getMetaData();
ResultSet columns = metaData.getColumns(null, "%", s, "%");
Map<String, Object> columnMap = null;
while (columns.next()) {
columnMap = new HashMap<>();
String columnName = columns.getString("COLUMN_NAME");
String typeName = columns.getString("TYPE_NAME");
columnMap.put("name", columnName.toLowerCase());
columnMap.put("type", typeName.toUpperCase());
/*columnMap.put("size", size);
columnMap.put("scale", dicimalDigits);
columnMap.put("comment", remarks);*/
columnMapList.add(columnMap);
}
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "SQLSERVER连通性异常:" + e.getMessage());
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(columnMapList));
}
@Override
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName) {
Map<String, Object> map = new HashMap<String, Object>();
// 连接数据库
String jdbcUrl = ds.getJdbcUrl();
String userName = ds.getUserName();
String passwd = ds.getPassword();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String[] stableName = tableName.split("\\.");
tableName = stableName[stableName.length-1];
try {
conn = DriverManager.getConnection(jdbcUrl, userName, passwd);
final DatabaseMetaData metaData = conn.getMetaData();
ResultSet columns = metaData.getColumns(null, "%", tableName, "%");
List<String> headerList = new ArrayList<String>();
while (columns.next()) {
String columnName = columns.getString("COLUMN_NAME");
headerList.add(columnName);
}
String queryDataSql = "select top 5 * from " + tableName;
// 建立Statement对象
stmt = conn.createStatement();
rs = stmt.executeQuery(queryDataSql);
List<List<Object>> resultList = new ArrayList<List<Object>>();
while (rs.next()) {
List<Object> row = new ArrayList<Object>();
for (int i = 1; i <= headerList.size(); i++) {
row.add(rs.getObject(i));
}
resultList.add(row);
}
map.put("result", resultList);
} catch (SQLException e) {
e.printStackTrace();
return new DmpAgentResult(ResultCode.EXCEPTION, "SQLSERVER连通性异常:" + e.getMessage());
} finally {
if (rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return new DmpAgentResult(ResultCode.SUCCESS, gson.toJson(map));
}
}
package com.jz.agent.config;
public class DmpDsAgentConstants {
public static String HADOOP_CONF_FILE_PATH = "G:\\ws_workstation\\dmp-new";
public static String HADOOP_DEFAULT_CONF_FILE_PATH = "";
public static String AGENT_HADOOP_USER_NAME = "hdfs";
public static Boolean IS_ENABLED_KERBEROS = false;
public static String SUBMIT_SYNCING_CONFIG_HDFS_PATH = "/jz/etlprocesses/guide_xml/";
public static String KERBEROS_CONF_FILE_PATH = "G:\\ws_workstation\\dmp-new\\jz-dmp-new\\jz-dmp-web-agent\\src\\main\\resources\\keytab";
public static String KERBEROS_CONFIGURATION_NAME = "krb5.conf";
public static String KERBEROS_KEYTAB_NAME_HDFS = "hdfs.keytab";
public static String KERBEROS_KEYTAB_NAME_HIVE = "hive.keytab";
public static String KERBEROS_KEYTAB_NAME_KUDU = "kudu.keytab";
public static String KERBEROS_KEYTAB_NAME_IMPALA = "impala.keytab";
public static String DMP_AGENT_PUBLIC_KEY = "/gExN7y/C++iJUXj2RZtaFLfsNrTmAcl";
public static String KERBEROS_KEYTAB_ACCOUNT_HDFS = "hdfs/master@HADOOP.COM";
public static String KERBEROS_KEYTAB_ACCOUNT_HIVE = "hive/master@HADOOP.COM";
public static String KERBEROS_KEYTAB_ACCOUNT_KUDU = "kudu/master@HADOOP.COM";
public static String KERBEROS_KEYTAB_ACCOUNT_IMPALA = "HADOOP.COM";
public static String KERBEROS_CONFIGURATION_PATH = KERBEROS_CONF_FILE_PATH + KERBEROS_CONFIGURATION_NAME;
public static String KERBEROS_KEYTAB_PATH_HDFS = KERBEROS_CONF_FILE_PATH + KERBEROS_KEYTAB_NAME_HDFS;
public static String KERBEROS_KEYTAB_PATH_HIVE = KERBEROS_CONF_FILE_PATH + KERBEROS_KEYTAB_NAME_HIVE;
public static String KERBEROS_KEYTAB_PATH_KUDU = KERBEROS_CONF_FILE_PATH + KERBEROS_KEYTAB_NAME_KUDU;
public static String KERBEROS_KEYTAB_PATH_IMPALA = KERBEROS_CONF_FILE_PATH + KERBEROS_KEYTAB_NAME_IMPALA;
}
package com.jz.agent.service;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
public interface DmpDsAgentService {
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds);
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds);
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String tableName);
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String tableName);
public DmpAgentResult submitSycingXmlConfig(DmpAgentDatasourceInfo ds, String xml);
}
\ No newline at end of file
package com.jz.agent.service;
import com.google.gson.Gson;
import com.jz.agent.client.DmpDsAgentClient;
import com.jz.agent.client.DmpDsAgentClientBuilder;
import com.jz.agent.client.service.DmpHdfsAgentClientService;
import com.jz.dmp.agent.DmpAgentResult;
import com.jz.dmp.modules.model.DmpAgentDatasourceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class DmpDsAgentServiceImp implements DmpDsAgentService {
private Gson gson = new Gson();
private static Logger logger = LoggerFactory.getLogger(DmpDsAgentServiceImp.class);
public DmpAgentResult testConnect(DmpAgentDatasourceInfo ds) {
logger.info("testConnect << REQUEST[DmpAgentDatasourceInfo=" + gson.toJson(ds) + "]");
DmpDsAgentClient client = DmpDsAgentClientBuilder.build(ds);
DmpAgentResult dar = client.testConnect(ds);
logger.info("testConnect >> RESPONSE[" + gson.toJson(dar) + "]");
return dar;
}
public DmpAgentResult getTableNameList(DmpAgentDatasourceInfo ds) {
logger.info("getTableNameList << REQUEST[DmpAgentDatasourceInfo=" + gson.toJson(ds) + "]");
DmpDsAgentClient client = DmpDsAgentClientBuilder.build(ds);
DmpAgentResult dar = client.getTableNameList(ds);
logger.info("getTableNameList >> RESPONSE[" + gson.toJson(dar) + "]");
return dar;
}
public DmpAgentResult getTableColumnList(DmpAgentDatasourceInfo ds, String table) {
logger.info("getTableColumnList << REQUEST[DmpAgentDatasourceInfo=" + gson.toJson(ds) + ", table=" + table + "]");
DmpDsAgentClient client = DmpDsAgentClientBuilder.build(ds);
DmpAgentResult dar = client.getTableColumnList(ds, table);
logger.info("getTableColumnList >> RESPONSE[" + gson.toJson(dar) + "]");
return dar;
}
public DmpAgentResult previewData(DmpAgentDatasourceInfo ds, String table) {
logger.info("previewData << REQUEST[DmpAgentDatasourceInfo=" + gson.toJson(ds) + ", table=" + table + "]");
DmpDsAgentClient client = DmpDsAgentClientBuilder.build(ds);
DmpAgentResult dar = client.previewData(ds, table);
logger.info("previewData >> RESPONSE[" + gson.toJson(dar) + "]");
return dar;
}
public DmpAgentResult submitSycingXmlConfig(DmpAgentDatasourceInfo ds, String xml) {
logger.info("submitSycingXmlConfig << REQUEST[DmpAgentDatasourceInfo=" + gson.toJson(ds) + ", table=" + xml + "]");
DmpHdfsAgentClientService client = new DmpHdfsAgentClientService();
DmpAgentResult dar = client.submitSycingConfig(ds, xml);
logger.info("submitSycingXmlConfig >> RESPONSE[" + gson.toJson(dar) + "]");
return dar;
}
}
package com.jz.agent.utils;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DbInfoUtil {
@SuppressWarnings({ "unused", "rawtypes" })
public static void main(String[] args) {
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List getTableInfo(String driver, String url, String user, String pwd, String table) {
List result = new ArrayList();
Connection conn = null;
DatabaseMetaData dbmd = null;
try {
conn = JDBCUtils.getConnections(driver, url, user, pwd);
dbmd = conn.getMetaData();
ResultSet resultSet = dbmd.getTables(null, "%", table, new String[] { "TABLE" });
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
//System.out.println(tableName);
if (tableName.equals(table)) {
ResultSet rs = conn.getMetaData().getColumns(null, getSchema(conn), tableName.toUpperCase(), "%");
while (rs.next()) {
Map map = new HashMap();
String colName = rs.getString("COLUMN_NAME");
String dbType = rs.getString("TYPE_NAME");
String remarks = rs.getString("REMARKS");
Integer columnSize = rs.getInt("COLUMN_SIZE");
Integer dicimalDigits = rs.getInt("DECIMAL_DIGITS");
/*map.put("code", colName);
if (remarks == null || remarks.equals("")) {
remarks = colName;
}
map.put("name", remarks);
map.put("dbType", dbType);
map.put("valueType", changeDbType(dbType));
*/
map.put("name", colName.toLowerCase());
map.put("type", dbType);
map.put("size", columnSize);
map.put("scale", dicimalDigits);
map.put("comment", remarks);
result.add(map);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
@SuppressWarnings("unused")
private static String changeDbType(String dbType) {
dbType = dbType.toUpperCase();
switch (dbType) {
case "VARCHAR":
case "VARCHAR2":
case "CHAR":
return "1";
case "NUMBER":
case "DECIMAL":
return "4";
case "INT":
case "SMALLINT":
case "INTEGER":
return "2";
case "BIGINT":
return "6";
case "DATETIME":
case "TIMESTAMP":
case "DATE":
return "7";
default:
return "1";
}
}
private static String getSchema(Connection conn) throws Exception {
String schema;
schema = conn.getMetaData().getUserName();
if ((schema == null) || (schema.length() == 0)) {
throw new Exception("ORACLE数据库模式不允许为空");
}
return schema.toUpperCase().toString();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<String> getTableNameList(String driver, String url, String user, String pwd) {
List result = new ArrayList();
Connection conn = null;
DatabaseMetaData dbmd = null;
try {
conn = JDBCUtils.getConnections(driver, url, user, pwd);
String catalog = conn.getCatalog();
dbmd = conn.getMetaData();
ResultSet tablesResultSet = dbmd.getTables(catalog, null, null, new String[] { "TABLE" });
while (tablesResultSet.next()) {
String tableName = tablesResultSet.getString("TABLE_NAME");
result.add(tableName.toLowerCase());
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map getTableLimitData(String driver, String url, String user, String pwd, String table) {
Map map = new HashMap<String, Object>();
Connection conn = null;
DatabaseMetaData dbmd = null;
try {
conn = JDBCUtils.getConnections(driver, url, user, pwd);
String sql = "select * from " + table + " limit 5";
dbmd = conn.getMetaData();
ResultSet resultSet = dbmd.getTables(null, "%", table, new String[] { "TABLE" });
List<String> headerList = new ArrayList<String>();
List<String> typeList = new ArrayList<String>();
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
if (tableName.equals(table)) {
ResultSet rs = conn.getMetaData().getColumns(null, getSchema(conn), tableName.toUpperCase(), "%");
while (rs.next()) {
String colName = rs.getString("COLUMN_NAME");
headerList.add(colName);
String dbType = rs.getString("TYPE_NAME");
typeList.add(dbType);
}
break;
}
}
map.put("header", headerList);
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List<List<Object>> resultList = new ArrayList<List<Object>>();
while (rs.next()) {
List<Object> row = new ArrayList<Object>();
for(int i = 1; i <= headerList.size(); i++) {
row.add(rs.getObject(i));
}
resultList.add(row);
}
map.put("result", resultList);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return map;
}
}
package com.jz.agent.utils;
import com.jz.agent.config.DmpDsAgentConstants;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;