如何使用python通过knox与hbase交互?

vohkndzv  于 2021-06-09  发布在  Hbase
关注(0)|答案(1)|浏览(532)

我正在尝试使用python通过knox与hbase进行交互,在python中,管理员给出了一个用于hive、hbase和spark的knoxapi端点列表,比如: https://knox-devl.www.mysite.com:9042/gateway/MYSITEHDO/hbaseversion/cluster 现在,因为我使用的是python的happybase库,所以我的连接代码是

import happybase

connection=happybase.Connection('https://knox-devl.www.mysite.com/gateway/MYSITEHDO/hbaseversion/cluster',port=9042)
connection.open()
print(connection.tables())

它显示的错误是: thriftpy.transport.TTransportException: TTransportException(message="Could not connect to ('https://knox-devl.www.mysite.com/gateway/MYSITEHDO/hbaseversion/cluster', 9042)", type=1) 我还尝试了phoenixdb lib

import phoenixdb

database_url = 'https://knox-devl.www.mysite.com:9042/gateway/MYSITEHDO/hbaseversion/cluster'
conn = phoenixdb.connect(database_url, autocommit=True)
cursor = conn.cursor()
cursor.execute("SHOW tables")

但我得到了另一个错误: phoenixdb.errors.InterfaceError: ('RPC request failed', None, None, BadStatusLine("''",)) Exception phoenixdb.errors.InterfaceError: InterfaceError('RPC request failed', None, None, BadStatusLine("''",)) in <bound method Connection.__del__ of <phoenixdb.connection.Connection object at 0x10bc97d90>> ignored 只有这样我才能通过curl获得一些数据:

curl -i -k -u guest:guest-password 'https://knox-devl.www.mysite.com:9042/gateway/MYSITEHDO/hbaseversion/cluster'

但是那里没有sql命令。
有没有人知道如何做到这一点,或者我在这里遗漏了什么,比如请求一个不同的url或者在集群上启用一些东西?

nbysray5

nbysray51#

正如您所指出的,通过knox与hbase交谈的唯一方法是通过hbase的restapi。happybase正在尝试通过rpc直接连接到hbase,knox将阻止rpc。
在启用了knox的集群之外,不能使用happybase。
在这里可以找到将hbase restapi与python结合使用的好教程。如果链接失效,本文中最有用的命令包括:
查看表的架构:

request = requests.get(baseurl + "/" + tablename + "/schema")

插入一行:

cellset = Element('CellSet')

linenumber = 0;

for line in shakespeare:      
    rowKey = username + "-" + filename + "-" + str(linenumber).zfill(6)
    rowKeyEncoded = base64.b64encode(rowKey)

    row = SubElement(cellset, 'Row', key=rowKeyEncoded)

    messageencoded = base64.b64encode(line.strip())
    linenumberencoded = encode(linenumber)
    usernameencoded = base64.b64encode(username)

    # Add bleet cell
    cell = SubElement(row, 'Cell', column=messagecolumnencoded)
    cell.text = messageencoded

    # Add username cell
    cell = SubElement(row, 'Cell', column=usernamecolumnencoded)
    cell.text = usernameencoded

    # Add Line Number cell
    cell = SubElement(row, 'Cell', column=linenumbercolumnencoded)
    cell.text = linenumberencoded

    linenumber = linenumber + 1

    # Submit XML to REST server
    request = requests.post(baseurl + "/" + tablename + "/fakerow", data=tostring(cellset), headers={"Content-Type" : "text/xml", "Accept" : "text/xml"})

删除表:

request = requests.delete(baseurl + "/" + tablename + "/schema")

相关问题