Skip to content

Instantly share code, notes, and snippets.

@utkjad
Created December 18, 2017 20:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utkjad/0e0fc591bc283cd8d5b1cb7806541c8b to your computer and use it in GitHub Desktop.
Save utkjad/0e0fc591bc283cd8d5b1cb7806541c8b to your computer and use it in GitHub Desktop.
Utility function to connect to Pyhive on HTTP mode.
from pyhive import hive
def connect_to_pyhive():
""" Connects to Pyhive with HTTP mode
"""
conn = hive.connect(thrift_transport=add_http_mode_support())
cursor = conn.cursor()
cursor.execute("show databases")
print cursor.fetchone()
def add_http_mode_support(username="xxx", password="xxx", port=10001, httpPath="/cliservice", host="127.0.0.1", transportMode="http"):
""" Utility function which geenrate Transport client object which in turn can be passwd to hive connection to
establish the HTTP mode connection.
"""
ap = "%s:%s" % (username, password)
import base64
from thrift.transport.THttpClient import THttpClient
_transport = THttpClient(host, port=port, path=httpPath)
_transport.setCustomHeaders({"Authorization": "Basic "+base64.b64encode(ap).strip()})
return _transport
if __name__ == '__main__':
connect_to_pyhive()
@leelulegend
Copy link

were you able to connect to the hive server which transport mode configured as "http"?

@Hanfee
Copy link

Hanfee commented Mar 12, 2021

why username and password is base64encode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment