Thrift achieved through exchanges and Hbase C #

      Recently started a project needs to store our data to Hadoop big data environment, as I am a .net platform developers, not how the contact data is too large (because he is still too big a). But fortunately baidu, the google, or it found a solution, that is, C # to write data to hbase, and big data developers to read data from hbase multi-dimensional process, as part of the historical data will be transferred to the hive, or the part of the data is pushed to the machine learning library study.

First, access to the thrift hbase defined, and generates a class c #

1.1 hbase source address to get the definition of thrift


Note Hbase version must correspond with the run Hbase.

Please select thrift, rather than thrift2 , the reason is more easy to use fancy thrift Interface

1.2 Get thrift, c # code generated

   thrift generated code can refer me " half hour into the Thrift "

Second, the start of thrift service hbase

Enter the following command:

hbase-daemon.sh start thrift
HBase -daemons. SH Start Thrift (cluster version)

The thrift is the default port 9090, you can modify the default port in hbase-site.xml configuration file.

Third, writing test code with C #

var transport = new TSocket("10.34.51.62", 9090);
            TProtocol protocol = new TBinaryProtocol(transport);
            var client = new Hbase.Client(protocol);
            transport.Open();
            var tabls = client.getTableNames();
            foreach(var t in tabls)
            {
                Console.WriteLine(Encoding.Default.GetString(t));
            }
            //
            Mutation mutation = new Mutation();
            mutation.Column = Encoding.UTF8.GetBytes("personal_data:abc");
            mutation.Value = Encoding.UTF8.GetBytes("Hello");
            client.mutateRow(Encoding.UTF8.GetBytes("emp"), Encoding.UTF8.GetBytes("008"), new List<Mutation> { mutation }, null);
            Console.WriteLine("add success");
            var row = client.getRow(Encoding.UTF8.GetBytes("emp"), Encoding.UTF8.GetBytes("008"), null);
            foreach (var r in row)
            { 
                Console.WriteLine(Encoding.UTF8.GetString(r.Row));
                foreach(var c in r.Columns)
                {
                    Console.WriteLine("-- " + Encoding.UTF8.GetString(c.Key) + ":" + Encoding.UTF8.GetString(c.Value.Value));
                }
            }
            client.deleteAllRow(Encoding.UTF8.GetBytes("emp"), Encoding.UTF8.GetBytes("008"), null);
            Console.WriteLine("delete success");
            Console.ReadKey();
            transport.Close();

The code implements hbase write, read, delete and other functions.

Fourth, the test results

Successfully written.

V. Summary

Note 1. Select the thrift and version, obtained by viewing the maven dependency

2. Use thirft version, rather than thrift2

3. hbase of rowKey + Key same column, the data will be modified

Guess you like

Origin www.cnblogs.com/cqhaibin/p/10926553.html