Skip to content

How to Access Hive With Python script?

You can read hive tables using pyhive python library. 

Install PyHive library

pip install pyhive

Connect to Hive using LDAP

from pyhive import hive
connection = hive.connect(host='HIVE_HOST',
                          port=10000,
                          database='temp',
                          username='HIVE_USERNAME',
                          password='HIVE_PASSWORD',
                          auth='CUSTOM')	

Connect to Hive using Kerberos

from pyhive import hive
connection = hive.connect(host='HIVE_HOST',
                          port=10000,
                          database='temp',
                          username='HIVE_USERNAME',
                          auth='KERBEROS',
                          kerberos_service_name='hive')	

To connect using kerberos, you don’t need to supply password. However you need to provide kerberos service name.  

Execute hive Query using PyHive

query="select * from temp.test_table"
cur = connection.cursor()
cur.execute(query)
res = cur.fetchall()

Published inHadoophive

2 Comments

  1. Harkiran Singh Harkiran Singh

    I’m getting below error using Kerberos. Can you please advise?

    thrift.transport.TTransport.TTransportException: Could not start SASL: b’Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to find a callback: 2′

  2. Pramith shetty Pramith shetty

    I recieve below error using LDAP
    Traceback (most recent call last):
    File “test.py”, line 7, in
    auth=’CUSTOM’)
    File “/usr/local/lib64/python3.6/site-packages/pyhive/hive.py”, line 94, in connect
    return Connection(*args, **kwargs)
    File “/usr/local/lib64/python3.6/site-packages/pyhive/hive.py”, line 192, in __init__
    self._transport.open()
    File “/usr/local/lib/python3.6/site-packages/thrift_sasl/__init__.py”, line 96, in open
    message=(“Bad status: %d (%s)” % (status, payload)))
    thrift.transport.TTransport.TTransportException: Bad status: 72 (b’1.1 400 Bad Request\r\nContent-Length: 0\r\nConnection: close\r\nServer: Jetty(7.6.0.v20120127)\r\n\r\n’

Leave a Reply

Your email address will not be published. Required fields are marked *