开发者

Using Hbase with C#

开发者 https://www.devze.com 2022-12-21 22:51 出处:网络
开发者_如何学运维How can I use a Hbase database with C#/VB.NET ? (use=connect, query, get the result, insert, update, delete)
开发者_如何学运维

How can I use a Hbase database with C#/VB.NET ? (use=connect, query, get the result, insert, update, delete)

I don't find useful answers with google.


I've just published HBase C# Thrift binding as nuget package. Alternatively you can grab code/binaries from bitbucket: https://bitbucket.org/vadim/hbase-sharp/downloads


From the description:

A REST-ful Web service gateway that supports XML, Protobuf, and binary data encoding options

There's a protobuf port for .NET and there are many XML manipulation APIs built-in.


HBase C# Thrift works nicely. Just download the latest thrift-0.9.2.exe, thrift.dll and Hbase.thrift file on your windows machine. You can generate the required c# files with the following command:

thrift-0.9.2.exe -gen csharp Hbase.thrift

You will get the following files as a result:

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

Include them in your project and add thrift.dll to your project references. Short sample code (list tables and insert/update a cell value):

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();                        
        }        
    }
}

The above code works nicely with the Hbase installation of the latest Hortonworks Data Platform, running on CentOS 7.

0

精彩评论

暂无评论...
验证码 换一张
取 消