Invoke HBase with Python

Source: Internet
Author: User

Use Python to invoke hbase to install thrift Hbase-thrift

Thrift service to start HBase: bin/hbase-daemon.sh start Thrift default port is 9090

MySQL data synchronization to HBase:

1. Put

2, IMPORTTSV

3. Write the MapReduce job import

4, Sqoop

Simple code:

#!/usr/bin/env python

#coding =utf-8

Import Sys

Sys.path.append ('/usr/lib/python2.6/site-packages/hbase ')

From Thrift Import Thrift

From Thrift.transport import Tsocket

From Thrift.transport import Ttransport

From Thrift.protocol import Tbinaryprotocol

From HBase import HBase

From hbase.ttypes Import *

Import CSV

From hbase.ttypes import Columndescriptor, Mutation, Batchmutation, Tregioninfo

From hbase.ttypes import IOError, alreadyexists

######

Def client_conn ():

Transport=tsocket.tsocket ("172.16.10.87", 9090)

Transport=ttransport.tbufferedtransport (transport)

Protocol=tbinaryprotocol.tbinaryprotocol (transport)

Client=hbase.client (Protocol)

Transport.open ()

Return client

Def __del__ ():

Transport.close ()


If __name__== "__main__":

Client=client_conn ()

#获取表名字

Print Client.gettablenames ()

# #创建表

#client. CreateTable (' name2 ', [Columndescriptor (name= "user_id:", Maxversions=1), Columndescriptor (name= "user_name" ),])

#写入数据

Client.mutaterow (' name2 ', ' A1 ', [Mutation (column= ' user_id:1 ', value= "1")])

Client.mutaterow (' Ca_record ', ' 1 ', [Mutation (column= ' user_id:0 ', value= ' 5 ')])

# #获取数据


Aa=client.getrow (' name2 ', ' A1 ')

For R in AA:

print ' Row ', R.row

print ' \BR '

print ' value ', R.columns.get ("User_id:1"). Value

# #删除表

#client. disabletable ("T1")

#client. deletetable ("T1")

Print Client.gettablenames ()

# # #获取表的行键值

#print Client.scannerget (Client.scanneropen (' T2 ', "cmd", ["a"]))

Print Client.scannerget (Client.scanneropen (' T2 ', "", ["a"])) #当row key is null for the first



Print "------"

Print client.getcolumndescriptors (' T3 ')

#在自己开发环境 as long as the thrift and Hbase-thrift package is installed, the import will not be a problem

This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1950002

Invoke HBase with Python

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.