Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Big Data Hadoop & Spark by (107k points)

I have installed HDP 2.5 Sandbox on my windows system. I want to access Hive running on Sandbox using Python (to be specific "pyhive" library). 

I can log in to the HDP. The IP address is 127.0.0.1. I have installed a pyhive library. However, I am unable to connect from the system to Sandbox from python. 

Can you suggest what to do?

2 Answers

0 votes
by (50.2k points)

The following code represents how we can connect to Hive using pyhive:

from pyhive import hive

import pandas as pd

#Create Hive connection 

conn = hive.Connection(host="127.0.0.1", port=10000, username="username")

# Read Hive table and Create pandas dataframe

df = pd.read_sql("SELECT * FROM db_Name.table_Name limit 10", conn)

print(df.head())

0 votes
by (3.1k points)

Few simple steps that can be followed for the conneciton: 

Step 1: Install PyHive 

You will need to download the PyHive library and its dependencies. You can do this using pip as follows: 

pip install pyhive 

If you are using HiveServer2 and require authentication, thrift and sasl may also be required: 

pip install thrift sasl 

Step 2: Import Libraries 

In your Python script, you will need to import the following libraries 

from pyhive import hive 

Step 3: Connect 

Connect to your Hive server with host, port number, and database name. This is how you do that: 

 

# Replace these variables with your own Hive connection details 

host = 'your_hive_host' 

port = 10000  # Default port for HiveServer2 

database = 'your_database_name' 

 

# Connect 

conn = hive.Connection(host=host, port=port, database=database) 

Step 4: Create a Cursor and Execute Queries 

After this you can create a cursor, then run SQL queries. 

#create a cursor 

cursor = conn.cursor() 

 #execute a query 

cursor.execute('SELECT * FROM your_table_name LIMIT 10') 

 #fetch results 

results = cursor.fetchall() 

 #print results 

for row in results: 

  print(row) 

#Close the cursor and connection 

cursor.close() 

conn.close() 

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...