Skip to content

Commit

Permalink
[test_5.0.x_batch][common-loader][支持拆分 schema、tableName 方法]
Browse files Browse the repository at this point in the history
  • Loading branch information
wangchuanpoxiao committed May 11, 2022
1 parent 9c9c0ac commit 69c378e
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.dtstack.dtcenter.loader.dto.Database;
import com.dtstack.dtcenter.loader.dto.SqlQueryDTO;
import com.dtstack.dtcenter.loader.dto.Table;
import com.dtstack.dtcenter.loader.dto.TableInfo;
import com.dtstack.dtcenter.loader.dto.source.ISourceDTO;
import com.dtstack.dtcenter.loader.exception.DtLoaderException;

Expand Down Expand Up @@ -175,4 +176,15 @@ public List<String> listFileNames(ISourceDTO sourceDTO, String path, Boolean inc
public Database getDatabase(ISourceDTO sourceDTO, String dbName) {
throw new DtLoaderException(ErrorCode.NOT_SUPPORT.getDesc());
}


@Override
public Integer executeUpdate(ISourceDTO source, SqlQueryDTO queryDTO) {
throw new DtLoaderException(ErrorCode.NOT_SUPPORT.getDesc());
}

@Override
public TableInfo getTableInfo(ISourceDTO sourceDTO, String tableName) {
throw new DtLoaderException(ErrorCode.NOT_SUPPORT.getDesc());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

package com.dtstack.dtcenter.common.loader.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class StringUtil {
private static final char[] DIGITS_LOWER = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

Expand All @@ -37,4 +46,128 @@ public static String encodeHex(byte[] data, char[] toDigits) {

return String.valueOf(out);
}

/**
* 解析出对应符号对内的内容
*
* @param text 需要解析的字符串
* @param quotationText 符号
* @return 解析结果
*/
public static List<String> splitWithQuotation(String text, String quotationText) {
Pattern quotationPattern = Pattern.compile(quotationText + "(.*?)" + quotationText);
Matcher quotationMatch = quotationPattern.matcher(text);

ArrayList<String> results = new ArrayList<>();
while (quotationMatch.find()) {
results.add(quotationMatch.group().trim().replace(quotationText, ""));
}
return results;
}

/**
* 解析出对应符号对内的内容
*
* @param text 需要解析的字符串
* @param signPair 左右符号
* @return 解析结果
*/
public static String splitWithPair(String text, Pair<Character, Character> signPair) {
if (StringUtils.isEmpty(text) || text.length() < 2) {
return text;
}
if (text.startsWith(String.valueOf(signPair.getLeft())) && text.endsWith(String.valueOf(signPair.getRight()))) {
return text.substring(1, text.length() -1);
}
return text;
}

/**
* 按指定切割符号进行切割字符串并忽略指定括号内的切割符号, 返回值不带指定括号
*
* @param str 字符串
* @param delimiter 切割符号
* @param pair 左右括号
* @return 切割后的字符串集合
*/
public static List<String> splitWithOutQuota(String str, char delimiter, Pair<Character, Character> pair) {
return splitIgnoreQuota(str, delimiter, pair).stream().map(sp -> splitWithPair(sp, pair)).collect(Collectors.toList());
}

/**
* 按指定切割符号进行切割字符串并忽略指定括号内的切割符号
*
* @param str 字符串
* @param delimiter 切割符号
* @param pair 左右括号
* @return 切割后的字符串集合
*/
public static List<String> splitIgnoreQuota(String str, char delimiter, Pair<Character, Character> pair) {
List<String> resultList = new ArrayList<>();
char[] chars = str.toCharArray();
StringBuilder b = new StringBuilder();

if (pair.getLeft().equals(pair.getRight())) {
boolean inEqualQuotes = false;
for (char c : chars) {
if (c == delimiter) {
if (inEqualQuotes) {
b.append(c);
} else {
resultList.add(b.toString());
b = new StringBuilder();
}
} else if (c == pair.getLeft()) {
inEqualQuotes = !inEqualQuotes;
b.append(c);
} else {
b.append(c);
}
}
} else {
int bracketLeftNum = 0;
for (char c : chars) {
if (c == delimiter) {
if (bracketLeftNum > 0) {
b.append(c);
} else {
resultList.add(b.toString());
b = new StringBuilder();
}
} else if (c == pair.getLeft()) {
bracketLeftNum++;
b.append(c);
} else if (c == pair.getRight()) {
bracketLeftNum--;
b.append(c);
} else {
b.append(c);
}
}
}
resultList.add(b.toString());
return resultList;
}

public static void main(String[] args) {
System.out.println(splitWithOutQuota("[ab].[b.c]", '.', Pair.of('[', ']')));
System.out.println("------------------------------------");
System.out.println(splitWithOutQuota("ab.[b.c]", '.', Pair.of('[', ']')));
System.out.println("------------------------------------");
System.out.println(splitWithOutQuota("[ab].c", '.', Pair.of('[', ']')));
System.out.println("------------------------------------");
System.out.println(splitWithOutQuota("b.c", '.', Pair.of('[', ']')));

System.out.println("++++++++++++++++++++++++++++++++++++");

System.out.println(splitWithOutQuota("\"ab\".\"b.c\"", '.', Pair.of('\"', '\"')));
System.out.println("------------------------------------");
System.out.println(splitWithOutQuota("ab.\"b.c\"", '.', Pair.of('\"', '\"')));
System.out.println("------------------------------------");
System.out.println(splitWithOutQuota("\"ab\".c", '.', Pair.of('\"', '\"')));
System.out.println("------------------------------------");
System.out.println(splitWithOutQuota("b.c", '.', Pair.of('\"', '\"')));
System.out.println("------------------------------------");
System.out.println(splitWithOutQuota("\"b.c\"", '.', Pair.of('\"', '\"')));
}
}
10 changes: 10 additions & 0 deletions core/src/main/java/com/dtstack/dtcenter/loader/client/IClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.dtstack.dtcenter.loader.dto.Database;
import com.dtstack.dtcenter.loader.dto.SqlQueryDTO;
import com.dtstack.dtcenter.loader.dto.Table;
import com.dtstack.dtcenter.loader.dto.TableInfo;
import com.dtstack.dtcenter.loader.dto.source.ISourceDTO;

import java.sql.Connection;
Expand Down Expand Up @@ -319,4 +320,13 @@ public interface IClient<T> {
* @return 数据库详细信息
*/
Database getDatabase(ISourceDTO sourceDTO, String dbName);

/**
* 获取 table 信息
*
* @param sourceDTO 数据源信息
* @param tableName 表名
* @return table 信息
*/
TableInfo getTableInfo(ISourceDTO sourceDTO, String tableName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.dtstack.dtcenter.loader.dto.Database;
import com.dtstack.dtcenter.loader.dto.SqlQueryDTO;
import com.dtstack.dtcenter.loader.dto.Table;
import com.dtstack.dtcenter.loader.dto.TableInfo;
import com.dtstack.dtcenter.loader.dto.source.ISourceDTO;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -214,4 +215,10 @@ public Database getDatabase(ISourceDTO sourceDTO, String dbName) {
return ClassLoaderCallBackMethod.callbackAndReset(() -> targetClient.getDatabase(sourceDTO, dbName),
targetClient.getClass().getClassLoader());
}

@Override
public TableInfo getTableInfo(ISourceDTO sourceDTO, String tableName) {
return ClassLoaderCallBackMethod.callbackAndReset(() -> targetClient.getTableInfo(sourceDTO, tableName),
targetClient.getClass().getClassLoader());
}
}
35 changes: 35 additions & 0 deletions core/src/main/java/com/dtstack/dtcenter/loader/dto/TableInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.dtstack.dtcenter.loader.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* table Info
*
* @author :wangchuan
* date:Created in 下午12:25 2022/3/21
* company: www.dtstack.com
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TableInfo {

/**
* db 信息
*/
private String database;

/**
* schema 信息
*/
private String schema;

/**
* 表名
*/
private String tableName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.dtstack.dtcenter.loader.dto.source.ISourceDTO;
import com.dtstack.dtcenter.loader.source.DataSourceType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

/**
* @author :qianyi
Expand Down Expand Up @@ -60,4 +61,9 @@ protected String transferSchemaAndTableName(String schema, String tableName) {
}
return String.format("%s.%s", schema, tableName);
}

@Override
protected Pair<Character, Character> getSpecialSign() {
return Pair.of('`', '`');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public class GreenplumClient extends AbsRdbmsClient {
// 获取正在使用数据库
private static final String CURRENT_DB = "select current_database()";

// 获取正在使用 schema
private static final String CURRENT_SCHEMA = "select current_schema()";

// 获取当前版本号
private static final String SHOW_VERSION = "select version()";

Expand Down Expand Up @@ -244,6 +247,11 @@ protected String getCurrentDbSql() {
return CURRENT_DB;
}

@Override
protected String getCurrentSchemaSql() {
return CURRENT_SCHEMA;
}

@Override
protected String getVersionSql() {
return SHOW_VERSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ protected DataSourceType getSourceType() {
// metaStore 地址 principal 地址
private final static String META_STORE_KERBEROS_PRINCIPAL = "hive.metastore.kerberos.principal";

// 获取正在使用数据库
private static final String CURRENT_DB = "select current_database()";

@Override
public List<String> getTableList(ISourceDTO sourceDTO, SqlQueryDTO queryDTO) {
Integer clearStatus = beforeQuery(sourceDTO, queryDTO, false);
Expand Down Expand Up @@ -722,4 +725,10 @@ protected Object dealResult(Object result){
}
return result;
}

@Override
protected String getCurrentDbSql() {
return CURRENT_DB;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public class KingbaseClient extends AbsRdbmsClient {
// 获取正在使用数据库
private static final String CURRENT_DB = "select current_database()";

// 获取正在使用 schema
private static final String CURRENT_SCHEMA = "select current_schema()";

private static final String DONT_EXIST = "doesn't exist";

// 根据schema选表表名模糊查询
Expand Down Expand Up @@ -257,6 +260,11 @@ protected String getCurrentDbSql() {
return CURRENT_DB;
}

@Override
protected String getCurrentSchemaSql() {
return CURRENT_SCHEMA;
}

@Override
protected String getTableBySchemaSql(ISourceDTO sourceDTO, SqlQueryDTO queryDTO) {
RdbmsSourceDTO rdbmsSourceDTO = (RdbmsSourceDTO) sourceDTO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class LibraClient extends AbsRdbmsClient {
// 获取正在使用数据库
private static final String CURRENT_DB = "select current_database()";

// 获取正在使用 schema
private static final String CURRENT_SCHEMA = "select current_schema()";

// 获取所有schema
private static final String DATABASE_QUERY = "select nspname from pg_namespace";

Expand Down Expand Up @@ -123,6 +126,11 @@ protected String getCurrentDbSql() {
return CURRENT_DB;
}

@Override
protected String getCurrentSchemaSql() {
return CURRENT_SCHEMA;
}

@Override
protected String getCreateDatabaseSql(String dbName, String comment) {
return String.format(CREATE_SCHEMA_SQL_TMPL, dbName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
Expand Down Expand Up @@ -287,4 +288,9 @@ protected String transferSchemaAndTableName(String schema, String tableName) {
protected String getVersionSql() {
return SHOW_VERSION;
}

@Override
protected Pair<Character, Character> getSpecialSign() {
return Pair.of('`', '`');
}
}
Loading

0 comments on commit 69c378e

Please sign in to comment.