python连接hbase做数据分析_Python访问hbase数据操作脚本分享

#!/usr/bin/python

import getopt,sys,time

from thrift.transport.TSocket import TSocket

from thrift.transport.TTransport import TBufferedTransport

from thrift.protocol import TBinaryProtocol

from hbase import Hbase

def usage():

print '''Usage :

-h: Show help information;

-l: Show all table in hbase;

-t {table} Show table descriptors;

-t {table} -k {key} : show cell;

-t {table} -k {key} -c {coulmn} : Show the coulmn;

-t {table} -k {key} -c {coulmn} -v {versions} : Show more version;

(write by liuhuorong@koudai.com)

'''

class geilihbase:

def __init__(self):

self.transport = TBufferedTransport(TSocket("127.0.0.1", "9090"))

self.transport.open()

self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)

self.client = Hbase.Client(self.protocol)

def __del__(self):

self.transport.close()

def glisttable(self):

for table in self.client.getTableNames():

print table

def ggetColumnDescriptors(self,table):

rarr=self.client.getColumnDescriptors(table)

if rarr:

for (k,v) in rarr.items():

print "%-20st%s" % (k,v)

def gget(self,table,key,coulmn):

rarr=self.client.get(table,key,coulmn)

print "%-15s %-20st%s" % (rarr[0].timestamp,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(rarr[0].timestamp/1000)),rarr[0].value)

def ggetrow(self,table,key):

rarr=self.client.getRow(table, key)

for (k,v) in rarr[0].columns.items():

print "%-20st%-15s %-20st%s" % (k,v.timestamp,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(v.timestamp/1000)),v.value)

def ggetver(self, table, key, coulmn, versions):

rarr=self.client.getVer(table,key,coulmn, versions);

for row in rarr:

print "%-15s %-20st%s" % (row.timestamp,time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(row.timestamp/1000)),row.value)

def main(argv):

tablename=""

key=""

coulmn=""

versions=""

try:

opts, args = getopt.getopt(argv, "lht:k:c:v:", ["help","list"])

except getopt.GetoptError:

usage()

sys.exit(2)

for opt, arg in opts:

if opt in ("-h", "--help"):

sys.exit(0)

elif opt in ("-l", "--list"):

ghbase=geilihbase()

ghbase.glisttable()

elif opt == '-t':

tablename = arg

elif opt == '-k':

key = arg

elif opt == '-c':

coulmn = arg

elif opt == '-v':

versions = int(arg)

if ( tablename and key and coulmn and versions ):

ghbase.ggetver(tablename, key, coulmn, versions)

if (tablename and key and coulmn ):

ghbase.gget(tablename, key, coulmn)

if (tablename and key ):

ghbase.ggetrow(tablename, key)

if (tablename ):

ghbase.ggetColumnDescriptors(tablename)

sys.exit(1)

if __name__ == "__main__":

main(sys.argv[1:])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值