Denver
Denver

Reputation: 245

Connecting Hive remote server from Python

I have a trouble connecting to hive running on remote server through my python script.
I'm using the same script (With different server details, of course) to connect to hive running on my localhost & am able to connect.

I'm starting the server on local host from command line with a command:

hive —service hiveserver2

That start the server and I run the python script

Script to connect to Hive running on local host:

import pyhs2    
conn = pyhs2.connect(host='localhost', port=10000, authMechanism='PLAIN', user='hive', password ='', database='default')
    with conn.cursor() as cur:
            cur.execute("show databases")
            for i in cur.fetch():
                print i  

Using above code, am able to access db @ Hive on local host.

I'm using below code to connect to remote server, here I'm not doing anything on command line to start the remote server.

Script to connect to Hive running on the remote server:

conn = pyhs2.connect(host='<my remote server Ip>', port=<port no>, authMechanism='PLAIN', user='<usernameToConnectToRemoteServer>', password ="<remoteServerPassword>"  database='default')
with conn.cursor() as cur:
        cur.execute("show databases")
        for i in cur.fetch():
            print i

and this returns me a message:

thrift.transport.TTransport.TTransportException: TSocket read 0 bytes.

I've tried to google & find the solution, as much as i can, but all I see are the examples to connect to local host. please help me resolve this.

Upvotes: 2

Views: 3269

Answers (1)

anurag
anurag

Reputation: 636

Try doing SSH to your remote machine and then connect to hive like below-

import paramiko
import traceback

def hive_query_executor():
dns_name = ''
conn_obj = paramiko.SSHClient()
conn_obj.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
    conn_obj.connect(dns_name, username="hadoop",
                     key_filename='')# or password

    Hive_query="select * from abc limit 10;"
    query_execute_command = 'ihive -e "' + impala_query + '"'
    std_in, std_out, std_err = conn_obj.exec_command(query_execute_command)

    conn_obj.close()

except:
    print "Error :" + str(traceback.format_exc())
    exit(0)


hive_query_executor()

Upvotes: 1

Related Questions