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, 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)
def resetCols(self, cols): ''' create cols ''' if self.tableName in self.__hbase.getTableNames(): self.__hbase.deleteTable(self.tableName) LOG.debug("create table %s with cols %s" % (self.tableName, cols)) self.__hbase.createTable( self.tableName, [ColumnDescriptor(name=str(col), maxVersions=5) for col in cols])
def writeTicks(self, ticks): ''' read quotes ''' tName = self.tableName(HBaseDAM.TICK) if tName not in self.__hbase.getTableNames(): self.__hbase.createTable(tName, [ColumnDescriptor(name=HBaseDAM.TICK, maxVersions=5)]) for tick in ticks: self.__hbase.updateRow(self.tableName(HBaseDAM.TICK), tick.time, [Mutation(column = "%s:%s" % (HBaseDAM.TICK, field), value = getattr(tick, field) ) for field in TICK_FIELDS])
def writeQuotes(self, quotes): ''' write quotes ''' tName = self.tableName(HBaseDAM.QUOTE) if tName not in self.__hbase.getTableNames(): self.__hbase.createTable(tName, [ColumnDescriptor(name=HBaseDAM.QUOTE, maxVersions=5)]) for quote in quotes: self.__hbase.updateRow(self.tableName(HBaseDAM.QUOTE), quote.time, [Mutation(column = "%s:%s" % (HBaseDAM.QUOTE, field), value = getattr(quote, field) ) for field in QUOTE_FIELDS])
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() # Check Thrift Server Type serverType = client.getThriftServerType() if serverType != TThriftServerType.ONE: raise Exception( "Mismatch between client and server, server type is %s" % serverType) 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
if __name__ == '__main__': h = HBaseClient() #delete all exiting tables for tName in h.getTableNames(): print "deleting %s" % tName h.deleteTable(tName) assert not h.getTableNames() #create table tName = 'testTable' h.createTable(tName, [ColumnDescriptor(name='col1', maxVersions=5), ColumnDescriptor(name='col2', maxVersions=5)]) print h.getTableNames() assert h.getTableNames() print "column families in %s" %(tName) print h.getColumnDescriptors(tName) #updateRow h.updateRows(tName, "bar", [Mutation(column="col1:bar", value='12345'), Mutation(column="col2:", value="67890")]) h.updateRows(tName, "foo", [Mutation(column="col1:foo", value='12345')]) print h.getRow(tName, 'bar') print h.getRow(tName, 'foo') #scan table rows = h.scanTable(tName, columns=["col1", "col2"]) print rows
# # 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 cols = client.getColumnDescriptors(t) print "column families in %s" % (t)
# # 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 cols = client.getColumnDescriptors(t) print "column families in %s" % (t)
# # 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 cols = client.getColumnDescriptors(t) print "column families in %s" %(t)
# testing if __name__ == '__main__': h = HBaseLib() #delete all exiting tables for tName in h.getTableNames(): print "disable %s" % tName h.disableTable(tName) #assert not h.getTableNames() #create table tName = 'testTable' h.createTable(tName, [ ColumnDescriptor(name='col1', maxVersions=5), ColumnDescriptor(name='col2', maxVersions=5) ]) print h.getTableNames() assert h.getTableNames() print "column families in %s" % tName print h.getColumnDescriptors(tName) #updateRow h.updateRow(tName, "bar", [ Mutation(column="col1:bar", value='12345'), Mutation(column="col2:", value="67890") ]) h.updateRow(tName, "foo", [Mutation(column="col1:foo", value='12345')]) print h.getRow(tName, 'bar')