使用happybase操作HBase

原创
2017/06/29 09:18
阅读数 885
from collections import OrderedDict
import happybase
connection = happybase.Connection('localhost', autoconnect=False)
connection.open()

table = connection.table('live_gift')
data = table.scan(filter="SingleColumnValueFilter('g', 'live_id', =, 'binary:15909')" and "SingleColumnValueFilter('g', 'gift_price', =, 'binary:20')", limit=20)
for k, d in data:
    print(k, d)
    

data = table.scan(columns=[b'g:user_id', b'g:gift_price'], filter="SingleColumnValueFilter('g', 'live_id', =, 'binary:15909')" and "SingleColumnValueFilter('g', 'gift_price', =, 'binary:20')", limit=20)

###求和
sum = 0
for k, d in data:
    gift_price = d['g:gift_price']
    sum = sum + int(gift_price)
    

        
        
new_data = table.counter_set(b'8029302121941_50001_534UYISNEHC_23171-key', b'g:live_id')
rows_as_dict = dict(table.rows([b'row-key-1', b'row-key-2']))
rows_as_ordered_dict = OrderedDict(data)


##计数
data = table.scan(columns=[b's:live_id', b's:start_time'], filter="SingleColumnValueFilter('s', 'start_time', >=, 'binary:1490976000')") 
count = 0
for k, d in data:
    live_id = d['s:live_id']
    count += 1
print(count)  ##live_num, 1249

    
##计数Method two
mylist =[]
data = table.scan(columns=[b's:live_id', b's:start_time'], filter="SingleColumnValueFilter('s', 'start_time', >=, 'binary:1490976000')")  
for k, d in data:
    live_id = d['s:live_id']
    mylist.append(live_id)
    
live_num = len(mylist)


import time
dt = "2016-04-01 00:00:00"
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
timestamp = int(time.mktime(timeArray))
timestamp


import time

##日期转为时间戳
def Timestamp_conversion(date):
    timeArray = time.strptime(date, "%Y-%m-%d %H:%M:%S")
    timestamp = int(time.mktime(timeArray))
    return timestamp
    
##时间戳转为日期
def date_conversion(timestamp):
    time_local = time.localtime(timestamp)
    date = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
    return date
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部