最近公司需要使用hbase数据库存储大数据,所以最近进行了研究。得出了点心得,记录下,也希望后面童鞋使用hbase时有点帮助。
一.hbase介绍
hbase的优点就不说了,hbase的介绍,我推荐个地址讲的很不错:
https://zhuanlan.zhihu.com/p/145551967?utm_source=wechat_session
二.使用thrift访问hbase。
要访问hbase需要进行几步来生成dll文件。
1.需要先下载hbase对应的thrift,http://archive.apache.org/dist/thrift/0.15.0/thrift-0.15.0.tar.gz .
这里0.15是最新的。0.15较之前的0.12(已废弃)相比,0.12之后的版本是用的Thrift2, 比之前的thrift精简了很多服务的调用。具体可查看地址:
https://blog.csdn.net/guxch/article/details/12163047
2.再通过链接 http://archive.apache.org/dist/thrift/0.15.0/thrift-0.15.0.exe 下载 thrift 的可执行文件,以及链接 https://hbaseuepublic.oss-cn-beijing.aliyuncs.com/hbase.thrift?spm=a2c4g.11186623.2.26.1de042ddV8UbdR&file=hbase.thrift 下载 hbase 的 thrift 定义文件。
3.下载完成后,打开命令行,跳转到对应的目录下,执行 thrift-0.15.0.exe --gen netstd hbase.thrift,这个命令会生成一个 gen-csharp 的文件夹
4.新建一个net core的类库,将gen-csharp下面的代码全部复制到新建的类库工程下面,并在这里类库中通过negut引用ApacheThrift 对应0.15.0版本的dll文件。
5.使用一个demo工程引用刚才所创建的net core类库。
public class Program
{
static void Main(string[] args)
{
TTransport transport = null;
try
{
TConfiguration configuration = null ;
IPAddress iPAddress = IPAddress.Parse(“127.0.0.1”);
transport = new TSocketTransport(iPAddress, 9090, configuration);
TBinaryProtocol tProtocol = new TBinaryProtocol(transport);
var client = new THbaseService.Client(tProtocol);
transport.OpenAsync().GetAwaiter().GetResult()
#region 插入一行数据多列
var colList = new List();
colList.Add(new TColumnValue(Encoding.UTF8.GetBytes("basic_info"), Encoding.UTF8.GetBytes("chat_user_id"), Encoding.UTF8.GetBytes("36234")));
colList.Add(new TColumnValue(Encoding.UTF8.GetBytes("basic_info"), Encoding.UTF8.GetBytes("friend_user_id"), Encoding.UTF8.GetBytes("36086")));
byte[] rowByte = Encoding.UTF8.GetBytes("1");
var list = new List()
{
new TPut(rowByte,colList)
};
client.putMultipleAsync(Encoding.UTF8.GetBytes("open_user_ship"), list).GetAwaiter().GetResult();
#endregion
TGet get = new TGet();
get.Row = Encoding.UTF8.GetBytes("1");
var result = client.getAsync(Encoding.UTF8.GetBytes("open_user_ship"), get).ConfigureAwait(false).GetAwaiter().GetResult();
foreach (var k in result.ColumnValues)
{
Console.WriteLine("Family:Qualifier:" + "n" + Encoding.UTF8.GetString(k.Qualifier));
Console.WriteLine("Value:" + Encoding.UTF8.GetString(k.Value));
}
Console.WriteLine("RowKey:{0}", result);
}
catch (Exception ex)
{
throw;
}
}
}
就这样基本就完成了thrift访问hbase数据库。希望对后面的童鞋有帮助。
参考地址:
https://developer.aliyun.com/article/766516?spm=a2c6h.13262185.0.0.7ccf4b12wBCmev;
https://blog.csdn.net/guxch/article/details/12163047



