python--Hbase插入数据,python--hbase插入
#coding=utf-8import happybase
port = xxxxxx
host = xxxxxx
class HbaseConPool(object):
'''hbase连接池'''
def __init__(self):
'''
Constructor
'''
self.pool = happybase.ConnectionPool(size=20,host=xxx,port=xxx,timeout=60000*2)
def __new__(cls,*args,**kwargs):
'''
'''
if not hasattr(cls,'_inst'):
cls._inst=super(HbaseConPool,cls).__new__(cls,*args,**kwargs)
return cls._inst
# def createHbaseConPool(self):
# pool = happybase.ConnectionPool(5,host=constant.HBASE_HOST,port=constant.HBASE_PORT)
def get_conn(self):
'''从hbase连接池中获得一个hbase链接'''
# if self.pool == None:
# self.pool = self.createHbaseConPool()
return self.pool.connection()
def close(self):
'''关闭hbase连接池'''
self.pool.connection.close()
# def __exit__(self):
# print '=============='
# self.pool.connection.close()
hbase_con_pool = HbaseConPool()
if __name__ == '__main__':
with hbase_con_pool.get_conn() as conn:
# batch = conn.batch('zx')
# table = conn.table('zx')
# # batch = table.batch()
# # batch = batch.put('002',{'cf1:age':'22'})
# # batch = batch.put('002',{'cf1:match':'138'})
# # batch.send()
# table.put('002',{'cf1:age':'22'})
# table.put('002',{'cf1:match':'138'})
# rs = table.scan()
# for key,data in rs:
# print key,data
#设置睡眠百分比(Row:用户id+设备id)
table = conn.table('sleep_status_count')
table.put('000005Z04U0UQB0OJM4V1',
{'cf:deviceId': '',
'cf:sleepStatusCount': '',
'cf:userId': ''
})
rs = table.row('000005Z04U0UQB0OJM4V1')
print rs
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。