def __init__(self): transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) self.__client = Client(protocol) transport.open()
def __init__(self, ip='localhost', port=9090, timeout=10): transport = TSocket.TSocket(ip, int(port)) transport.setTimeout(timeout * 1000) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) self.__client = Client(protocol) transport.open()
def demo_client(host, port, is_framed_transport): # Make socket socket = TSocket.TSocket(host, port) # Make transport if is_framed_transport: transport = TTransport.TFramedTransport(socket) else: transport = TTransport.TBufferedTransport(socket) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Client(protocol) # Connect! transport.open() t = "demo_table" # # Scan all tables, look for the demo table and delete it. # print "scanning tables..." for table in client.getTableNames(): print " found: %s" % (table) if table == t: if client.isTableEnabled(table): print " disabling table: %s" % (t) client.disableTable(table) print " deleting table: %s" % (t) client.deleteTable(table) columns = [] col = ColumnDescriptor() col.name = 'entry:' col.maxVersions = 10 columns.append(col) col = ColumnDescriptor() col.name = 'unused:' columns.append(col) try: print "creating table: %s" % (t) client.createTable(t, columns) except AlreadyExists, ae: print "WARN: " + ae.message
def __init__(self, ip='localhost', port=9090, timeout = 10): transport = TSocket.TSocket(ip, int(port)) transport.setTimeout(timeout * 1000) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) self.__client = Client(protocol) transport.open()
def make_connection(self): self.socket = TSocket.TSocket(self.config.hbase_host, self.config.hbase_port) self.socket.setTimeout(self.config.hbase_timeout) self.transport = TTransport.TBufferedTransport(self.socket) self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport) self.client = Client(self.protocol) self.transport.open()
def demo_client(host, port, is_framed_transport): # Make socket socket = TSocket.TSocket(host, port) # Make transport if is_framed_transport: transport = TTransport.TFramedTransport(socket) else: transport = TTransport.TBufferedTransport(socket) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Client(protocol) # Connect! transport.open() t = "demo_table" # # Scan all tables, look for the demo table and delete it. # print "scanning tables..." for table in client.getTableNames(): print " found: %s" %(table) if table == t: if client.isTableEnabled(table): print " disabling table: %s" %(t) client.disableTable(table) print " deleting table: %s" %(t) client.deleteTable(table) columns = [] col = ColumnDescriptor() col.name = 'entry:' col.maxVersions = 10 columns.append(col) col = ColumnDescriptor() col.name = 'unused:' columns.append(col) try: print "creating table: %s" %(t) client.createTable(t, columns) except AlreadyExists, ae: print "WARN: " + ae.message
class HbaseUtil: def __init__(self, tableName, host='localhost', port=9090): self.tableName = tableName transport = TSocket.TSocket(host, port) self.transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) self.client = Client(protocol) self.transport.open() if tableName not in self.client.getTableNames(): print 'creating table %s' % tableName columns = [] col = ColumnDescriptor() col.name = 'page:title' col.maxVersions = 10 columns.append(col) col = ColumnDescriptor() col.name = 'page:article' columns.append(col) self.client.createTable(tableName, columns) #self.printAll() def close(self): self.transport.close() def insert(self, rowkey, title, content): mutations = [Mutation(column='page:article', value=content.encode('utf-8')), Mutation(column='page:title', value=title.encode('utf-8'))] self.client.mutateRow(self.tableName, rowkey, mutations, {}) def existRowKey(self, rowkey): pass def printAll(self): print 'starting scanner...' scanner = self.client.scannerOpen(self.tableName, '', ['page:title'], {}) r = self.client.scannerGet(scanner) while r: for i in r: print i.row for k, v in i.columns.items(): print k, v.value, v.timestamp x = v.value print x r = self.client.scannerGet(scanner) print 'scanner finished '
def __init__(self, tableName, host='localhost', port=9090): self.tableName = tableName transport = TSocket.TSocket(host, port) self.transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) self.client = Client(protocol) self.transport.open() if tableName not in self.client.getTableNames(): print 'creating table %s' % tableName columns = [] col = ColumnDescriptor() col.name = 'page:title' col.maxVersions = 10 columns.append(col) col = ColumnDescriptor() col.name = 'page:article' columns.append(col) self.client.createTable(tableName, columns)
class HBaseClient: ''' Hbase client ''' def __init__(self): transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) self.__client = Client(protocol) transport.open() def getTableNames(self): ''' get table names ''' return self.__client.getTableNames() def deleteTable(self, tName): ''' delete table name ''' if self.__client.isTableEnabled(tName): self.__client.disableTable(tName) self.__client.deleteTable(tName) def createTable(self, tName, ColumnDescriptors): try: self.__client.createTable(tName, ColumnDescriptors) except ttypes.AlreadyExists as excp: raise UfException(Errors.HBASE_CREATE_ERROR, "AlreadyExists Error when creating table %s with cols: %s): %s" % \ (tName, [col.name for col in ColumnDescriptors], excp.message)) def getColumnDescriptors(self, tName): try: return self.__client.getColumnDescriptors(tName) except: raise UfException(Errors.UNKNOWN_ERROR, "Error when getting column descriptors table %s" % tName) def updateRow(self, tName, rowName, mutations, timestamp=None): ''' add row to table ''' try: if timestamp is None: self.__client.mutateRow(tName, rowName, mutations) else: self.__client.mutateRowTs(tName, rowName, mutations, timestamp) except Exception as excp: raise UfException(Errors.HBASE_UPDATE_ERROR, "Error when updating table %s - rowName %s - mutations %s: %s" % \ (tName, rowName, mutations, excp)) def getRow(self, tName, rowName): ''' get row ''' result = self.__client.getRow(tName, rowName) if not result: return result else: return result[0] def scanTable(self, tName, columns, startRow="", endRow=None): ''' scan a table ''' if endRow is None: scanner = self.__client.scannerOpen(tName, startRow, columns) else: scanner = self.__client.scannerOpenWithStop(tName, startRow, endRow, columns) ret = [] row = self.__client.scannerGet(scanner) while row: ret.append(row[0]) row = self.__client.scannerGet(scanner) return ret def getClient(self): ''' return client, in case low level api is needed ''' return self.__client
for k in sorted(entry.columns): print k + " => " + entry.columns[k].value, print # Make socket transport = TSocket.TSocket('localhost', 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Client(protocol) # Connect! transport.open() t = "demo_table" # # Scan all tables, look for the demo table and delete it. # print "scanning tables..." for table in client.getTableNames(): print " found: %s" % (table) if table == t: if client.isTableEnabled(table): print " disabling table: %s" % (t)
for k in sorted(entry.columns): print k + " => " + entry.columns[k].value, print # Make socket transport = TSocket.TSocket("localhost", 9090) # Buffering is critical. Raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Client(protocol) # Connect! transport.open() t = "demo_table" # # Scan all tables, look for the demo table and delete it. # print "scanning tables..." for table in client.getTableNames(): print " found: %s" % (table) if table == t: if client.isTableEnabled(table): print " disabling table: %s" % (t)
from thrift import Thrift from thrift.transport import TSocket, TTransport from thrift.protocol import TBinaryProtocol # Hbase的 客户端代码 from hbase import ttypes from hbase.Hbase import Client, ColumnDescriptor, Mutation # make socket 这里配置的是hbase zookeeper的地址,因为master只负责负载均衡,读写由zookeeper协调 transport = TSocket.TSocket('localhost', 9090) # buffering is critical . raw sockets are very slow transport = TTransport.TBufferedTransport(transport) # wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # create a client to use the protocol encoder client = Client(protocol) # connect transport.open() t = 'tab2' # 扫描所有表获取所有表名称 print 'scanning tables ......' for table in client.getTableNames(): print 'found:%s' % table if client.isTableEnabled(table): print ' disabling table: %s' % t # 置为无效 client.disableTable(table)
from thrift.transport import TSocket, TTransport from thrift.protocol import TBinaryProtocol from hbase.Hbase import Client, ColumnDescriptor, Mutation import json from ..config import Testingconfig socket = TSocket.TSocket(Testingconfig.HBASE_HOST, Testingconfig.HBASE_PORT) transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) client = Client(protocol) #接口调用前查询方法 def select_before(userid, company): table = Testingconfig.HBASE_TABLE socket.open() results = client.getRow(table, str(userid) + company) socket.close() return results #hbase入库方法 def save_to_hbase(userid, company, data): table = Testingconfig.HBASE_TABLE socket.open() row = str(userid) + company mutations = [Mutation(column="info:current", value=json.dumps(data))] client.mutateRow(table, row, mutations) socket.close() suc_msg = { "msg": "",