Sample code Analysis for hbase operations in Python

Source: Internet
Author: User


Install HBase

HBase is a distributed Columnstore system built on HDFS and is primarily used for massive structured data storage. Here, our goal is simply to provide a basic environment for Python to access hbase, so download the binary package directly and use a stand-alone installation. Unzip after download, modify the configuration file, then you can start hbase directly. The system version used is ubuntu14.04.

Download

wget https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/1.2.4/hbase-1.2.4-bin.tar.gz Tar zxvf Hbase-1.2.4-bin.tar.gz

Configuration

Modify hbase-env.sh, set Java_home.

Export Java_home=/usr/lib/jvm/java-8-oracle

Modify Hbase-site.xml to set the root directory where the data is stored.

<configuration>    <property>        <name>hbase.rootdir</name>        <value>file:// /home/mi/work/hbase/data</value>    </property></configuration>

Start

bin/start-hbase.sh  # Launch Bin/hbase Shell  # enter HBase Interactive shell

Installing Thrift

After you install HBase, you also need to install thrift, because when you call HBase in other languages, you need to connect through thrift.

Installing Thrift Dependencies

sudo apt-get install Automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make Pkg-config

Ps:libboost1.55-all-dev, there's a problem with my ubuntu14.04, so it's libboost1.55.

Compiling the installation

Download the source code, unzip the installation after the compilation. Thrift Download Address

Tar zxf thrift-0.10.0.tar.gzcd thrift-0.10.0/./configure--with-cpp--with-boost--with-python--without-csharp-- With-java--without-erlang--without-perl--with-php--without-php_extension--without-ruby--without-haskell  -- Without-gomake  # Compilation takes a long time sudo make install

Start the HBase Thrift service

bin/hbase-daemon.sh Start Thrift

Check system processes

~/work/hbase/hbase-1.2.4/conf$ jps3009 ThriftServer4184 HMaster5932 Jps733 Main

You can see that Thriftserver has started successfully, and then we can use multiple languages to access hbase through thrift.

Python Operation HBase

The following example shows how to access HBase in Python.

Install dependent packages

sudo pip install thriftsudo pip install Hbase-thrift

Demo Program

From thrift import thriftfrom thrift.transport import tsocketfrom thrift.transport import Ttransportfrom Thrift.protocol Import tbinaryprotocolfrom hbase import hbasefrom hbase.ttypes Import *transport = Tsocket.tsocket (' localhost ', 9090) Transport = Ttransport.tbufferedtransport (transport) protocol = TBINARYPROTOCOL.TBINARYPROTOCOL (transport) client = Hbase.client (Protocol) Transport.open () contents = columndescriptor (name= ' cf: ', Maxversions=1) # client.deletetable (' Test ') client.createtable (' test ', [contents]) Print client.gettablenames () # Insert Datatransport.open () row = ' Row-key1 ' mutations = [Mutation (column= "cf:a", value= "1")]client.mutaterow (' Test ', row, mutations) # get one Rowtablename = ' Test ' RowKey = ' row-key1 ' result = Client.getrow (TableName, RowKey) print resultfor R in result:    print ' t He row is ', R.row    print ' The values are ', R.columns.get (' cf:a '). Value

Execution Result:

[' Test '][trowresult (columns={' cf:a ': Tcell (timestamp=1488617173254, Value= ' 1 ')}, row= ' Row-key1 ')]the row is row-key1the values are 1 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.