printRow(client.getRow(t, row)[0]) mutations = [ Mutation(column="entry:num", value=str(e)), Mutation(column="entry:sqr", value=str(e * e)) ] client.mutateRow(t, row, mutations) printRow(client.getRow(t, row)[0]) time.sleep(0.05) mutations = [ Mutation(column="entry:num", value="-999"), Mutation(column="entry:sqr", isDelete=True) ] client.mutateRowTs(t, row, mutations, 1) # shouldn't override latest printRow(client.getRow(t, row)[0]) versions = client.getVer(t, row, "entry:num", 10) printVersions(row, versions) if len(versions) != 4: print("FATAL: wrong # of versions") sys.exit(-1) r = client.get(t, row, "entry:foo") if not r: print "yup, we didn't find entry:foo" # just to be explicit, we get lists back, if it's empty there was no matching row. if len(r) > 0: raise "shouldn't get here!"
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
] client.mutateRow(t, row, mutations) printRow(client.getRow(t, row)) time.sleep(0.05) mutations = [] m = Mutation() m.column = "entry:num" m.value = "-999" mutations.append(m) m = Mutation() m.column = "entry:sqr" m.isDelete = 1 mutations.append(m) client.mutateRowTs(t, row, mutations, 1) # shouldn't override latest printRow(client.getRow(t, row)) versions = client.getVer(t, row, "entry:num", 10) printVersions(row, versions) if len(versions) != 4: print ("FATAL: wrong # of versions") sys.exit(-1) try: client.get(t, row, "entry:foo") raise "shouldn't get here!" except ttypes.NotFound, e: pass print