Working with Hive using AWS S3 and Python (2023)

Beginner’s guide to maintaining a Hive schema with external data storage and executing Hive queries using Python

Working with Hive using AWS S3 and Python (1)

Published in

Towards Data Science


6 min read


Dec 30, 2019


Working with Hive using AWS S3 and Python (3)

In this article, I’m going to share my experience of maintaining a Hive schema. This will be useful to the freshers who are willing to step into Big Data technologies. Mainly this will describe how to connect to Hive using python and How to use AWS S3 as the data storage. If you are not familiar with Hive concepts please go through the article on Wikipedia.

The main objective of this article is to provide a guide to connect Hive through python and execute queries. I’m using “Pyhive” library for that. I’m creating my connection class as “HiveConnection” and Hive queries will be passed into the functions. AWS S3 will be used as the file storage for Hive tables.

import pandas as pd
pyhive import hive
class HiveConnection:
def select_query(query_str: str, database:str =HIVE_SCHEMA) -> pd.DataFrame:
Execute a select query which returns a result set
:param query_str: select query to be executed
:param database: Hive Schema

conn = hive.Connection(host=HIVE_URL, port=HIVE_PORT, database=database, username=HIVE_USER)

result = pd.read_sql(query_str, conn)
return result

def execute_query(query_str: str, database: str=HIVE_SCHEMA):
execute an query which does not return a result set.
:param query_str: Hive query to be executed
:param database: Hive Schema

conn = hive.Connection(host=HIVE_URL, port=HIVE_PORT, database=database, username=HIVE_USER)
cur = conn.cursor()
# Make sure to set the staging default to HDFS to avoid some potential S3 related errors
cur.execute("SET hive.exec.stagingdir=/tmp/hive/")
cur.execute("SET hive.exec.scratchdir=/tmp/hive/")
return "SUCCESS"

I’m keeping the queries as separated strings. This way you can format the queries with external parameters when necessary. Hive configurations (HIVE_URL, HIVE_PORT, HIVE_USER, HIVE_SCHEMA) as constants. Function “select_query” will be use to retrieve data and function “execute_query” will be used for other queries.

Hive provides a shell interactive tool to initiate databases, tables and manipulate the data in tables. We can go into the Hive command line by typing command “hive”. You can execute all the queries given in this article in the shell also.

Schema is a collection of tables which is similar to a database. Both keywords SCHEMA and DATABASE are allowed in Hive. We can pick either. Here we use SCHEMA instead of DATABASE. Schema can be created with “CREATE SCHEMA”. To go inside the schema, the keyword “USE” is available.

USE userdb;

There are three types of Hive tables. They are Internal, External and Temporary. Internal tables store metadata of the table inside the database as well as the table data. But external tables store metadata inside the database while table data is stored in a remote location like AWS S3 and hdfs. When dropping an internal table, all the table data will be erased with the metadata. When dropping an external table, only the metadata will be erased; not the table data. In this way, actual data will be protected. If you point a new table to the same location, data will be visible through the new table.

Hive is a data warehouse and uses MapReduce Framework. So the speed of the data retrieving may not fair enough for small queries. Hive tables can be partitioned in order to increase the performance. Partitioning technique can be applied to both external and internal tables. Concepts like bucketing are also there. You can choose any of these techniques to enhance performance.

Temporary tables are useful when copying data from one place to another. It acts as a temporary location to hold the data within a database session. All the temporary tables are cleared after the session timeout. Creating a temporary table is not useful with “Pyhive” library as multiple queries are not supported in a single session. Even though we created a table, the same session will no longer be available to access the table. But this is possible in the Hive command line. You can create a temporary table and then select data from that table in a single session.

Internal Tables

The following query is to create an internal table with a remote data storage, AWS S3. The file format is CSV and field are terminated by a comma. “s3_location” points to the S3 directory where the data files are. This is a user-defined external parameter for the query string. It should be passed in the time of query formatting.

CREATE TABLE `user_info` (
`business_unit` INT,
`employee_id` INT,
`email` VARCHAR(250),
`first_name` VARCHAR(250),
`last_name` VARCHAR(250),
`gender` VARCHAR(250),
`birthday` DATE,
`created_date` DATE,
`updated_date` DATE
LOCATION '{s3_location}'
"s3select.format" = "csv",
"skip.header.line.count" = "1"

If the data strings contain commas, it will break the table structure. So I have defined an escape character and all the unnecessary commas needed to be preceded by this escape character before creating the table.

Following is an example record. Note that email contains a comma.


above record need to be formatted like this :

External Tables

Here, I have partitioned “user_info” table with “business_unit” and “created_date”

`employee_id` INT,
`email` VARCHAR(250),
`first_name` VARCHAR(250),
`last_name` VARCHAR(250),
`gender` VARCHAR(250),
`birthday` DATE,
`updated_date` DATE
) partitioned by(
`business_unit` INT,
`created_date` DATE,
LOCATION '{s3_location}'
"s3select.format" = "csv",
"s3select.headerInfo" = "ignore"

Temporary table

Query for creating a temporary table.

`business_unit` INT,
`employee_id` VARCHAR(250),
`email` VARCHAR(250),
`first_name` VARCHAR(250),
`last_name` VARCHAR(250),
`gender` VARCHAR(250),
`birthday` DATE,
`created_date` DATE,
`updated_date` DATE
) ;

Drop Table

Query to drop a table. If you are dropping an external table data in remote file storage will not be erased.


Insert data

Once the table is created with an external file storage, data in the remote location will be visible through a table with no partition. But this is not true when it comes to a table with partitions. Which means data can not be directly copied into a partitioned table. We need to create a temporary table with no partition and insert data into the partitioned table by providing the partition values. The following query describes how to insert records to such a table.

INSERT INTO TABLE user_static_info PARTITION (business_unit={business_unit}, `created_date`='{execution_date}')
signup_channel ,
signup_from_fb ,
WHERE business_id={business_unit}

Since “Pyhive” is not supported for multiple queries in a single session; I had to create the internal table “tmp_user_static_info” which points to S3 data directory without partitions. Then it was dropped after inserting data to the external, partitioned table.

Retrieve data

SELECT queries are used to retrieve data in Hive. These are much similar to SQL SELECT queries. It has the following form. You can build the query for your requirements.

SELECT [ALL | DISTINCT] select_expr, select_expr, …
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[HAVING having_condition]
[CLUSTER BY col_list | [DISTRIBUTE BY col_list] [SORT BY col_list]]
[LIMIT number];

Update and Delete data

Hive does not support UPDATE and DELETE data directly. If you want to change anything from a table; copy the necessary data to a new table with SELECT queries. Then you can replace the old table with a new table by dropping the old table and renaming the new table.

Alter tables

Table alterations are possible in Hive. But this needs to be done very carefully without affecting the existing data. Because we can’t alter the data. As an example, adding a new field in the middle will not shift data. If we add a new field as the second field, data that belong to the third column will still appear in the second column and fourth field data in the 3rd field and so on. The last field will not contain any data. This is because of the restriction of updating hive table data. If we added a new field as the last field, there will be an empty field and we can insert data into that field.

ALTER TABLE user_static_info ADD COLUMNS (last_sign_in DATE);

If we want to drop external data we can use the following steps.

DROP TABLE user_static_info;

Finally, the following code shows how to execute a query using “execute_query” function in “HiveConnection” class.

from src.database.hive_con import HiveConnection

create_temp_table_query_str = """CREATE TABLE `user_info` (
`business_unit` INT,
`employee_id` INT,
`email` VARCHAR(250),
`first_name` VARCHAR(250),
`last_name` VARCHAR(250),
`gender` VARCHAR(250),
`birthday` DATE,
`created_date` DATE,
`updated_date` DATE
LOCATION '{s3_location}'
"s3select.format" = "csv",
"skip.header.line.count" = "1"

HiveConnection.execute_query(query_str=create_temp_table_query_str, database=userdb)


Can Hive work with S3? ›

The Hive connector can read and write tables that are stored in Amazon S3 or S3-compatible systems. This is accomplished by having a table or database location that uses an S3 prefix, rather than an HDFS prefix.

Can you use Python with Hive? ›

In the era of BigData, Hive is an invaluable tool and having it in conjunction with flexible programming like Python gives you an extra layer of manoeuvrability when building your next BigData project.

How to pull data from aws S3 using python? ›

How to Upload And Download Files From AWS S3 Using Python (2022)
  1. Step 1: Setup an account. ...
  2. Step 2: Create a user. ...
  3. Step 3: Create a bucket. ...
  4. Step 4: Create a policy and add it to your user. ...
  5. Step 5: Download AWS CLI and configure your user. ...
  6. Step 6: Upload your files. ...
  7. Step 7: Check if authentication is working.
Jan 20, 2022

How to connect to Hive server from Python? ›

1 Answer
  1. from pyhive import hive.
  2. import pandas as pd.
  3. #Create Hive connection.
  4. conn = hive.Connection(host="", port=10000, username="username")
  5. # Read Hive table and Create pandas dataframe.
  6. df = pd.read_sql("SELECT * FROM db_Name.table_Name limit 10", conn)
  7. print(df.head())

Why is Hive discontinued? ›

As a smart tech brand in the middle of a climate crisis, we know our focus needs to change,” the page reads, “So we've made the tough decision to discontinue our smart security and leak detection products – and develop smart home tech that'll get us closer to Net Zero.”

How do I create a Hive table on S3? ›

To create a Hive table on top of those files, you have to specify the structure of the files by giving columns names and types. CREATE EXTERNAL TABLE posts (title STRING, comment_count INT) LOCATION 's3://my-bucket/files/'; Here is a list of all types allowed.

What is the main disadvantage of Hive? ›

Limitation of Hive

It does not offer real-time queries for row-level updates. The latency in the apache hive query is very high. Hive only supported online analytical processing (OLAP) and doesn't support online transaction processing (OLTP). Hive Query Language doesn't support the transaction processing feature.

Is Hive outdated? ›

While Hive as a query engine is mostly dead today, having been replaced by several other more advanced technologies, the key concepts it brought with it are very much still with us today.

How do I run a Python script in S3? ›

User Guide
  1. Step 1: Create an IAM service role for a hybrid and multicloud environment.
  2. Step 2: Create a hybrid activation for a hybrid and multicloud environment.
  3. Step 3: Install SSM Agent for a hybrid and multicloud environment (Linux)
  4. Step 4: Install SSM Agent for a hybrid and multicloud environment (Windows)

How to use AWS S3 Sync in Python? ›

AWS CLI Installation and Configuration
  1. Setup your Programmatic Access – Create Access Key.
  2. Install AWS CLI.
  3. Configure AWS CLI.
  4. AWS S3 sync Local to S3 bucket - Copying/Uploading files.
  5. Sync S3 bucket to Local - Downloading Files from S3 to local.
  6. Sync and delete the extra files on the S3 destination.
Jun 21, 2022

How to read CSV file from S3 bucket using Python? ›

  1. # Create the S3 object.
  2. obj = client. get_object(
  3. Bucket = 'sql-server-shack-demo-1',
  4. Key = 'sql-shack-demo.csv'
  5. )
  6. # Read data from the S3 object.
  7. data = pandas. read_csv(obj['Body'])
  8. # Print the data frame.
Jul 31, 2020

How do I write a Hive query in Python? ›

  1. Install PyHive and Dependancies. Before we can query Hive using Python, we have to install the PyHive module and associated dependancies. ...
  2. Create Python Script. ...
  3. Start Hortonworks HDP Sandbox. ...
  4. Run Python Script.
Apr 24, 2017

How to read data from Hive using Python? ›

To load data from Hive in Python, there are several approaches:
  1. Use PySpark with Hive enabled to directly load data from Hive databases using Spark SQL: Read Data from Hive in Spark 1. x and 2. x.
  2. Use ODBC or JDBC Hive drivers. Cloudera has implemented ODBC drivers for Hive and Impala.
  3. Native Python libraries.
Jan 6, 2021

How do I connect to Hive using PySpark? ›

  1. Step 1 – Import PySpark.
  2. Step 2 – Create SparkSession with Hive enabled.
  3. Step 3 – Read Hive table into Spark DataFrame using spark.sql()
  4. Step 4 – Read using
  5. Step 5 – Connect to remove Hive.
Apr 24, 2023

Is Hive better than Nest? ›

Nest and Hive Features Compared

In terms of the number of features, there's no doubt that Nest wins hands down, but some may question whether all of them are necessary. Some may even find the Hive's shorter list of features means the learning curve is less steep and it has everything they're looking for.

Is Hive owned by Amazon? ›

Hive is a trademark owned by Centrica Hive Limited that produces smart home devices.

Is there an alternative to Hive? ›

Basecamp. If you're looking for a great Hive alternative that also offers both people and product management, Basecamp is another great option. This software solution offers tools that help reduce the complexities of a typical project management methodology.

Can we create tables in S3? ›

On the Create table page, in the Source section, do the following: For Create table from, select Amazon S3. For Select S3 path, enter a URI pointing to the Amazon S3 data in the format s3:// BUCKET_NAME / PATH .

How to convert CSV to Hive table? ›

  1. Step 1: Import the modules. In this scenario, we are going to import the pyspark and pyspark SQL modules and also specify the app name as below: ...
  2. Step 2: Create Spark Session. ...
  3. Step 3: Verify the databases. ...
  4. Step 4: Read CSV File and Write to Table. ...
  5. Step 5: Fetch the rows from the table. ...
  6. Step 6: Print the schema of the table.
Jan 19, 2023

How do I create a table in Hive from PySpark? ›

Following are the Steps to Save PySpark DataFrame to Hive Table.
  1. Step 1 – Create SparkSession with hive enabled.
  2. Step 2 – Create PySpark DataFrame.
  3. Step 3 – Save PySpark DataFrame to Hive table.
  4. Step 4 – Confirm Hive table is created.
Apr 24, 2023

Why hive is better than SQL? ›

Hive writes and queries data in HDFS. SQL requires multiple reads and writes. Hive is better for analyzing complex data sets. SQL is better for analyzing less complicated data sets very quickly.

What is the maximum data size hive can handle? ›

The maximum size of a string data type supported by Hive is 2 GB. Hive supports the text file format by default, and it also supports the binary format sequence files, ORC files, Avro data files, and Parquet files.

What is the life span of hive? ›

Honey beehives can last indefinitely if conditions are right for them to continue to grow in the spring and summer and hibernate in the winter, intact. If there is a beehive that needs to be removed around your home, contact a trained beekeeper or the experienced pest control experts at Petri Pest Control Services.

What is the difference between Hadoop and Hive? ›

Hadoop is a framework to process/query the Big data, while Hive is an SQL Based tool that builds over Hadoop to process the data. Hive process/queries all the data using HQL (Hive Query Language). It's SQL-Like Language, while Hadoop can understand Map Reduce only.

What is the difference between Hive and relational database? ›

It differs from a relational database in a way that it stores schema in a database and processed data into HDFS. For processing, Hive provides a SQL-like interface to query data stored in various databases and file systems that integrate with Hadoop.

Where does the data of a Hive table gets stored? ›

Hive stores its database and table metadata in a metastore, which is a database or file backed store that enables easy data abstraction and discovery.

Is the Hive getting shut down? ›

A free decryptor for files encrypted with the Hive ransomware was released by a South Korean cybersecurity agency in the summer of 2022. UPDATE: The US Department of Justice has confirmed dismantling the Hive ransomware operation.

Is Facebook using Hive? ›

Conclusion: Facebook designed hive for data analysis and bring out insights quickly, not putting more time on writing programs on data. Key points: Let us map the functionalities of Hive to Map Reduce.

What language does Hive use? ›

Hive provides an SQL dialect, called Hive Query Language (abbreviated HiveQL or just HQL) for querying data stored in a Hadoop cluster. SQL knowledge is widespread for a reason; it's an effective, reasonably intuitive model for organizing and using data.

Can I run Python on AWS? ›

The AWS SDK for Python (Boto3) enables you to use Python code to interact with AWS services like Amazon S3. For example, you can use the SDK to create an Amazon S3 bucket, list your available buckets, and then delete the bucket you just created.

How to read and write in S3 bucket using Python? ›

Create a s3 client. Read a csv file from local filesystem that has to be moved to s3 bucket. Now send the put_object request to write the file on s3 bucket. Check s3 bucket on s2 management console to validate if file is uploaded.

How to list all files in S3 bucket Python? ›

Follow the below steps to list the contents from the S3 Bucket using the boto3 client.
  1. Create Boto3 session using boto3.session() method.
  2. Create the boto3 s3 client using the boto3. ...
  3. Invoke the list_objects_v2() method with the bucket name to list all the objects in the S3 bucket.
Oct 12, 2021

Why is boto3 used? ›

The AWS SDK for Python (Boto3) provides a Python API for AWS infrastructure services. Using the SDK for Python, you can build applications on top of Amazon S3, Amazon EC2, Amazon DynamoDB, and more.

How do I read data from aws S3 using PySpark? ›

1. Spark read a text file from S3 into RDD
  1. 1.1 textFile() – Read text file from S3 into RDD. sparkContext. ...
  2. 1.3 Reading multiple files at a time. ...
  3. 1.4 Read all text files matching a pattern. ...
  4. 1.5 Read files from multiple directories on S3 bucket into single RDD.
Jan 29, 2023

How do I connect my S3 to PySpark? ›

Some of this information can be found if you log in to the AWS Redshift console and look at the JDBC URL.
  1. Step 1: Install required packages. ...
  2. Step 2: Configure Spark with Python (PySpark) ...
  3. Step 3: Configuration for Connecting PySpark to AWS. ...
  4. Step4: Setup AWS Credentials. ...
  5. Step 5: Validate Reading Parquet Files from S3.
Jul 18, 2022

How to put CSV file in S3 using Python? ›

  1. Writing csv file to Amazon S3 using python.
  2. using boto to upload csv file into Amazon S3 bucket.
  3. CSV file upload from buffer to S3.
  4. saving csv file to s3 using boto3.
  5. Python: Read CSV from S3 bucket with `import csv`
  6. Django: Uploading a CSV file to AWS S3.

Can pandas write directly to S3? ›

Pandas (starting with version 1.2. 0) supports the ability to read and write files stored in S3 using the s3fs Python package. S3Fs is a Pythonic file interface to S3.

How do I connect my S3 to boto3? ›

Installation of boto3 and Building AWS S3 Client
  1. Installing boto3 to your application: On the Terminal, use the code. ...
  2. Build an S3 client to access the service methods: ...
  3. Listing buckets: ...
  4. Deleting Buckets: ...
  5. Listing the files from a bucket: ...
  6. Uploading files: ...
  7. Downloading files: ...
  8. Deleting files:
Dec 5, 2022

How to connect to Hive using JDBC in Python? ›

  1. Cursors support with. from hivejdbc import connect conn = connect('', database='default') with conn. ...
  2. Cursors are iterable. from hivejdbc import connect conn = connect('', database='default') cursor = conn. ...
  3. Username and Password.
Dec 21, 2020

How to query JSON data in Hive? ›

  1. Load JSON file from your home folder in HDFS to Hive.
  2. Login to the web console. ...
  3. Copy /data/sample_json from HDFS to your home folder in HDFS. $ ...
  4. Check the content of the sample_json directory. ...
  5. It contains the file user_country. ...
  6. Launch Hive by typing hive in the web console. ...
  7. Use your database.

How to connect Hive using pandas? ›

Connecting to Hive Data

Create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the create_engine function. Set the Server, Port, TransportMode, and AuthScheme connection properties to connect to Hive.

Can we use Hive on S3? ›

With Amazon EMR release version 5.18. 0 and later, you can use S3 Select with Hive on Amazon EMR. S3 Select allows applications to retrieve only a subset of data from an object.

How to fetch data from Hive? ›

To list out the databases in Hive warehouse, enter the command 'show databases'. The database creates in a default location of the Hive warehouse. In Cloudera, Hive database store in a /user/hive/warehouse. Copy the input data to HDFS from local by using the copy From Local command.

What is the difference between Hive and PySpark? ›

Hive uses HQL, while Spark uses SQL as the language for querying the data. Access rights is another difference between the two tools with Hive offering access rights and grouping the users as per their roles. However, no such option is present in Spark SQL.

How do I connect to Hive from Jupyter? ›

Connect to Hive
  1. Put the cluster authentication file user.keytab to the jupyter notebook host's /opt directory for Hive authentication, and put the authentication-related krb5.conf file to the /etc/ path.
  2. Create a jaas.conf configuration file in the jupyter notebook host /opt directory, the content is as follows:

What is PySpark in Python? ›

PySpark is the Python API for Apache Spark. It enables you to perform real-time, large-scale data processing in a distributed environment using Python. It also provides a PySpark shell for interactively analyzing your data.

Does Hadoop support S3? ›

Introducing the Hadoop S3A client.

Hadoop's “S3A” client offers high-performance IO against Amazon S3 object store and compatible implementations. Directly reads and writes S3 objects. Compatible with standard S3 clients.

Is S3 Hadoop compatible? ›

While Apache Hadoop has traditionally worked with HDFS, S3 also meets Hadoop's file system requirements. Companies such as Netflix have used this compatibility to build Hadoop data warehouses that store information in S3, rather than HDFS.

Is S3 HDFS compatible? ›

HDFS and the EMR File System (EMRFS), which uses Amazon S3, are both compatible with Amazon EMR, but they're not interchangeable. HDFS is an implementation of the Hadoop FileSystem API, which models POSIX file system behavior.

Which is not supported by Hive? ›

Hive does not support all schemas, and has some limitations on column names, notably: It does not preserve case, so some columns names can conflict.

Is HDFS faster than S3? ›

S3 is an order of magnitude slower than HDFS, but the speed of its various operations remains stable, and the total TPS grows as the number of concurrent operations increases.

How to transfer data from S3 to HDFS? ›

For JAR location, enter command-runner.

For more information, see Run commands and scripts on an Amazon EMR cluster. For Arguments, enter options similar to the following: s3-dist-cp --src=s3://s3distcp-source/input-data --dest=hdfs:///output-folder1. For Action on failure, choose Continue.

Can S3 be used for big data? ›

Scalability and support for structured, semi-structured, and unstructured data – Amazon S3 is a petabyte scale object store which provides virtually unlimited scalability to store any type of data.

Why S3 Cannot be used as database? ›

PRO TIP: No, AWS S3 is not a database. It is a simple storage service that stores files and folders in the cloud. It cannot be used to store structured data or run database queries. Because AWS S3 can be used to store data in a centralized location, it can make it easier to manage and store data.

Will Apache spark replace Hadoop? ›

Hadoop excels over Apache Spark in some business applications, but when processing speed and ease of use is taken into account, Apache Spark has its own advantages that make it unique. The most important thing to note is, neither of these two can replace each other.

When should we not use Hadoop framework? ›

Hadoop framework is not recommended for small-structured datasets as you have other tools available in market which can do this work quite easily and at a fast pace than Hadoop like MS Excel, RDBMS etc. For a small data analytics, Hadoop can be costlier than other tools.

Can you run code on S3? ›

You can run it. It will execute your script from S3 on EC2 instance. You can find the working sample here: Ping me on @ljmocic if you have any questions!

Can you use S3 as a database? ›

AWS S3 is a key-value store, one of the major categories of NoSQL databases used for accumulating voluminous, mutating, unstructured, or semistructured data. Uploaded objects are referenced by a unique key, which can be any string.

What is S3 compatible API? ›

The term S3-compatible means the data store uses the S3 API to communicate with any connected device or system. It originally referred to data stored in a public cloud; however, S3-compatible storage has extended to on-premises and private cloud deployments.

Is Hive an ETL or not? ›

Hive is a powerful tool for ETL, data warehousing for Hadoop, and a database for Hadoop. It is, however, relatively slow compared with traditional databases.

Is the Hive shutting down? ›

A free decryptor for files encrypted with the Hive ransomware was released by a South Korean cybersecurity agency in the summer of 2022. UPDATE: The US Department of Justice has confirmed dismantling the Hive ransomware operation.


Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated: 18/11/2023

Views: 6292

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.