thrift/swift:codegen-thrift java代码生成器

介绍codegen-thrift工具,可将Java接口自动转换为Thrift服务端和服务客户端代码,支持多种数据类型自动转换,简化Thrift服务部署流程。

codegen-thrift 是最近我完成的一个java代码生成程序,实现从普通接口类型生成基于thrift/swift框架的service/client 端封装java代码.这个工具可以让你在不修改现有的接口定义的情况下,自动生成基于现有接口的thrift service /client代码,实现client端通过网络对服务端接口的透明调用。

支持byte[],Date,float,Float,URI,URL类型的自动转换.

执行 java -jar target/codegen-thrift-1.0.0-SNAPSHOT-standalone.jar -help 显示运行参数说明.
参数说明

	-ic,--interface-class 要生成thrift代码的接口类名
	-rc,--reference-class 实现interface-class接口的一个类名(可选),如果不指定,生成的接口方法中参数名为arg1,arg2... 
	-p,--package <arg> 输出代码的包名
	-o,--output <arg> 代码输出位置
	-gt,--task-type 生成代码的类型:CLIENT,SERVICE

下面以thrift.test.original.TestInterface类为例,说明代码生成的过程

生成service端代码

java -jar target\codegen-thrift-1.0.0-SNAPSHOT-standalone.jar ^
	-ic thrift.test.original.TestInterface ^ 
	-rc thrift.test.original.TestInterface$BaseImpl ^
	-p thrift.test.service ^
	-o J:\codegen\codegen-thrift\src\test\java ^
	-gt SERVICE

生成代码中https://gitee.com/l0km/codegen/blob/master/codegen-thrift/src/test/java/thrift/test/service/TestInterfaceThriftDecorator.javathrift.test.original.TestInterface对应thrift service端实现类。

生成thrift client代码

生成IDL文件

gen-thrift.bat

https://gitee.com/l0km/codegen/blob/master/codegen-thrift/gen-thrift.bat

生成thrift client端数据类型及接口定义

gen-client.bat

https://gitee.com/l0km/codegen/blob/master/codegen-thrift/gen-client.bat
##生成 client代码:

java -jar target\codegen-thrift-1.0.0-SNAPSHOT-standalone.jar ^
	-ic thrift.test.original.TestInterface ^
	-rc thrift.test.original.TestInterface$BaseImpl ^
	-p thrift.test.client ^
	-o J:\codegen\codegen-thrift\src\test\java ^
	--thrift-package thrift.test.client.thrift ^
	-gt CLIENT

生成的代码中https://gitee.com/l0km/codegen/blob/master/codegen-thrift/src/test/java/thrift/test/client/TestInterfaceThriftClient.javathrift.test.original.TestInterface接口对应的 thrift client端实现。

启动服务

因为已经基于TestInterface接口自动生成了对应的service服务实现TestInterfaceThriftDecorator,所以将一个TestInterface接口实例启动为thrift服务就非常简单了:

	InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
	ThriftServerService service = ThriftServerService.bulider()
			.withServices(new TestInterfaceThriftDecorator(new TestInterface.BaseImpl()))
			.setServerPort(32232)
			.build();
	service.startAsync();

client 端创建thrift 接口实例

因为client的TestInterfaceThriftClient基于thrift框架实现了TestInterface接口,只要创建了TestInterfaceThriftClient实例,就可以像普通的接口实例一样调用,网络传输,数据类型转换等工作都由TestInterfaceThriftClient自动完成。

	// TestInterface 的thrift client实例
	TestInterface clientInstance = ClientFactory
			.builder()
			.setHostAndPort("127.0.0.1", 32232)
			.build(TestInterface.class, TestInterfaceThriftClient.class);
	// 调用接口
	clientInstance.test(null, 0, null, null, null);

生成代码的依赖

<dependency>
	<groupId>com.gitee.l0km</groupId>
	<artifactId>common-thrift</artifactId>
	<version>1.0.3-SNAPSHOT</version>
</dependency>

依赖库git仓库https://gitee.com/l0km/common-java/tree/master/common-thrift

thrift-generator 是通过 Java 的接口生成 thrift 文件的工具。例子:public interface ICommonUserService {     public User login(int id, String name);     public User getUserById(long id);     public boolean saveUser(User user);     public List getUserIds(long id);      public Map getUserByIds(List ids);     public Map<String, List> getUsersByName(List names);     public Map<Long, List> getGroupUsers(List names, List userList, List lns, long ll);     public List testCase1(Map num1, List num2, List num3, long num4, String num5); } public class ThriftStructBuilderTest {     private ThriftFileBuilder fileBuilder = new ThriftFileBuilder();     @Test     public void toOutputstream() throws Exception {         this.fileBuilder.buildToOutputStream(ICommonUserService.class, System.out);     } }执行代码:mvn test -Dtest=com.sohu.thrift.generator.builder.ThriftStructBuilderTest之后控制台输出如下:namespace java com.sohu.thrift.generator.test.thrift     enum Status {             NORMAL = 0,             BLOCKED = 1     }     struct Account {             1:i32 id,             2:string name     }     struct User {             1:i32 id,             2:string name,             3:bool sex,             4:Status status,             5:list ids,             6:Account account     }     service ICommonUserService {             User login(1:i32 arg0,2:string arg1),             map<string, list> getUsersByName(1:list arg0),             bool saveUser(1:User arg0),             map getUserByIds(1:list arg0),             list getUserIds(1:i64 arg0),             map<i64, list> getGroupUsers(1:list arg0,2:list arg1,3:list arg2,4:i64 arg3),             User getUserById(1:i64 arg0),             list testCase1(1:map arg0,2:list arg1,3:list arg2,4:i64 arg3,5:string arg4)     }
<think>好的,我现在需要帮助用户确认Iceberg Hive Catalog配置中的URI格式和Hive仓库路径的正确设置。用户提到了几个可能的配置选项,我需要结合他们提供的hive-site.xml内容以及之前的回答来给出准确的建议。 首先,用户提供的hive-site.xml中有几个关键的property: 1. hive.metastore.uris的value是thrift://hw-cdh-1:9083,这应该是Hive Metastore的服务地址。用户需要确定在Iceberg的CREATE CATALOG语句中,uri参数是否应该使用这个具体的地址(hw-cdh-1)还是示例中的metastore-host。这里需要指出,实际配置应替换为真实的主机名或IP,但用户示例中的hw-cdh-1可能就是他们实际的主机名,因此应直接使用。 2. hive.metastore.warehouse.dir的value是/user/hive/warehouse,这是一个HDFS路径,但用户示例中给出的warehouse路径是hdfs://nn:8020/warehouse/path,而用户现在想确认应该使用hdfs://namenode:8020/user/hive/warehouse还是直接使用/user/hive/warehouse。这里需要说明,Hive的warehouse.dir配置可能已经包含了HDFS的协议和地址,但有时候也可能只是路径部分,需要根据实际情况补全完整的HDFS URL。例如,如果Hive的配置是相对路径,那么Iceberg中的warehouse参数需要明确指定HDFS的namenode地址和端口,如hdfs://hw-cdh-1:8020/user/hive/warehouse,但用户提供的hive-site.xml中的warehouse.dir是/user/hive/warehouse,可能没有指定HDFS的地址,因此需要结合集群的实际HDFS配置来确定完整的路径。 接下来,用户的问题涉及URI格式和仓库路径的正确配置。需要解释thrift URI的格式,确认是否需要使用具体的主机名,以及仓库路径是否需要完整的HDFS URL。 此外,用户引用了之前的回答中的内容,特别是关于hive.metastore.uris和warehouse的映射。之前的回答中示例使用了thrift://metastore-host:9083和hdfs://namenode:8020/user/hive/warehouse,但用户的实际配置是thrift://hw-cdh-1:9083和/user/hive/warehouse。需要指出用户需要将示例中的占位符(如metastore-host、namenode)替换为他们实际的环境配置,比如hw-cdh-1可能是他们的Metastore主机名,而HDFS的namenode地址可能是hdfs://hw-cdh-1:8020或其他地址。 另外,用户提到的引用[3]和[4]涉及到Hive Metastore的性能问题,但当前问题主要关注URI和路径配置,这部分可能暂时不需要处理,但需要注意在配置正确的情况下,避免因为路径或URI错误导致的连接问题,进而引发性能问题或元数据不一致。 总结用户的需求:确认在Iceberg的Hive Catalog配置中,uri参数应该使用thrift://hw-cdh-1:9083还是其他形式,以及warehouse路径是否需要完整的HDFS URL还是可以相对路径。需要结合用户的hive-site.xml配置和实际集群环境来给出建议,同时参考之前的回答,确保配置正确,避免常见的连接失败或路径权限问题。</think>--- ### **关键配置解析** #### **1. Thrift URI 配置** 在 `hive-site.xml` 中,`hive.metastore.uris` 的值为 `thrift://hw-cdh-1:9083`,这是 **Hive Metastore 的实际服务地址**。在 Iceberg Hive Catalog 的 `CREATE CATALOG` 语句中,`uri` 参数应与此保持一致: ```sql CREATE CATALOG hive_catalog WITH ( 'type'='iceberg', 'catalog-type'='hive', 'uri'='thrift://hw-cdh-1:9083', -- 直接使用实际的主机名 hw-cdh-1 ... ); ``` - **`thrift://metastore-host:9083` 是示例占位符**,需替换为实际地址[^1][^3] - 若 `hive.metastore.uris` 包含多个地址(如高可用场景),需用逗号分隔,例如:`thrift://node1:9083,thrift://node2:9083` --- #### **2. Hive Warehouse 路径配置** `hive.metastore.warehouse.dir` 的值为 `/user/hive/warehouse`,但需根据 HDFS 实际地址补充 **完整的 URI 协议和 NameNode 信息**: ```sql CREATE CATALOG hive_catalog WITH ( ... 'warehouse'='hdfs://hw-cdh-1:8020/user/hive/warehouse', -- 补全 HDFS 协议和 NameNode 地址 ... ); ``` - **`hdfs://namenode:8020` 是示例占位符**,需替换为实际 NameNode 地址(例如 `hdfs://hw-cdh-1:8020`) - **路径格式对比**: | 格式 | 示例 | 适用场景 | |-----|-----|---------| | 完整 HDFS URI | `hdfs://hw-cdh-1:8020/user/hive/warehouse` | 跨集群或非默认文件系统 | | 相对路径 | `/user/hive/warehouse` | 仅当 Hadoop 配置已指定默认文件系统(如 `fs.defaultFS`)时可用 | --- ### **验证配置正确性** 1. **检查 Metastore 连通性**: ```bash telnet hw-cdh-1 9083 # 确认 Thrift 端口可达 ``` 2. **验证 HDFS 路径权限**: ```bash hadoop fs -ls hdfs://hw-cdh-1:8020/user/hive/warehouse ``` 3. **测试 Iceberg 表创建**: ```sql USE CATALOG hive_catalog; CREATE TABLE test_db.sample (id INT) WITH ('format-version'='2'); -- 检查表路径是否生成在正确位置 DESCRIBE EXTENDED test_db.sample; ``` --- ### **常见配置错误与修复** | 错误场景 | 表现 | 解决方案 | |---------|------|----------| | URI 主机名错误 | `Connection refused` 或超时 | 检查 `hive.metastore.uris` 与实际服务地址是否一致[^3] | | HDFS 路径权限不足 | `Permission denied` | 执行 `hadoop fs -chmod` 调整权限 | | 未指定完整 HDFS URI | 表路径指向本地文件系统 | 在 `warehouse` 参数中补全 `hdfs://namenode:port` 前缀 | --- ### **总结配置模板** 根据您的 `hive-site.xml` 配置,**最终 Iceberg Catalog 定义应为**: ```sql CREATE CATALOG hive_catalog WITH ( 'type'='iceberg', 'catalog-type'='hive', 'uri'='thrift://hw-cdh-1:9083', -- 来自 hive.metastore.uris 'warehouse'='hdfs://hw-cdh-1:8020/user/hive/warehouse', -- 补全 HDFS 地址和端口 'clients'='5', 'property-version'='1' ); ``` --- ### 相关问题 1. 如何诊断 Iceberg 与 Hive Metastore 的连接超时问题? 2. Iceberg 表存储路径与 Hive 默认路径不一致时如何处理? 3. 如何配置 Iceberg 使用 Kerberos 认证的 Hive Metastore?
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值