From 68c0abb8e3072a3528acb85cbf796d57e01aaad5 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Thu, 30 Nov 2023 12:29:28 +0800 Subject: [PATCH] =?UTF-8?q?feature(hudi-query):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E7=9A=84Schema=E6=9F=A5=E8=AF=A2=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/hudi/service/TableService.java | 11 ++--------- .../service/hudi/utils/HoodieUtils.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/service-hudi-query/src/main/java/com/lanyuanxiaoyao/service/hudi/service/TableService.java b/service-hudi-query/src/main/java/com/lanyuanxiaoyao/service/hudi/service/TableService.java index 5bb1eb0..8c77beb 100644 --- a/service-hudi-query/src/main/java/com/lanyuanxiaoyao/service/hudi/service/TableService.java +++ b/service-hudi-query/src/main/java/com/lanyuanxiaoyao/service/hudi/service/TableService.java @@ -4,9 +4,7 @@ import com.eshore.odcp.hudi.connector.entity.TableMeta; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.lanyuanxiaoyao.service.forest.service.InfoService; -import org.apache.hadoop.conf.Configuration; -import org.apache.hudi.common.table.HoodieTableMetaClient; -import org.apache.hudi.common.table.TableSchemaResolver; +import com.lanyuanxiaoyao.service.hudi.utils.HoodieUtils; import org.apache.hudi.org.apache.avro.Schema; import org.eclipse.collections.api.map.ImmutableMap; import org.slf4j.Logger; @@ -45,12 +43,7 @@ public class TableService { @Cacheable(value = "table-schema", sync = true) @Retryable(Throwable.class) public ImmutableMap schema(String hdfs) throws Exception { - HoodieTableMetaClient client = HoodieTableMetaClient.builder() - .setConf(new Configuration()) - .setBasePath(hdfs) - .build(); - TableSchemaResolver schemaUtil = new TableSchemaResolver(client); - Schema schema = schemaUtil.getTableAvroSchema(true); + Schema schema = HoodieUtils.getSchema(hdfs); return mapper.readValue(schema.toString(), new TypeReference>() { }); } diff --git a/service-hudi-query/src/main/java/com/lanyuanxiaoyao/service/hudi/utils/HoodieUtils.java b/service-hudi-query/src/main/java/com/lanyuanxiaoyao/service/hudi/utils/HoodieUtils.java index b08552c..cfa1c20 100644 --- a/service-hudi-query/src/main/java/com/lanyuanxiaoyao/service/hudi/utils/HoodieUtils.java +++ b/service-hudi-query/src/main/java/com/lanyuanxiaoyao/service/hudi/utils/HoodieUtils.java @@ -8,12 +8,15 @@ import java.util.List; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.table.TableSchemaResolver; import org.apache.hudi.common.table.timeline.HoodieDefaultTimeline; import org.apache.hudi.common.table.timeline.HoodieInstant; import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.org.apache.avro.Schema; import org.eclipse.collections.api.factory.Lists; import org.eclipse.collections.api.list.ImmutableList; import org.slf4j.Logger; @@ -28,6 +31,19 @@ import org.slf4j.LoggerFactory; public class HoodieUtils { private static final Logger logger = LoggerFactory.getLogger(HoodieUtils.class); + public static Schema getSchema(String hdfs) throws Exception { + HoodieTableMetaClient client = HoodieTableMetaClient.builder() + .setConf(new Configuration()) + .setBasePath(hdfs) + .build(); + return getSchema(client); + } + + public static Schema getSchema(HoodieTableMetaClient client) throws Exception { + TableSchemaResolver schemaUtil = new TableSchemaResolver(client); + return schemaUtil.getTableAvroSchema(true); + } + public static ImmutableList getAllInstants(HoodieTableMetaClient client, Function getTimeline) throws IOException { FileSystem fileSystem = client.getRawFs(); // 直接使用 toString 方法得到的值是被缓存的