增加hive和impala数据源支持

This commit is contained in:
暮光:城中城
2021-05-11 23:14:44 +08:00
parent 2fab0d6a80
commit 09d6ccca57
13 changed files with 210 additions and 14 deletions

View File

@@ -72,7 +72,37 @@
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.1</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-1.2-api</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-slf4j-impl</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-web</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.dozermapper</groupId>
<artifactId>dozer-core</artifactId>

View File

@@ -15,6 +15,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
@@ -92,5 +96,4 @@ public class DbTransferDataController {
List<String> selectNames = SqlParseUtil.getSelectNames(sql);
return DocDbResponseJson.ok(selectNames);
}
}

View File

@@ -10,12 +10,14 @@ public class TableDdlVo {
private String sqlserver;
private String oracle;
private String postgresql;
private String hive;
public String getTableDDLByType() {
if (Objects.equals(current, DatabaseFactoryBean.DatabaseProduct.MYSQL.name().toLowerCase())) return mysql;
if (Objects.equals(current, DatabaseFactoryBean.DatabaseProduct.SQLSERVER.name().toLowerCase())) return sqlserver;
if (Objects.equals(current, DatabaseFactoryBean.DatabaseProduct.ORACLE.name().toLowerCase())) return oracle;
if (Objects.equals(current, DatabaseFactoryBean.DatabaseProduct.POSTGRESQL.name().toLowerCase())) return postgresql;
if (Objects.equals(current, DatabaseFactoryBean.DatabaseProduct.HIVE.name().toLowerCase())) return hive;
return null;
}

View File

@@ -30,6 +30,15 @@ public class DatasourceUtil {
}
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.MYSQL);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/mysql/*.xml");
} else if (dbUrl.contains("hive")) {
// jdbc:hive2://127.0.0.1:21050/ads_data;auth=noSasl
String[] urlParamArr = dbUrl.split(";");
String[] urlDbNameArr = urlParamArr[0].split("/");
if (urlDbNameArr.length >= 2) {
databaseFactoryBean.setDbName(urlDbNameArr[urlDbNameArr.length - 1]);
}
databaseFactoryBean.setDatabaseProduct(DatabaseFactoryBean.DatabaseProduct.HIVE);
resources = resolver.getResources("classpath:com/zyplayer/doc/db/framework/db/mapper/hive/*.xml");
} else if (dbUrl.contains("postgresql")) {
// jdbc:mysql://192.168.0.1:3306/user_info?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
String[] urlParamArr = dbUrl.split("\\?");

View File

@@ -18,6 +18,11 @@ public class DatabaseFactoryBean {
private String groupName;
private DatabaseProduct databaseProduct;
public static enum DatabaseProduct {
MYSQL, SQLSERVER, ORACLE, POSTGRESQL, HIVE
}
public Long getId() {
return id;
}
@@ -42,10 +47,6 @@ public class DatabaseFactoryBean {
this.groupName = groupName;
}
public static enum DatabaseProduct {
MYSQL, SQLSERVER, ORACLE, POSTGRESQL
}
public DruidDataSource getDataSource() {
return dataSource;
}

View File

@@ -103,15 +103,17 @@ public class SqlExecutor {
preparedStatement.setObject(i + 1, paramDataList.get(i));
}
}
// 最大限制1分钟
preparedStatement.setQueryTimeout(60);
// 限制下最大数量
if (executeParam.getMaxRows() != null) {
preparedStatement.setMaxRows(executeParam.getMaxRows());
}
if (ExecuteType.SELECT.equals(executeParam.getExecuteType())) {
preparedStatement.executeQuery();
} else {
preparedStatement.execute();
}
// 限制下最大数量
if (executeParam.getMaxRows() != null) {
preparedStatement.setMaxRows(executeParam.getMaxRows());
}
// 查询的结果集
ResultSet resultSet = preparedStatement.getResultSet();
List<Map<String, Object>> resultList = new LinkedList<>();

View File

@@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zyplayer.doc.db.framework.db.mapper.base.BaseMapper">
<resultMap id="TableColumnDescDtoMap" type="com.zyplayer.doc.db.framework.db.dto.TableColumnDescDto" >
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="col_name" property="name" jdbcType="VARCHAR" />
<result column="data_type" property="type" jdbcType="VARCHAR" />
<result column="comment" property="description" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="QueryTableColumnDescDtoMap" type="com.zyplayer.doc.db.framework.db.dto.QueryTableColumnDescDto" >
<result column="TABLE_NAME" property="tableName" jdbcType="VARCHAR" />
<result column="COLUMN_NAME" property="columnName" jdbcType="VARCHAR" />
<result column="DESCRIPTION" property="description" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="TableStatusDtoMap" type="com.zyplayer.doc.db.controller.vo.TableStatusVo">
<result column="Name" property="name"/>
<result column="Engine" property="engine"/>
<result column="Version" property="version"/>
<result column="Row_format" property="rowFormat"/>
<result column="Rows" property="rows"/>
<result column="Avg_row_length" property="avgRowLength"/>
<result column="Data_length" property="dataLength"/>
<result column="Max_data_length" property="maxDataLength"/>
<result column="Index_length" property="indexLength"/>
<result column="Data_free" property="dataFree"/>
<result column="Auto_increment" property="autoIncrement"/>
<result column="Create_time" property="createTime"/>
<result column="Update_time" property="updateTime"/>
<result column="Check_time" property="checkTime"/>
<result column="Collation" property="collation"/>
<result column="Checksum" property="checksum"/>
<result column="Create_options" property="createOptions"/>
<result column="Comment" property="comment"/>
</resultMap>
<resultMap id="DatabasesDtoMap" type="com.zyplayer.doc.db.framework.db.dto.DatabaseInfoDto" >
<result column="name" property="dbName" jdbcType="VARCHAR" />
<result column="database_name" property="dbName" jdbcType="VARCHAR" />
</resultMap>
<select id="getDatabaseList" resultMap="DatabasesDtoMap">
show databases
</select>
<select id="getTableDdl" resultType="java.util.Map">
show create table `${dbName}`.${tableName}
</select>
<resultMap id="TablesDtoMap" type="com.zyplayer.doc.db.framework.db.dto.TableInfoDto" >
<result column="name" property="tableName" jdbcType="VARCHAR" />
<result column="tab_name" property="tableName" jdbcType="VARCHAR" />
</resultMap>
<select id="getTableList" resultMap="TablesDtoMap">
show tables in ${dbName}
</select>
<select id="getTableColumnList" resultMap="TableColumnDescDtoMap">
describe ${dbName}.${tableName}
</select>
<select id="getTableStatus" resultMap="TableStatusDtoMap">
select #{tableName} as Name
</select>
<select id="getTableColumnDescList" resultMap="TableColumnDescDtoMap">
select 1 from ${dbName}.${tableName} where 0 = 1
</select>
<select id="getTableAndColumnBySearch" resultMap="QueryTableColumnDescDtoMap">
select 1
</select>
<select id="getTableDescList" resultType="com.zyplayer.doc.db.framework.db.dto.TableDescDto">
select 1 from ${dbName}.${tableName} where 0 = 1
</select>
<insert id="updateTableDesc">
select 1 from ${dbName}.${tableName} where 0 = 1
</insert>
<insert id="updateTableColumnDesc">
select 1 from ${dbName}.${tableName} where 0 = 1
</insert>
</mapper>

View File

@@ -0,0 +1,15 @@
package com.zyplayer.doc.db.framework.db.mapper.hive;
import com.zyplayer.doc.db.framework.db.dto.ColumnInfoDto;
import org.apache.ibatis.annotations.Param;
/**
* mysql数据库的mapper持有对象
*
* @author 暮光:城中城
* @since 2018年8月8日
*/
public interface HiveMapper {
ColumnInfoDto getColumnInfo(@Param("dbName") String dbName, @Param("tableName") String tableName, @Param("columnName") String columnName);
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zyplayer.doc.db.framework.db.mapper.mysql.MysqlMapper">
<select id="getColumnInfo" resultType="com.zyplayer.doc.db.framework.db.dto.ColumnInfoDto">
select
IS_NULLABLE isNullable, COLUMN_TYPE columnType, CHARACTER_MAXIMUM_LENGTH maxLength,
COLUMN_DEFAULT columnDefault,EXTRA extra
from information_schema.columns t
where t.table_schema=#{dbName}
and t.table_name=#{tableName}
and t.column_name=#{columnName}
</select>
</mapper>

View File

@@ -0,0 +1,27 @@
package com.zyplayer.doc.db.service;
import com.zyplayer.doc.db.framework.db.bean.DatabaseFactoryBean;
import com.zyplayer.doc.db.framework.db.dto.TableInfoDto;
import com.zyplayer.doc.db.framework.db.mapper.base.BaseMapper;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HiveServiceImpl extends DbBaseService {
@Override
public DatabaseFactoryBean.DatabaseProduct getDatabaseProduct() {
return DatabaseFactoryBean.DatabaseProduct.HIVE;
}
@Override
public List<TableInfoDto> getTableList(Long sourceId, String dbName) {
BaseMapper baseMapper = this.getViewAuthBaseMapper(sourceId);
List<TableInfoDto> tableList = baseMapper.getTableList(dbName);
for (TableInfoDto tableInfoDto : tableList) {
tableInfoDto.setDbName(dbName);
}
return tableList;
}
}

View File

@@ -1 +1 @@
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=favicon-db.png><title>数据库文档管理</title><link href=css/app.b6b9fe42.css rel=preload as=style><link href=css/chunk-vendors.8924efc6.css rel=preload as=style><link href=js/app.a7f24e90.js rel=preload as=script><link href=js/chunk-vendors.d40f789d.js rel=preload as=script><link href=css/chunk-vendors.8924efc6.css rel=stylesheet><link href=css/app.b6b9fe42.css rel=stylesheet></head><body><noscript><strong>We're sorry but zyplayer-db-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=js/chunk-vendors.d40f789d.js></script><script src=js/app.a7f24e90.js></script></body></html>
<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><link rel=icon href=favicon-db.png><title>数据库文档管理</title><link href=css/app.b6b9fe42.css rel=preload as=style><link href=css/chunk-vendors.8924efc6.css rel=preload as=style><link href=js/app.412c8ca9.js rel=preload as=script><link href=js/chunk-vendors.d40f789d.js rel=preload as=script><link href=css/chunk-vendors.8924efc6.css rel=stylesheet><link href=css/app.b6b9fe42.css rel=stylesheet></head><body><noscript><strong>We're sorry but zyplayer-db-ui doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=js/chunk-vendors.d40f789d.js></script><script src=js/app.412c8ca9.js></script></body></html>

View File

@@ -39,7 +39,7 @@
:total="tableTotalCount">
</el-pagination>
<!--增加数据源弹窗-->
<el-dialog :inline="true" :title="newDatasource.id>0?'编辑数据源':'新增数据源'" :visible.sync="datasourceDialogVisible" width="760px">
<el-dialog :inline="true" :title="newDatasource.id>0?'编辑数据源':'新增数据源'" :visible.sync="datasourceDialogVisible" width="760px" :close-on-click-modal="false">
<el-form label-width="120px">
<el-form-item label="分组:">
<el-select v-model="newDatasource.groupName" placeholder="请选择或输入新的分组名字" style="width: 100%" filterable allow-create>
@@ -56,6 +56,7 @@
<el-option label="net.sourceforge.jtds.jdbc.Driver" value="net.sourceforge.jtds.jdbc.Driver"></el-option>
<el-option label="oracle.jdbc.driver.OracleDriver" value="oracle.jdbc.driver.OracleDriver"></el-option>
<el-option label="org.postgresql.Driver" value="org.postgresql.Driver"></el-option>
<el-option label="org.apache.hive.jdbc.HiveDriver" value="org.apache.hive.jdbc.HiveDriver"></el-option>
</el-select>
</el-form-item>
<el-form-item label="数据源URL">
@@ -243,8 +244,8 @@
}).catch(()=>{});
},
saveDatasource() {
this.datasourceDialogVisible = false;
datasourceApi.manageUpdateDatasource(this.newDatasource).then(() => {
this.datasourceDialogVisible = false;
this.$message.success("保存成功!");
this.$emit('loadDatasourceList');
this.getDatasourceList();
@@ -275,6 +276,8 @@
this.urlPlaceholder = "例jdbc:oracle:thin:@127.0.0.1:1521/user_info";
} else if (this.newDatasource.driverClassName == 'org.postgresql.Driver') {
this.urlPlaceholder = "例jdbc:postgresql://127.0.0.1:5432/user_info";
} else if (this.newDatasource.driverClassName == 'org.apache.hive.jdbc.HiveDriver') {
this.urlPlaceholder = "例jdbc:hive2://127.0.0.1:21050/user_info;auth=noSasl";
}
},
handleCurrentChange(to) {