Python – Required field ‘sessionHandle’ not set! When using hives with Python

Required field ‘sessionHandle’ not set! When using hives with Python… here is a solution to the problem.

Required field ‘sessionHandle’ not set! When using hives with Python

I’ve tried various ways to use Hive with Python.

One is

How to Access Hive via Python?

Also tried https://sites.google.com/site/tingyusz/home/blogs/hiveinpython

Where am I going

  File "py_hive.py", line 8, in <module>
    database='default') as conn:
  File "/home/karimk/python/lib/python2.7/site-packages/pyhs2/__init__.py", line 7, in connect
    return Connection(*args, **kwargs)
  File "/home/karimk/python/lib/python2.7/site-packages/pyhs2/connections.py", line 52, in __init__
    cur.execute(query) 
  File "/home/karimk/python/lib/python2.7/site-packages/pyhs2/cursor.py", line 61, in execute
    res = self.client.ExecuteStatement(query)
  File "/home/karimk/python/lib/python2.7/site-packages/pyhs2/TCLIService/TCLIService.py", line 244, in ExecuteStatement
    return self.recv_ExecuteStatement()
  File "/home/karimk/python/lib/python2.7/site-packages/pyhs2/TCLIService/TCLIService.py", line 260, in recv_ExecuteStatement
    raise x
thrift. Thrift.TApplicationException: Required field 'sessionHandle' is unset! Struct:TExecuteStatementReq(sessionHandle:null, statement:USE default, confOverlay:{})

Python code:

import pyhs2

with pyhs2.connect(host='dmeet-master02.inetuhosted.net',
                   port=10000,
                   authMechanism="PLAIN",
                   user='userk',
                   password='userk',
                   database='default') as conn:
    with conn.cursor() as cur:
        #Show databases
        print cur.getDatabases()

#Execute query
        cur.execute("select * from table")

#Return column info from query
        print cur.getSchema()

#Fetch table results
        for i in cur.fetch():
            print i
            break

Any tips?

Solution

I’m using similar initialization code to connect Hive and it works for me.

However, I can see that it fails to initialize the connection.
The sessionHandle field is set internally when you open a connection to the server. When the corresponding socket connection fails to open, it is not set (or set to none). Try accessing without specifying a database and see if it works.

Related Problems and Solutions