Skip to content

Instantly share code, notes, and snippets.

@vcancy
Created June 29, 2018 08:09
Show Gist options
  • Save vcancy/1740230224c0a8cb0670d7cc2b71164d to your computer and use it in GitHub Desktop.
Save vcancy/1740230224c0a8cb0670d7cc2b71164d to your computer and use it in GitHub Desktop.
hbase demo
import happybase
import time
import traceback
import json
def put(pool):
with pool.connection() as conn:
table = conn.table('Log')
row_key = str(int(time.time())) + '000001'
data = {
"data:request".encode(encoding='utf-8'): '123'.encode(encoding='utf-8'),
"data:response": json.dumps({"1": "2"}),
"data:time": '',
"data:func": '',
"data:error": '',
"user_id:user_id": '0001'
}
table.put(row_key, data)
conn.close()
def get(pool):
with pool.connection() as connection:
try:
table = connection.table('Log')
x = [i for i in table.scan()]
datas = convert_hbaseresult(x)
print(datas)
except Exception as e:
traceback.print_exc()
def convert_hbaseresult(hbase_result):
ret = []
for data in hbase_result:
row_key = data[0].decode('utf-8')
data = {k.decode('utf-8'): v.decode('utf-8') for k, v in data[1].items()}
ret.append({"row_key": row_key, "data": data})
return ret
def create(pool):
with pool.connection() as connection:
connection.create_table(
'Log',
{
'data': dict(max_versions=1),
'user_id': dict()
})
if __name__ == '__main__':
pool = happybase.ConnectionPool(size=3, host='127.0.0.1')
create(pool)
put(pool)
get(pool)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment