使用happybase操作HBase
博客专区 > KYO4321 的博客 > 博客详情
使用happybase操作HBase
KYO4321 发表于10个月前
使用happybase操作HBase
  • 发表于 10个月前
  • 阅读 30
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

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
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 3
博文 32
码字总数 16565
×
KYO4321
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: