dotnetcore 与 hbase 之二——thrift 客户端的制作

说明

在上一篇文章dotnetcore 与 hbase 之一——hbase 环境准备结束后,我们已经有了 hbase 数据库环境。接下来就可以利用 thrift 生成 c# hbase 客户端了。如果不了解 thrift,请戳这里

dotnet core 实现的 thrift

thrift 从 0.11.0 版本才有 netcore 的实现加入。在此之前有 .net farmwork 的实现版本。为了使用带有 dotnet core 版本我们选择最老的版本 0.11.0 进行安装。但是这有个问题, hbase 里的 thrift 版本是 0.9.0,版本不一致会导致某些传输协议不能使用,目前测试只有简单二进制协议TBinaryProtocol能用!!!

1. thrift 安装

thrift-0.11.0 可以从这里下载。

ubuntu18.04 安装 thrift 请转到这里或者自行百度。

2. thrift hbase 生成

在 hbase1.2.8 解压文件夹中查找Hbase.thrift,Linux 下可以使用命令find ./ -name Hbase.thrift进行查找。

hbase-1.2.8|⇒ find ./ -name Hbase.thrift
./hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

打开进入含有Hbase.thrift的目录,执行生成命令thrift --gen netcore Hbase.thrift。一般会有如下警告,可忽略:

[WARNING:/home/hsx/Downloads/hbase-1.2.8-src/hbase-1.2.8/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift:89] The "byte" type is a compatibility alias for "i8". Use "i8" to emphasize the signedness of this type.

执行查看命令ls -al,看生成的文件夹gen-netcore

total 40
drwxr-xr-x gen-netcore
-rw-rw-r-- Hbase.thrift

进入gen-netcore文件夹可看到一些 c#代码:

total 716K
-rw-r--r-- AlreadyExists.cs
-rw-r--r-- BatchMutation.cs
-rw-r--r-- ColumnDescriptor.cs
-rw-r--r-- Hbase.cs
-rw-r--r-- IllegalArgument.cs
-rw-r--r-- IOError.cs
-rw-r--r-- Mutation.cs
-rw-r--r-- TAppend.cs
-rw-r--r-- TCell.cs
-rw-r--r-- TColumn.cs
-rw-r--r-- TIncrement.cs
-rw-r--r-- TRegionInfo.cs
-rw-r--r-- TRowResult.cs
-rw-r--r-- TScan.cs

我们只需新建一个 dotnet 类库项目,比如dotnet new classlib -o HbaseNetCore,直接把这些文件拷贝到类库项目目录中就行。打开该项目,发现它引用了 thrift,但由于找不到引用会报错:

using Thrift;
using Thrift.Collections;
using Thrift.Protocols;
using Thrift.Protocols.Entities;
using Thrift.Protocols.Utilities;
using Thrift.Transports;
using Thrift.Transports.Client;
using Thrift.Transports.Server;

下一步介绍 thrift 项目的获取来解决该引用问题。

3. 获取 thrift 项目

打开 thrift-0.11.0 的解压文件夹,执行查找find ./ -name netcore,如下:

扫描二维码关注公众号,回复: 6989035 查看本文章
./lib/netcore # c#实现的thrift源码
./tutorial/netcore # c#实现的thrift使用教程
./test/netcore # 测试

我们真正需要的就是./lib/netcore中的thrift项目。事实上,./lib/中有多种语言实现的 thrift,比如:

lib|⇒ ls
as3     csharp  erl   java    lua          netcore  php  st
c_glib  d       go    javame  Makefile     nodejs   py   ts
cocoa   dart    haxe  js      Makefile.am  ocaml    rb   xml
cpp     delphi  hs    json    Makefile.in  perl     rs

找到./lib/netcore/thrift,将该项目拷贝至第二步中项目HbaseNetCore旁,然后 HbaseNetCore 引用 thrift 项目即可。事实上,在创建所有项目前应该创建一个解决方案来管理项目的,读者请自行创建。

4. 测试

新建测试项目来测试以上生成的客户端。具体实施请参看这里HBase and Thrift in .NET C# Tutorial

总结

以上步骤网上基本都有教程,此处列出来的是当初让笔者困惑的地方,如对读者也有帮助,那再好不过。如读者已经悉知,请自行略过。
由于使用的 thrift 与 hbase 中的版本不一致,导致只能使用简单二进制协议TBinaryProtocol!!!
后期将做一些对HbaseNetCore项目的帮助工具。敬请期待。

猜你喜欢

转载自www.cnblogs.com/hsxian/p/11326482.html