火山引擎 LAS Catalog Java SDK 是一个与 LAS Catalog 服务交互的开发工具,能帮助您通过代码管理元数据,提升数据治理效率。它适用于数据集成、批量元数据操作等场景。
本文将指导您如何安装并使用该 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>
指定依赖时请选择特定的版本号,否则可能会在构建时导致不可预见的问题。
<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>
说明
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 |