Home Database Mysql Tutorial 基于C#+Thrift操作HBase实践

基于C#+Thrift操作HBase实践

Jun 07, 2016 pm 04:32 PM
hbase based on practice operate

在基于HBase数据库的开发中,对应Java语言来说,可以直接使用HBase的原生API来操作HBase表数据,当然你要是不嫌麻烦可以使用Thrift客户端Java API,这里有我曾经使用过的 HBase Thrift客户端Java API实践,可以参考。对于具有其他编程语言背景的开发人员,为

在基于HBase数据库的开发中,对应Java语言来说,可以直接使用HBase的原生API来操作HBase表数据,当然你要是不嫌麻烦可以使用Thrift客户端Java API,这里有我曾经使用过的 HBase Thrift客户端Java API实践,可以参考。对于具有其他编程语言背景的开发人员,为了获取HBase带来的好处,那么就可以选择使用HBase Thrift客户端对应编程语言的API,来实现与HBase的交互。
这里,我们使用C#客户端来操作HBase。HBase的Thrift接口的定义,可以通过链接http://svn.apache.org/viewvc/hbase/trunk/hbase-server/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift?view=markup看到,我们需要安装Thrift编译器,才能生成HBase跨语言的API,这里,我使用的版本是0.9.0。需要注意的是,一定要保证,安装了某个版本Thrift的Thrift编译器,在导入对应语言库的时候,版本一定要统一,否则就会出现各种各样的问题,因为不同Thrift版本,对应编程语言的库API可能有变化。
首先,下载上面链接的内容,保存为Hbase.thrift。
然后,执行如下命令,生成C#编程语言的HBase Thrift客户端API:

[hadoop@master hbase]$ thrift --gen csharp Hbase.thrift
[hadoop@master hbase]$ ls
gen-csharp
Copy after login

这里,我们基于C#语言,使用HBase 的Thrift 客户端API访问HBase表。事实上,如果使用Java来实现对HBase表的操作,最好是使用HBase的原生API,无论从性能还是便利性方面,都会提供更好的体验。使用Thrift API访问,实际也是在HBase API之上进行了一层封装,可能初次使用Thrift API感觉很别扭,有时候还要参考Thrift服务端的实现代码。
准备工作如下:

    1. 下载Thrift软件包,解压缩后,拷贝thrift-0.9.0/lib/java/src下面的代码到工作区(开发工具中)
    2. 将上面生成的gen-csharp目录中代码拷贝到工作区
    3. 保证HBase集群正常运行,接着启动HBase的Thrift服务,执行如下命令:
bin/hbase thrift -b master -p 9090 start
Copy after login

上面,HBase的Thrift服务端口为9090,下面通过Thrift API访问的时候,需要用到,而不是HBase的服务端口(默认60000)。
接着,实现一个简单的例子,访问Hbase表。
首先,我们通过HBase Shell创建一个表:

create 'test_info', 'info'
Copy after login

表名为test_info,列簇名称为info。
然后,我们开始基于上面生成的Thrift代码来实现对HBase表的操作。
这里,我们实际上是对HBase Thrift客户端Java API实践中的Java代码进行了翻译,改写成C#语言的相关操作。我们在客户端,进行了一层抽象,更加便于传递各种参数,抽象类为AbstractHBaseThriftService,对应的命名空间为HbaseThrift.HBase.Thrift,该类实现代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Thrift.Transport;
using Thrift.Protocol;
namespace HbaseThrift.HBase.Thrift
{
    public abstract class AbstractHBaseThriftService
    {
        protected static readonly string CHARSET = "UTF-8";
	    private string host = "localhost";
	    private int port = 9090;
	    private readonly TTransport transport;
	    protected readonly Hbase.Client client;
        public AbstractHBaseThriftService() : this("localhost", 9090)
        {
        }
        public AbstractHBaseThriftService(string host, int port)
        {
            this.host = host;
            this.port = port;
            transport = new TSocket(host, port);
            TProtocol protocol = new TBinaryProtocol(transport, true, true);
            client = new Hbase.Client(protocol);
        }
        public void Open() {
            if (transport != null)
            {
                transport.Open();
            }
	    }
        public void Close()
        {
            if (transport != null)
            {
                transport.Close();
            }
        }
        public abstract List GetTables();
	    public abstract void Update(string table, string rowKey, bool writeToWal,
			string fieldName, string fieldValue, Dictionary attributes);
        public abstract void Update(string table, string rowKey, bool writeToWal,
			Dictionary fieldNameValues, Dictionary attributes);
	    public abstract void DeleteCell(string table, string rowKey, bool writeToWal,
			    string column, Dictionary attributes);
	    public abstract void DeleteCells(string table, string rowKey, bool writeToWal,
			    List columns, Dictionary attributes);
	     public abstract void DeleteRow(string table, string rowKey,
		            Dictionary attributes);
	    public abstract int ScannerOpen(string table, string startRow, List columns,
	            Dictionary attributes);
	    public abstract int ScannerOpen(string table, string startRow, string stopRow, List columns,
	            Dictionary attributes);
	    public abstract int ScannerOpenWithPrefix(string table, string startAndPrefix,
                List columns, Dictionary attributes);
	    public abstract int ScannerOpenTs(string table, string startRow,
	            List columns, long timestamp, Dictionary attributes);
	    public abstract int ScannerOpenTs(string table, string startRow, string stopRow,
	            List columns, long timestamp, Dictionary attributes);
	    public abstract List ScannerGetList(int id, int nbRows);
	    public abstract List ScannerGet(int id);
	    public abstract List GetRow(string table, string row,
		            Dictionary attributes);
	    public abstract List GetRows(string table,
                 List rows, Dictionary attributes);
	    public abstract List GetRowsWithColumns(string table,
                 List rows, List columns, Dictionary attributes);
	    public abstract void ScannerClose(int id);
	    /**
	     * Iterate result rows(just for test purpose)
	     * @param result
	     */
	    public abstract void IterateResults(TRowResult result);
    }
}
Copy after login

这里,简单叙述一下,我们提供的客户端API的基本功能:

  • 建立到Thrift服务的连接:Open()
  • 获取到HBase中的所有表名:GetTables()
  • 更新HBase表记录:Update()
  • 删除HBase表中一行的记录的数据(cell):DeleteCell()和DeleCells()
  • 删除HBase表中一行记录:deleteRow()
  • 打开一个Scanner,返回id:ScannerOpen()、ScannerOpenWithPrefix()和ScannerOpenTs();然后用返回的id迭代记录:ScannerGetList()和ScannerGet()
  • 获取一行记录结果:GetRow()、GetRows()和GetRowsWithColumns()
  • 关闭一个Scanner:ScannerClose()
  • 迭代结果,用于调试:IterateResults()

比如,我们想要实现分页的逻辑,可能和传统的关系型数据库操作有些不同。基于HBase表的实现是,首先打开一个Scanner实例(例如调用ScannerOpen()),返回一个id,然后再使用该id,调用ScannerGetList()方法(可以指定每次返回几条记录的变量nbRows的值),返回一个记录列表,反复调用该ScannerGetList()方法,直到此次没有结果返回为止。后面会通过测试用例来实际体会。
现在,我们基于上抽象出来的客户端操作接口,给出一个基本的实现,代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HbaseThrift.HBase.Thrift
{
    class HBaseThriftClient : AbstractHBaseThriftService
    {
        public HBaseThriftClient() : this("localhost", 9090)
        {
        }
        public HBaseThriftClient(string host, int port) : base(host, port)
        {
        }
        public override List GetTables()
        {
            List tables = client.getTableNames();
            List list = new List();
            foreach(byte[] table in tables)
            {
                list.Add(Decode(table));
            }
            return list;
        }
        public override void Update(string table, string rowKey, bool writeToWal, string fieldName, string fieldValue, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] row = Encode(rowKey);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            List mutations = new List();
            Mutation mutation = new Mutation();
            mutation.IsDelete = false;
            mutation.WriteToWAL = writeToWal;
            mutation.Column = Encode(fieldName);
            mutation.Value = Encode(fieldValue);
            mutations.Add(mutation);
            client.mutateRow(tableName, row, mutations, encodedAttributes);
        }
        public override void Update(string table, string rowKey, bool writeToWal, Dictionary fieldNameValues, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] row = Encode(rowKey);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            List mutations = new List();
            foreach (KeyValuePair pair in fieldNameValues)
            {
                Mutation mutation = new Mutation();
                mutation.IsDelete = false;
                mutation.WriteToWAL = writeToWal;
                mutation.Column = Encode(pair.Key);
                mutation.Value = Encode(pair.Value);
                mutations.Add(mutation);
            }
            client.mutateRow(tableName, row, mutations, encodedAttributes);
        }
        public override void DeleteCell(string table, string rowKey, bool writeToWal, string column, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] row = Encode(rowKey);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            List mutations = new List();
            Mutation mutation = new Mutation();
            mutation.IsDelete = true;
            mutation.WriteToWAL = writeToWal;
            mutation.Column = Encode(column);
            mutations.Add(mutation);
            client.mutateRow(tableName, row, mutations, encodedAttributes);
        }
        public override void DeleteCells(string table, string rowKey, bool writeToWal, List columns, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] row = Encode(rowKey);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            List mutations = new List();
            foreach (string column in columns)
            {
                Mutation mutation = new Mutation();
                mutation.IsDelete = true;
                mutation.WriteToWAL = writeToWal;
                mutation.Column = Encode(column);
                mutations.Add(mutation);
            }
            client.mutateRow(tableName, row, mutations, encodedAttributes);
        }
        public override void DeleteRow(string table, string rowKey, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] row = Encode(rowKey);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            client.deleteAllRow(tableName, row, encodedAttributes);
        }
        public override int ScannerOpen(string table, string startRow, List columns, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] start = Encode(startRow);
            List encodedColumns = EncodeStringList(columns);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            return client.scannerOpen(tableName, start, encodedColumns, encodedAttributes);
        }
        public override int ScannerOpen(string table, string startRow, string stopRow, List columns, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] start = Encode(startRow);
            byte[] stop = Encode(stopRow);
            List encodedColumns = EncodeStringList(columns);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            return client.scannerOpenWithStop(tableName, start, stop, encodedColumns, encodedAttributes);
        }
        public override int ScannerOpenWithPrefix(string table, string startAndPrefix, List columns, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] prefix = Encode(startAndPrefix);
            List encodedColumns = EncodeStringList(columns);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            return client.scannerOpenWithPrefix(tableName, prefix, encodedColumns, encodedAttributes);
        }
        public override int ScannerOpenTs(string table, string startRow, List columns, long timestamp, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] start = Encode(startRow);
            List encodedColumns = EncodeStringList(columns);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            return client.scannerOpenTs(tableName, start, encodedColumns, timestamp, encodedAttributes);
        }
        public override int ScannerOpenTs(string table, string startRow, string stopRow, List columns, long timestamp, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] start = Encode(startRow);
            byte[] stop = Encode(stopRow);
            List encodedColumns = EncodeStringList(columns);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            return client.scannerOpenWithStopTs(tableName, start, stop, encodedColumns, timestamp, encodedAttributes);
        }
        public override List ScannerGetList(int id, int nbRows)
        {
            return client.scannerGetList(id, nbRows);
        }
        public override List ScannerGet(int id)
        {
            return client.scannerGet(id);
        }
        public override List GetRow(string table, string row, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            byte[] startRow = Encode(row);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            return client.getRow(tableName, startRow, encodedAttributes);
        }
        public override List GetRows(string table, List rows, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            List encodedRows = EncodeStringList(rows);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            return client.getRows(tableName, encodedRows, encodedAttributes);
        }
        public override List GetRowsWithColumns(string table, List rows, List columns, Dictionary attributes)
        {
            byte[] tableName = Encode(table);
            List encodedRows = EncodeStringList(rows);
            List encodedColumns = EncodeStringList(columns);
            Dictionary encodedAttributes = EncodeAttributes(attributes);
            return client.getRowsWithColumns(tableName, encodedRows, encodedColumns, encodedAttributes);
        }
        public override void ScannerClose(int id)
        {
            client.scannerClose(id);
        }
        public override void IterateResults(TRowResult result)
        {
            foreach (KeyValuePair pair in result.Columns)
            {
                Console.WriteLine("\tCol=" + Decode(pair.Key) + ", Value=" + Decode(pair.Value.Value));
            }
        }
        private String Decode(byte[] bs)
        {
            return UTF8Encoding.Default.GetString(bs);
        }
        private byte[] Encode(String str)
        {
            return UTF8Encoding.Default.GetBytes(str);
        }
        private Dictionary EncodeAttributes(Dictionary attributes)
        {
            Dictionary encodedAttributes = new Dictionary();
            foreach (KeyValuePair pair in attributes)
            {
                encodedAttributes.Add(Encode(pair.Key), Encode(pair.Value));
            }
            return encodedAttributes;
        }
        private List EncodeStringList(List strings) 
        {
            List list = new List();
            if (strings != null)
            {
                foreach (String str in strings)
                {
                    list.Add(Encode(str));
                }
            }
            return list;
        }
    }
}
Copy after login

上面代码,给出了基本的实现,接着我们给出测试用例,调用我们实现的客户端操作,与HBase表进行交互。实现的测试用例类如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HbaseThrift.HBase.Thrift
{
    class Test
    {
        private readonly AbstractHBaseThriftService client;
        public Test(String host, int port)
        {
            client = new HBaseThriftClient(host, port);
        }
        public Test() : this("master", 9090)
        {
        }
        static String RandomlyBirthday()
        {
            Random r = new Random();
            int year = 1900 + r.Next(100);
            int month = 1 + r.Next(12);
            int date = 1 + r.Next(30);
            return year + "-" + month.ToString().PadLeft(2, '0') + "-" + date.ToString().PadLeft(2, '0');
        }
        static String RandomlyGender()
        {
            Random r = new Random();
            int flag = r.Next(2);
            return flag == 0 ? "M" : "F";
        }
        static String RandomlyUserType()
        {
            Random r = new Random();
            int flag = 1 + r.Next(10);
            return flag.ToString();
        }
        public void Close()
        {
            client.Close();
        }
        public void CaseForUpdate() {
		    bool writeToWal = false;
            Dictionary attributes = new Dictionary(0);
		    string table = SetTable();
		    // put kv pairs
		    for (int i = 0; i 
<p>上面的测试可以实现操作Hbase表数据。另外,在生成的Thrift客户端代码中,Iface中给出了全部的服务接口,可以根据需要来选择,客户端Client实现了与Thrift交互的一些逻辑的处理,通过该类对象可以代理HBase提供的Thrift服务。</p>
<p><strong>参考链接</strong></p>
Copy after login
  • http://wiki.apache.org/hadoop/Hbase/ThriftApi
  • http://svn.apache.org/viewvc/hbase/trunk/hbase-server/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift?view=markup
  • http://www.cnblogs.com/panfeng412/archive/2012/11/11/hbase-thrift-api-common-issues-summary.html
  • https://github.com/simplegeo/hadoop-hbase/blob/master/src/examples/thrift/DemoClient.java
  • http://thrift.apache.org/tutorial/java/
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

How to stop Outlook from automatically adding events to my calendar How to stop Outlook from automatically adding events to my calendar Feb 26, 2024 am 09:49 AM

As an email manager application, Microsoft Outlook allows us to schedule events and appointments. It enables us to stay organized by providing tools to create, manage and track these activities (also called events) in the Outlook application. However, sometimes unwanted events are added to the calendar in Outlook, which creates confusion for users and spams the calendar. In this article, we will explore various scenarios and steps that can help us prevent Outlook from automatically adding events to my calendar. Outlook Events – A brief overview Outlook events serve multiple purposes and have many useful features as follows: Calendar Integration: In Outlook

What to do if you forget to press F2 for win10 boot password What to do if you forget to press F2 for win10 boot password Feb 28, 2024 am 08:31 AM

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

Dreamweaver CMS station group practice sharing Dreamweaver CMS station group practice sharing Mar 18, 2024 am 10:18 AM

Dream Weaver CMS Station Group Practice Sharing In recent years, with the rapid development of the Internet, website construction has become more and more important. When building multiple websites, site group technology has become a very effective method. Among the many website construction tools, Dreamweaver CMS has become the first choice of many website enthusiasts due to its flexibility and ease of use. This article will share some practical experience about Dreamweaver CMS station group, as well as some specific code examples, hoping to provide some help to readers who are exploring station group technology. 1. What is Dreamweaver CMS station group? Dream Weaver CMS

PHP Coding Practices: Refusing Alternatives to Goto Statements PHP Coding Practices: Refusing Alternatives to Goto Statements Mar 28, 2024 pm 09:24 PM

PHP Coding Practices: Refusal to Use Alternatives to Goto Statements In recent years, with the continuous updating and iteration of programming languages, programmers have begun to pay more attention to coding specifications and best practices. In PHP programming, the goto statement has existed as a control flow statement for a long time, but in practical applications it often leads to a decrease in the readability and maintainability of the code. This article will share some alternatives to help developers refuse to use goto statements and improve code quality. 1. Why refuse to use goto statement? First, let's think about why

Best Practices for Traffic Management with Golang Best Practices for Traffic Management with Golang Mar 07, 2024 am 08:27 AM

Golang is a powerful and efficient programming language that is widely used to build web services and applications. In network services, traffic management is a crucial part. It can help us control and optimize data transmission on the network and ensure the stability and performance of services. This article will introduce the best practices for traffic management using Golang and provide specific code examples. 1. Use Golang’s net package for basic traffic management. Golang’s net package provides a way to handle network data.

See all articles