如何在 C#/VB.NET 中使用 Hbase 数据库?(使用=连接、查询、获取结果、插入、更新、删除)
我在谷歌上找不到有用的答案。
我刚刚将 HBase C# Thrift 绑定发布为 nuget 包。或者,您可以从 bitbucket 获取代码/二进制文件:https ://bitbucket.org/vadim/hbase-sharp/downloads
HBase C# Thrift 运行良好。只需在您的 Windows 机器上下载最新的 thrift-0.9.2.exe、thrift.dll 和 Hbase.thrift 文件。您可以使用以下命令生成所需的 c# 文件:
thrift-0.9.2.exe -gen csharp Hbase.thrift
结果,您将获得以下文件:
AlreadyExists.cs
BatchMutation.cs
ColumnDescriptor.cs
Hbase.cs
IllegalArgument.cs
IOError.cs
Mutation.cs
TAppend.cs
TCell.cs
TColumn.cs
TIncrement.cs
TRegionInfo.cs
TRowResult.cs
TScan.cs
将它们包含在您的项目中并将 thrift.dll 添加到您的项目引用中。简短的示例代码(列出表格并插入/更新单元格值):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Thrift.Protocol;
using Thrift.Transport;
namespace bdc
{
class Program
{
static void Main(string[] args)
{
// Connection
var socket = new TSocket("hdp1.localdomain", 9090);
var transport =new TBufferedTransport(socket);
var protocol = new TBinaryProtocol(transport);
Hbase.Client hba = new Hbase.Client(protocol);
transport.Open();
// Get table names
Console.WriteLine("<-GET LIST OF TABLES->");
var tableNames = hba.getTableNames();
foreach (var tableName in tableNames)
Console.WriteLine(Encoding.UTF8.GetString(tableName, 0, tableName.Length));
// Insert rows
Console.WriteLine("<-INSERT->");
Mutation _mutation = new Mutation();
_mutation.IsDelete = false;
_mutation.Column = Encoding.UTF8.GetBytes("image:bodyimage");
_mutation.Value = Encoding.UTF8.GetBytes("newnew image 2.jpg");
hba.mutateRow(Encoding.UTF8.GetBytes("blogposts"), Encoding.UTF8.GetBytes("post1"), new List<Mutation> { _mutation }, null);
// Finished
Console.WriteLine("<-FINISHED->");
Console.ReadKey();
}
}
}
上面的代码在 CentOS 7 上运行的最新 Hortonworks 数据平台的 Hbase 安装中可以很好地工作。