from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol import TBinaryProtocol
from hbase import THBaseService
from hbase.ttypes import TPut, TColumnValue, TGet, TNamespaceDescriptor, TTableDescriptor, TColumnFamilyDescriptor, \
def __init__(self, host, port):
socket = TSocket.TSocket(host=host, port=port)
self.transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
self.client = THBaseService.Client(protocol)
return [ns.name for ns in self.client.listNamespaceDescriptors()]
def namespace_exists(self, namespace):
return namespace in self.get_ns_list()
def create_ns(self, namespace):
if not self.namespace_exists(namespace):
self.client.createNamespace(TNamespaceDescriptor(name=namespace))
def create_table(self, table_name, column_family, namespace="ns"):
if not self.namespace_exists(namespace):
self.client.createNamespace(TNamespaceDescriptor(name=namespace))
if not self.table_exists(namespace, table_name):
_table_name = TTableName(
ns=namespace.encode("utf8"), qualifier=table_name.encode("utf8")
self.client.createTable(
TColumnFamilyDescriptor(name=column_family.encode("utf8"))
def table_exists(self, namespace, table_name):
namespace = namespace.encode("utf8")
table_name = table_name.encode("utf8")
_table_name = TTableName(ns=namespace, qualifier=table_name)
return self.client.tableExists(_table_name)
def put(self, namespace, table_name, row_key, family, qualifier, value):
row_key, family = row_key.encode("utf8"), family.encode("utf8")
qualifier, value = qualifier.encode("utf8"), value.encode("utf8")
table_in_bytes = self._table_in_bytes(namespace, table_name)
TColumnValue(family=family, qualifier=qualifier, value=value)
self.client.put(table_in_bytes, put)
def get(self, namespace, table_name, row_key):
row_key = row_key.encode("utf8")
table_in_bytes = self._table_in_bytes(namespace, table_name)
result = self.client.get(table_in_bytes, get)
def scan(self, namespace, table_name, start_row, stop_row):
start_row, stop_row = start_row.encode("utf8"), stop_row.encode("utf8")
table_in_bytes = self._table_in_bytes(namespace, table_name)
scan = TScan(startRow=start_row, stopRow=stop_row)
current_results = self.client.getScannerResults(
table_in_bytes, scan, caching
for result in current_results:
next_start_row = self._create_closest_row_after(last_result.row)
scan = TScan(startRow=next_start_row, stopRow=stop_row)
def batch_put(self, namespace, table_name):
puts = [TPut(row="row2".encode("utf8"), columnValues=[TColumnValue(family="f".encode("utf8"),
qualifier="q1".encode("utf8"), value="value2".encode("utf8"))]), ...]
puts = [TPut(row="row1".encode("utf8"), columnValues=[TColumnValue(family="cf1".encode("utf8"),
qualifier="q1".encode("utf8"),
value="value1".encode("utf8"))]),
TPut(row="row2".encode("utf8"), columnValues=[TColumnValue(family="cf1".encode("utf8"),
qualifier="q2".encode("utf8"),
value="value2".encode("utf8"))])]
table_in_bytes = self._table_in_bytes(namespace, table_name)
self.client.putMultiple(table=table_in_bytes, tputs=puts)
def batch_get(self, namespace, table_name):
gets = [TGet(row="row2".encode("utf8")), ...]
gets = [TGet(row="row1".encode("utf8")), TGet(row="row2".encode("utf8"))]
table_in_bytes = self._table_in_bytes(namespace, table_name)
self.client.getMultiple(table_in_bytes, gets)
def get_table_descriptor(self, ns, table):
"cf1": {"attr1": value1, "attr2": value2, ....},
"cf2": {"attr1": value1, "attr2": value2, ....},
table = table.encode("utf8")
table = TTableName(ns=ns, qualifier=table)
info = self.client.getTableDescriptor(table)
if info and info.columns:
for item in info.columns:
attributes = item.attributes
for key, value in attributes.items():
new_attr[self._bytes_to_str(key)] = self._bytes_to_str(value)
res[self._bytes_to_str(item.name)] = new_attr
for key, value in info.attributes.items():
table_attr[self._bytes_to_str(key)] = self._bytes_to_str(value)
def delete(self, namespace, table):
namespace, table = namespace.encode("utf8"), table.encode("utf8")
self.client.disableTable(TTableName(ns=namespace, qualifier=table))
self.client.deleteTable(TTableName(ns=namespace, qualifier=table))
def _bytes_to_str(byte_str):
return bytes.decode(byte_str)
def _table_in_bytes(namespace, table_name):
return "{namespace}:{table_name}".format(
namespace=namespace, table_name=table_name
def _create_closest_row_after(row):
client = Demo(host="xxx", port=9090)
client.create_table(table, column, name_space)
print(client.table_exists(name_space, table))
row_key = "row" + str(i)
client.put(name_space, table, row_key, column, "name", "handsome_boy" + str(i))
result = client.scan(name_space, table, start_row, stop_row)
result = client.get(name_space, table, "row4")
if __name__ == "__main__":