搜索
首页数据库mysql教程基于C#+Thrift操作HBase实践

在基于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

这里,我们基于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

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

create 'test_info', 'info'

表名为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);
    }
}

这里,简单叙述一下,我们提供的客户端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;
        }
    }
}

上面代码,给出了基本的实现,接着我们给出测试用例,调用我们实现的客户端操作,与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>
  • 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/
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
MySQL的角色:Web应用程序中的数据库MySQL的角色:Web应用程序中的数据库Apr 17, 2025 am 12:23 AM

MySQL在Web应用中的主要作用是存储和管理数据。1.MySQL高效处理用户信息、产品目录和交易记录等数据。2.通过SQL查询,开发者能从数据库提取信息生成动态内容。3.MySQL基于客户端-服务器模型工作,确保查询速度可接受。

mysql:构建您的第一个数据库mysql:构建您的第一个数据库Apr 17, 2025 am 12:22 AM

构建MySQL数据库的步骤包括:1.创建数据库和表,2.插入数据,3.进行查询。首先,使用CREATEDATABASE和CREATETABLE语句创建数据库和表,然后用INSERTINTO语句插入数据,最后用SELECT语句查询数据。

MySQL:一种对数据存储的初学者友好方法MySQL:一种对数据存储的初学者友好方法Apr 17, 2025 am 12:21 AM

MySQL适合初学者,因为它易用且功能强大。1.MySQL是关系型数据库,使用SQL进行CRUD操作。2.安装简单,需配置root用户密码。3.使用INSERT、UPDATE、DELETE、SELECT进行数据操作。4.复杂查询可使用ORDERBY、WHERE和JOIN。5.调试需检查语法,使用EXPLAIN分析查询。6.优化建议包括使用索引、选择合适数据类型和良好编程习惯。

MySQL初学者友好吗?评估学习曲线MySQL初学者友好吗?评估学习曲线Apr 17, 2025 am 12:19 AM

MySQL适合初学者,因为:1)易于安装和配置,2)有丰富的学习资源,3)SQL语法直观,4)工具支持强大。尽管如此,初学者需克服数据库设计、查询优化、安全管理和数据备份等挑战。

SQL是一种编程语言吗?澄清术语SQL是一种编程语言吗?澄清术语Apr 17, 2025 am 12:17 AM

是的,sqlisaprogramminglanguges pecialized fordatamanage.1)它具有焦点,focusingonwhattoachieveratherthanhow.2)sqlisessential forquerying forquerying,插入,更新,更新,和detletingdatainrelationalDatabases.3)

解释酸的特性(原子,一致性,隔离,耐用性)。解释酸的特性(原子,一致性,隔离,耐用性)。Apr 16, 2025 am 12:20 AM

ACID属性包括原子性、一致性、隔离性和持久性,是数据库设计的基石。1.原子性确保事务要么完全成功,要么完全失败。2.一致性保证数据库在事务前后保持一致状态。3.隔离性确保事务之间互不干扰。4.持久性确保事务提交后数据永久保存。

MySQL:数据库管理系统与编程语言MySQL:数据库管理系统与编程语言Apr 16, 2025 am 12:19 AM

MySQL既是数据库管理系统(DBMS),也与编程语言紧密相关。1)作为DBMS,MySQL用于存储、组织和检索数据,优化索引可提高查询性能。2)通过SQL与编程语言结合,嵌入在如Python中,使用ORM工具如SQLAlchemy可简化操作。3)性能优化包括索引、查询、缓存、分库分表和事务管理。

mySQL:使用SQL命令管理数据mySQL:使用SQL命令管理数据Apr 16, 2025 am 12:19 AM

MySQL使用SQL命令管理数据。1.基本命令包括SELECT、INSERT、UPDATE和DELETE。2.高级用法涉及JOIN、子查询和聚合函数。3.常见错误有语法、逻辑和性能问题。4.优化技巧包括使用索引、避免SELECT*和使用LIMIT。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中