support hive

This commit is contained in:
inrgihc
2022-02-02 14:12:17 +08:00
parent c187bef98c
commit 8559e2ebe9
41 changed files with 567 additions and 210 deletions

View File

@@ -5,7 +5,7 @@
<parent>
<groupId>com.gitee.dbswitch</groupId>
<artifactId>dbswitch-parent</artifactId>
<version>1.6.3</version>
<version>1.6.4</version>
</parent>
<artifactId>dbswitch-common</artifactId>

View File

@@ -0,0 +1,75 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.common.constant;
/**
* 常量定义
*
* @author tang
*/
public final class Const {
/**
* What's the file systems file separator on this operating system?
*/
public static final String FILE_SEPARATOR = System.getProperty("file.separator");
/**
* What's the path separator on this operating system?
*/
public static final String PATH_SEPARATOR = System.getProperty("path.separator");
/**
* CR: operating systems specific Carriage Return
*/
public static final String CR = System.getProperty("line.separator");
/**
* DOSCR: MS-DOS specific Carriage Return
*/
public static final String DOSCR = "\n\r";
/**
* An empty ("") String.
*/
public static final String EMPTY_STRING = "";
/**
* The Java runtime version
*/
public static final String JAVA_VERSION = System.getProperty("java.vm.version");
/**
* Create Table Statement Prefix String
*/
public static final String CREATE_TABLE = "CREATE TABLE ";
/**
* Drop Table Statement Prefix String
*/
public static final String DROP_TABLE = "DROP TABLE ";
/**
* Constant Keyword String
*/
public static final String IF_NOT_EXISTS = "IF NOT EXISTS ";
/**
* Constant Keyword String
*/
public static final String IF_EXISTS = "IF EXISTS ";
/**
* Constructor Function
*/
private Const() {
}
}

View File

@@ -0,0 +1,37 @@
// Copyright tang. All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.common.type;
/**
* 数据库表类型:视图表、物理表
*
* @author tang
*/
public enum DBTableType {
/**
* 物理表
*/
TABLE(0),
/**
* 视图表
*/
VIEW(1);
private int index;
DBTableType(int idx) {
this.index = idx;
}
public int getIndex() {
return index;
}
}

View File

@@ -68,7 +68,13 @@ public enum DatabaseTypeEnum {
/**
* Kingbase数据库类型
*/
KINGBASE(10);
KINGBASE(10),
/**
* HIVE数据库
*/
HIVE(11),
;
private int index;

View File

@@ -9,8 +9,8 @@
/////////////////////////////////////////////////////////////
package com.gitee.dbswitch.common.util;
import java.util.List;
import com.gitee.dbswitch.common.type.DatabaseTypeEnum;
import java.util.List;
/**
* 普通工具类
@@ -32,7 +32,8 @@ public final class CommonUtils {
*/
public static String getTableFullNameByDatabase(DatabaseTypeEnum dbType, String schema,
String table) {
if (dbType == DatabaseTypeEnum.MYSQL || dbType == DatabaseTypeEnum.MARIADB) {
if (dbType == DatabaseTypeEnum.MYSQL || dbType == DatabaseTypeEnum.MARIADB
|| dbType == DatabaseTypeEnum.HIVE) {
return String.format("`%s`.`%s`", schema, table);
} else if (dbType == DatabaseTypeEnum.SQLSERVER || dbType == DatabaseTypeEnum.SQLSERVER2000) {
return String.format("[%s].[%s]", schema, table);
@@ -74,7 +75,8 @@ public final class CommonUtils {
}
private static String quoteString(DatabaseTypeEnum dbType, String keyName) {
if (dbType == DatabaseTypeEnum.MYSQL || dbType == DatabaseTypeEnum.MARIADB) {
if (dbType == DatabaseTypeEnum.MYSQL || dbType == DatabaseTypeEnum.MARIADB
|| dbType == DatabaseTypeEnum.HIVE) {
return String.format("`%s`", keyName);
} else if (dbType == DatabaseTypeEnum.SQLSERVER || dbType == DatabaseTypeEnum.SQLSERVER2000) {
return String.format("[%s]", keyName);

View File

@@ -0,0 +1,52 @@
package com.gitee.dbswitch.common.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class HivePrepareUtils {
private final static String HIVE_SQL_1 = "set hive.resultset.use.unique.column.names=false";
private final static String HIVE_SQL_2 = "set hive.support.concurrency=true";
private final static String HIVE_SQL_3 = "set hive.txn.manager = org.apache.hadoop.hive.ql.lockmgr.DbTxnManager";
private HivePrepareUtils() {
}
public static void prepare(Connection connection, String schema, String table)
throws SQLException {
executeWithoutResultSet(connection, HIVE_SQL_1);
if (isTransactionalTable(connection, schema, table)) {
executeWithoutResultSet(connection, HIVE_SQL_2);
executeWithoutResultSet(connection, HIVE_SQL_3);
}
}
private static boolean isTransactionalTable(Connection connection, String schema, String table)
throws SQLException {
String fullTableName = String.format("`%s`.`%s`", schema, table);
String sql = String.format("DESCRIBE FORMATTED %s", fullTableName);
try (Statement st = connection.createStatement();
ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) {
String dataType = rs.getString("data_type");
String comment = rs.getString("comment");
if (dataType != null
&& comment != null
&& dataType.startsWith("transactional")
&& comment.startsWith("true")) {
return true;
}
}
return false;
}
}
private static boolean executeWithoutResultSet(Connection connection, String sql)
throws SQLException {
try (Statement st = connection.createStatement()) {
return st.execute(sql);
}
}
}