Unverified Commit 2b71dfec authored by gaoyan's avatar gaoyan Committed by GitHub

[Optimize ]Optimize Paimon log printing that can cause ambiguity (#2986)

parent 1ca1dfef
...@@ -74,28 +74,42 @@ import cn.hutool.core.util.ReflectUtil; ...@@ -74,28 +74,42 @@ import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil; import cn.hutool.core.util.URLUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
@Data
public class PaimonUtil { public class PaimonUtil {
private static final Cache<Class<?>, Schema> SCHEMA_CACHE = CacheUtil.newLRUCache(100);
private static final CatalogContext CONTEXT =
CatalogContext.create(new Path(URLUtil.toURI(URLUtil.url(PathConstant.TMP_PATH + "paimon"))));
private static final Catalog CATALOG = CatalogFactory.createCatalog(CONTEXT);
static { private static PaimonUtil instance;
private final Cache<Class<?>, Schema> schemaCache;
private final CatalogContext context;
private final Catalog catalog;
public PaimonUtil() {
schemaCache = CacheUtil.newLRUCache(100);
context = CatalogContext.create(new Path(URLUtil.toURI(URLUtil.url(PathConstant.TMP_PATH + "paimon"))));
catalog = CatalogFactory.createCatalog(context);
try { try {
CATALOG.createDatabase(DINKY_DB, true); catalog.createDatabase(DINKY_DB, true);
} catch (Catalog.DatabaseAlreadyExistException e) { } catch (Catalog.DatabaseAlreadyExistException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public static synchronized PaimonUtil getInstance() {
if (instance == null) {
instance = new PaimonUtil();
}
return instance;
}
public static void dropTable(String table) { public static void dropTable(String table) {
Identifier identifier = Identifier.create(DINKY_DB, table); Identifier identifier = Identifier.create(DINKY_DB, table);
if (CATALOG.tableExists(identifier)) { if (getInstance().getCatalog().tableExists(identifier)) {
try { try {
CATALOG.dropTable(identifier, true); getInstance().getCatalog().dropTable(identifier, true);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -122,6 +136,8 @@ public class PaimonUtil { ...@@ -122,6 +136,8 @@ public class PaimonUtil {
String fieldName = StrUtil.toCamelCase(dataField.name()); String fieldName = StrUtil.toCamelCase(dataField.name());
Object fieldValue = ReflectUtil.getFieldValue(t, fieldName); Object fieldValue = ReflectUtil.getFieldValue(t, fieldName);
try { try {
// TODO BinaryWriter.write已被废弃,后续可以考虑改成这种方式
// BinaryWriter.createValueSetter(type).setValue(writer, i, fieldValue);
if (type.getTypeRoot() == DataTypeRoot.VARCHAR) { if (type.getTypeRoot() == DataTypeRoot.VARCHAR) {
BinaryWriter.write( BinaryWriter.write(
writer, i, BinaryString.fromString(JSONUtil.toJsonStr(fieldValue)), type, null); writer, i, BinaryString.fromString(JSONUtil.toJsonStr(fieldValue)), type, null);
...@@ -169,10 +185,10 @@ public class PaimonUtil { ...@@ -169,10 +185,10 @@ public class PaimonUtil {
ReadBuilder readBuilder; ReadBuilder readBuilder;
try { try {
if (!CATALOG.tableExists(identifier)) { if (!getInstance().getCatalog().tableExists(identifier)) {
return dataList; return dataList;
} }
readBuilder = CATALOG.getTable(identifier).newReadBuilder(); readBuilder = getInstance().getCatalog().getTable(identifier).newReadBuilder();
if (filter != null) { if (filter != null) {
List<Predicate> predicates = filter.apply(builder); List<Predicate> predicates = filter.apply(builder);
readBuilder.withFilter(predicates); readBuilder.withFilter(predicates);
...@@ -218,18 +234,18 @@ public class PaimonUtil { ...@@ -218,18 +234,18 @@ public class PaimonUtil {
public static Table createOrGetTable(String tableName, Class<?> clazz) { public static Table createOrGetTable(String tableName, Class<?> clazz) {
try { try {
Identifier identifier = Identifier.create(DINKY_DB, tableName); Identifier identifier = Identifier.create(DINKY_DB, tableName);
if (CATALOG.tableExists(identifier)) { if (getInstance().getCatalog().tableExists(identifier)) {
return CATALOG.getTable(identifier); return getInstance().getCatalog().getTable(identifier);
} }
CATALOG.createTable(identifier, getSchemaByClass(clazz), false); getInstance().getCatalog().createTable(identifier, getSchemaByClass(clazz), false);
return CATALOG.getTable(identifier); return getInstance().getCatalog().getTable(identifier);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public static Schema getSchemaByClass(Class<?> clazz) { public static Schema getSchemaByClass(Class<?> clazz) {
return SCHEMA_CACHE.get(clazz, () -> { return getInstance().getSchemaCache().get(clazz, () -> {
List<String> primaryKeys = new ArrayList<>(); List<String> primaryKeys = new ArrayList<>();
List<String> partitionKeys = new ArrayList<>(); List<String> partitionKeys = new ArrayList<>();
Schema.Builder builder = Schema.newBuilder(); Schema.Builder builder = Schema.newBuilder();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment