You need to enable JavaScript to run this app.
导航
LAS Catalog Java SDK 使用指南
最近更新时间:2025.11.04 16:29:16首次发布时间:2025.10.30 15:24:22
复制全文
我的收藏
有用
有用
无用
无用

火山引擎 LAS Catalog Java SDK 是一个与 LAS Catalog 服务交互的开发工具,能帮助您通过代码管理元数据,提升数据治理效率。它适用于数据集成、批量元数据操作等场景。
本文将指导您如何安装并使用该 SDK。

使用前提

  • 您已注册火山引擎账号并完成实名认证,具体步骤,请参见账号注册实名认证
  • 如果要使用火山引擎Java SDK访问指定服务的API,请确认您已在 火山引擎控制台 开通当前服务。
  • 您已获取账号的AccessKey、SecretKey,具体步骤,请参见获取AccessKey、SecretKey
  • 火山引擎 Java SDK 支持 Java JDK 1.8 及其以上版本。

SDK 获取和安装

推荐您通过 Maven 安装依赖的方式使用火山引擎 Java SDK。
首先您需要在您的操作系统中 下载安装 Maven ,安装完成后您只需在 Maven 项目的 pom.xml 文件加入相应的依赖项即可。

<repositories>
    <!-- 自定义仓库 -->
    <repository>
      <id>bytedance-las-repo</id>
      <url>https://artifact.bytedance.com/repository/data_compute_engine_service</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

指定依赖时请选择特定的版本号,否则可能会在构建时导致不可预见的问题。

  • 由于高于或者等于 Java 9 版本的SDK中 javax.annotation-api 被移除,若您的SDK高于或者等于Java 9,需要添加以下依赖:
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

添加依赖

根据需要引入lf-client-2或者 lf-client-3 SDK依赖包。

兼容开源 Hive 2 协议,具体结构可以参考官网 Thrift 定义

<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>lf-client-2</artifactId>
    <version>1.6.0-RELEASE</version>
</dependency>

依赖包版本

  • 1.5.0-RELEASE(发布于2025年4月28日)
  • 1.6.0-RELEASE(发布于2025年10月17日)

环境变量导入

说明

Endpoint和Region用于指定服务接入点和区域,请参考链接地址获取对应值。

export VOLCENGINE_AK=your ak
export VOLCENGINE_SK=your sk
export VOLCENGINE_ENDPOINT=endpoint
export VOLCENGINE_REGION=region

代码示例

说明

认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。

构建客户端

String ak = System.getenv("VOLCENGINE_AK");
String sk = System.getenv("VOLCENGINE_SK");
String endpoint = System.getenv("VOLCENGINE_ENDPOINT");
String region = System.getenv("VOLCENGINE_REGION");
HiveConf conf = new HiveConf();
conf.set(HiveConf.ConfVars.METASTOREURIS.varname, endpoint);
conf.set(ServerlessHiveConf.ConfVars.HIVE_CLIENT_LAS_REGION_NAME.varname, region);
conf.set(ServerlessHiveConf.ConfVars.HIVE_CLIENT_LAS_AK.varname, ak);
conf.set(ServerlessHiveConf.ConfVars.HIVE_CLIENT_LAS_SK.varname, sk);
conf.setBoolean(ServerlessHiveConf.ConfVars.METASTORE_IS_PUBLIC_CLOUD.varname, true);
conf.setBoolean(HiveConf.ConfVars.METASTORE_EXECUTE_SET_UGI.varname, false);
conf.set(ServerlessHiveConf.ConfVars.HIVE_CLIENT_PUBLIC_CLOUD_TYPE.varname, "catalog-service");
// 设置该请求访问的las的catalog
conf.set("hive.metastore.catalog.default", "hive");
HiveMetaStoreClient hmsClient = new HiveMetaStoreClient(conf);
//ak/sk也可以通过设置 hmsClient.setAccessKeyIdAndSecretAccessKey(ak,sk);
//可以使用retryingmetastoreclient,自动重试,IMetaStoreClient hmsClient = RetryingMetaStoreClient.getProxy(conf,null, HiveMetaStoreClient.class.getName());

获取表

List<String> allDatabases = hmsClient.getAllDatabases();
System.out.println(allDatabases);
Table table = hmsClient.getTable("db_test","tbl_test");
System.out.println(table);

创建表

import org.apache.hadoop.hive.metastore.api.*;
Table table = new Table();
table.setDbName("db_test");
table.setTableName("tbl_test");
// 字段定义
List<FieldSchema> cols = new ArrayList<>();
cols.add(new FieldSchema("user_id", "bigint", "用户ID"));
cols.add(new FieldSchema("user_name", "string", "用户名"));
cols.add(new FieldSchema("age", "int", "年龄"));
cols.add(new FieldSchema("email", "string", "邮箱"));
table.setSd(new StorageDescriptor());
table.getSd().setCols(cols);
// 分区字段
List<FieldSchema> partCols = new ArrayList<>();
partCols.add(new FieldSchema("dt", "string", "分区日期"));
table.setPartitionKeys(partCols);
// 存储格式
table.getSd().setInputFormat("org.apache.hadoop.mapred.TextInputFormat");
table.getSd().setOutputFormat("org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat");
table.getSd().setSerdeInfo(new SerDeInfo());
table.getSd().getSerdeInfo().setSerializationLib("org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe");
// 表属性
Map<String, String> tblProps = new HashMap<>();
tblProps.put("creator", "hive_demo");
table.setParameters(tblProps);
// 通过 HMS 客户端创建表
hiveMetastoreClient.createTable(table);

链接地址

Region

Endpoint

备注

cn-beijing

thrift://lakeformation.las.cn-beijing.ivolces.com:48869

仅支持火山内部访问,不支持公网访问

cn-shanghai

thrift://lakeformation.las.cn-shanghai.ivolces.com:48869

cn-guangzhou

thrift://lakeformation.las.cn-guangzhou.ivolces.com:48869

ap-southeast-1

thrift://lakeformation.las.ap-southeast-1.ivolces.com:48869