def select(self, metric, resource, namespace, year1, month1, day1, hour, pretime): os.system('kinit -kt /etc/hbase.keytab hbase') sock = TSocket.TSocket(self.hbase_host, self.hbase_port) transport = TTransport.TSaslClientTransport(sock, self.hbase_host, "hbase") # Use the Binary protocol (must match your Thrift server's expected protocol) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() table = 'Monitor_record' return_list = [] result_list = [] pretime = int(pretime) if namespace: for i in range(pretime): year, month, day = self.time_handle(year1, month1, day1, i + 1) resultlist = [] pre = metric + "_" + resource + "_" + namespace + "_" + year + "-" + month + "-" + day + "T" + hour filter1 = "RowFilter(=, 'binaryprefix:{pre}')".format(pre=pre) tscan = TScan(filterString=filter1) sid = client.scannerOpenWithScan(table, tscan, {}) result = client.scannerGet(sid) while result: print result resultlist.append( float( result[0].columns.get("Metric:index_value").value)) result = client.scannerGet(sid) result_list.append(resultlist) else: for i in range(pretime): year, month, day = self.time_handle(year1, month1, day1, i + 1) resultlist = [] pre = metric + "_" + resource + "_" + year + "-" + month + "-" + day + "T" + hour filter1 = "RowFilter(=, 'binaryprefix:{pre}')".format(pre=pre) tscan = TScan(filterString=filter1) sid = client.scannerOpenWithScan(table, tscan, {}) result = client.scannerGet(sid) while result: print result resultlist.append( float( result[0].columns.get("Metric:index_value").value)) result = client.scannerGet(sid) result_list.append(resultlist) for i in range(len(result_list)): return_list = return_list + result_list[i] print return_list return return_list
def createConnection(): socket = TSocket.TSocket(host, port) if framed: transport = TTransport.TFramedTransport(socket) else: if secureMode: transport = TTransport.TSaslClientTransport( socket, host, saslServiceName) else: transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = THBaseService.Client(protocol) transport.open() return client, transport
def execute(): mutationsbatch = [] mutations_attributes = {} sock = TSocket.TSocket(thriftServer, thriftPort) transport = TTransport.TSaslClientTransport(sock, thriftServer, saslServiceName) #protocol = TCompactProtocol.TCompactProtocol(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() mutations = [ Hbase.Mutation(column="c:coluna1", value='Texto da coluna 1'), Hbase.Mutation(column="c:coluna2", value='Texto da coluna 2') ] row_key = '00001' mutationsbatch.append(Hbase.BatchMutation(row=row_key, mutations=mutations)) client.mutateRows(tablename, mutationsbatch, mutations_attributes) print('OK') del mutations del mutationsbatch mutationsbatch = [] transport.close() del client del protocol del transport del sock
from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from hbase import Hbase from hbase.ttypes import * import os if __name__ == '__main__': os.system( 'kinit -kt /etc/security/keytabs/xfyan.keytab xfyan/[email protected]' ) socket = TSocket.TSocket('172.24.5.242', 9090) #transport = TTransport.TBufferedTransport(socket) transport = TTransport.TSaslClientTransport(socket, host='bgs-5p242-yanxufei', service='xfyan', mechanism='GSSAPI') protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() #获取所有表名,返回list client.getTableNames() #使能表,返回true false client.enableTable(tableName='xfyan_test') #不使能表 client.disableTable(tableName='xfyan_test') #判断表是否使能 返回true false client.isTableEnabled(tableName='xfyan_test') #获取列族信息,返回字典 client.getColumnDescriptors(tableName='xfyan_test')
thriftServer = "hw10447.local" thriftPort = 9090 # The service name is the "primary" component of the Kerberos principal the # Thrift server uses. # See: http://web.mit.edu/kerberos/krb5-1.5/krb5-1.5.4/doc/krb5-user/What-is-a-Kerberos-Principal_003f.html # e.g. For a server principal of 'hbase/[email protected]', the primary is "hbase" saslServiceName = "hbase" # HBase table and data information tableName = "FOOBAR" row = 'r1' colName = "f1:cq1" # Open a socket to the server sock = TSocket.TSocket(thriftServer, thriftPort) # Set up a SASL transport. transport = TTransport.TSaslClientTransport(sock, thriftServer, saslServiceName) # Use the Binary protocol (must match your Thrift server's expected protocol) protocol = TBinaryProtocol.TBinaryProtocol(transport) # Pass the above to the generated HBase clietn client = Hbase.Client(protocol) transport.open() # Fetch a row from HBase print "Row=>%s" % (client.getRow(tableName, row, {})) # Cleanup transport.close()
def select(self, metric, resource, year, month, day, namespace): print("===start===") #base_host = hbase_host #hbase_port = hbase_port os.system('kinit -kt /etc/hbase.keytab hbase') sock = TSocket.TSocket(hbase_host, hbase_port) transport = TTransport.TSaslClientTransport(sock, hbase_host, "hbase") # Use the Binary protocol (must match your Thrift server's expected protocol) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) transport.open() table = 'Monitor_record' print("===end===") # connection = testHTTPServer_RequestHandler.connection table = 'Monitor_record' last = 0 result_list = [] listtotal = [] print("namespace", namespace) for i in range(t): if (namespace): list1 = [] for hour in range(24): if hour < 9: hour = "0" + str(hour) else: hour = str(hour) rowkey = "{metric}_{resource}_{namespace}_{year}-{month}-{day}T{hour}:00:00Z".format( metric=metric, resource=resource, namespace=namespace, year=year, month=month, day=day, hour=hour) result = client.getRow(table, rowkey, None) if result: list1.append( float(result[0].columns.get( "Metric:index_value").value)) if i == 0: last = result[0].columns.get("Metric:time").value result_list = list1 else: list1 = [] for hour in range(24): if hour < 9: hour = "0" + str(hour) else: hour = str(hour) rowkey = "{metric}_{resource}_{year}-{month}-{day}T{hour}:00:00Z".format( metric=metric, resource=resource, year=year, month=month, day=day, hour=hour) result = client.getRow(table, rowkey, None) if result: list1.append( float(result[0].columns.get( "Metric:index_value").value)) if i == 0: last = result[0].columns.get("Metric:time").value result_list = list1 year, month, day = self.time_handle(year, month, day) listtotal.append(result_list) if not listtotal: raise Exception("缺少目标容器节点近期数据") if not last: raise Exception("未发现startTime当天之前的数据") transport.close() listtotal.reverse() result_list = [] for li in listtotal: result_list += li return result_list, last.decode()