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