Spark · Hive · Oozie

Who talks to whom in Hive

About

This page shows the parts of Hive and how they connect. It shows two paths. In Path A, an Oozie hive2 action uses Beeline and HiveServer2 to run a query. In Path B, Spark connects directly to the Metastore service and reads the files itself. At the end, a short PySpark example gets the table description from the Metastore service and reads the data from HDFS or S3.

Select a path. Then push Next to see each step. Push a box to read about that part.

Push a box for its description
HIVE starts JDBC :10000 Thrift :9083 JDBC SQL Thrift :9083 (direct) sends job reads files reads files Ooziehive2 action BeelineSQL client HiveServer2runs queries Metastorecatalog broker Backing DBMySQL / Postgres Tezruns on YARN SparkenableHiveSupport() HDFS / S3the data files (Parquet, ORC, text)
Step 1 of 6

The parts

The full system

Hive

Hive gives SQL access to large data files. The files are in HDFS or S3.

Hive has two server parts: HiveServer2 and the Metastore service. The Metastore service keeps its catalog in a backing database.

Hive does not keep the data rows. HDFS or S3 keeps the data files.

Server · runs queries

HiveServer2

HiveServer2 is a server. It gets SQL from clients and runs it.

  • It checks the user and the permissions.
  • It gets table information from the Metastore service.
  • It runs a job on the cluster (Tez or MapReduce). For a very simple query, it reads the files without a job.
  • It sends the result to the client.
Address: jdbc:hive2://host:10000
Execution engine

Tez

Tez does the work for HiveServer2. HiveServer2 changes the SQL into a Tez job.

The job is a graph of steps (a DAG). Tez runs the steps in YARN containers. It sends data between steps without a write to HDFS.

Tez is faster than MapReduce. PySpark jobs do not use Tez.

Setting: hive.execution.engine = tez
Service · catalog broker

Metastore service

The Metastore service is a broker for the table catalog. The catalog itself is in the backing database.

Clients send Thrift requests to the service. The service reads or writes the backing database. Then it sends the answer to the client.

The service does not keep data rows. It does not run queries.

HiveServer2, Spark, Impala and Trino use this service.

Address: thrift://host:9083
Setting: hive.metastore.uris
Relational database · the catalog

Backing database

This is a normal database, for example MySQL, PostgreSQL or Oracle. People also call it the Metastore database.

This database is the catalog. For each table, it keeps the name, columns, file location, file format and partitions.

Only the Metastore service connects to it. Clients must not connect to it.

Setting: javax.jdo.option.ConnectionURL
Example: jdbc:mysql://dbhost/metastore
Protocol · not a server

Thrift

Thrift lets a program call a function on a different computer.

Thrift is not a server. It is the method that two programs use to talk on the network.

The Metastore service uses Thrift. Spark and HiveServer2 call functions such as get_table through Thrift.

The HiveServer2 JDBC driver also uses Thrift inside.

Seen as: thrift:// in an address
Client tool

Beeline

Beeline is a command-line SQL client.

It connects to HiveServer2 through JDBC. It accepts typed SQL or an .hql file.

Beeline does not run the query. HiveServer2 runs the query.

Example: beeline -u jdbc:hive2://host:10000 -f load.hql
Workflow step

Oozie hive2 action

Oozie is a workflow scheduler. A hive2 action is one step in an Oozie workflow.

The step starts a launcher job on YARN. The launcher runs Beeline. Beeline sends the .hql script to HiveServer2.

On a Kerberos cluster, the step needs a hive2 credential.

Setting: <jdbc-url> = the HiveServer2 address
Compute engine

Spark with Hive support

enableHiveSupport() connects Spark to the Metastore service.

Spark does not use HiveServer2 or Beeline. Spark gets the table information. Then Spark reads the files itself.

If hive.metastore.uris is empty, Spark uses a local Derby database. Then the cluster tables are not visible.

Temp views and spark.sql() on temp views do not need Hive.

Main setting: hive.metastore.uris
File storage

HDFS / S3

HDFS and S3 keep the data files.

The Metastore service tells tools where the files are. Spark then reads the files directly. HiveServer2 starts a Tez job that reads the files.

Remember: the backing database keeps metadata. HDFS keeps data.

Example: hdfs://namenode:8020/user/hive/warehouse/mydb.db/mytable

Which address goes where

AddressGoes toWho uses it
jdbc:hive2://host:10000HiveServer2Beeline, Oozie hive2 action, DBeaver, Tableau
thrift://host:9083Metastore serviceHiveServer2, Spark, Impala, Trino
jdbc:mysql://dbhost/metastoreBacking databaseThe Metastore service only

Useful settings

SettingWhat it doesExample value
Spark (in the Spark session or spark-defaults.conf)
spark.sql.catalogImplementationSelects the Spark catalog. enableHiveSupport() sets it to hive. Without Hive support, the value is in-memory.hive
spark.hadoop.hive.metastore.urisGives Spark the Metastore service address. Use it when hive-site.xml is not available.thrift://metastore-service-host:9083
spark.sql.warehouse.dirSets the default location for new managed tables.hdfs:///user/hive/warehouse
spark.sql.hive.metastore.versionTells Spark the Hive version of the Metastore service.3.1.3
spark.sql.hive.metastore.jarsTells Spark where to get the Hive client JARs for that version.builtin, maven or a path
spark.sql.hive.convertMetastoreParquetSpark reads Hive Parquet tables with its own reader. This is faster than the Hive SerDe.true
spark.sql.hive.convertMetastoreOrcThe same as the line above, for ORC tables.true
HiveServer2 and Tez (in hive-site.xml, or with SET in Beeline)
hive.execution.engineSelects the engine that runs the jobs for HiveServer2.tez (or mr)
tez.queue.nameSelects the YARN queue for Tez jobs.etl
hive.fetch.task.conversionLets simple queries read the files without a Tez job.more
hive.server2.thrift.portSets the HiveServer2 port for JDBC clients.10000
hive.server2.transport.modeSelects the network mode. In http mode, the port is usually 10001.binary or http
hive.server2.authenticationSelects how HiveServer2 checks users.KERBEROS or LDAP
Metastore service (in hive-site.xml)
hive.metastore.urisGives clients the Metastore service address. A list with commas gives high availability.thrift://ms1:9083,thrift://ms2:9083
hive.metastore.warehouse.dirSets the default location for managed tables./user/hive/warehouse
javax.jdo.option.ConnectionURLConnects the Metastore service to the backing database. Only the service uses it.jdbc:mysql://dbhost/metastore
hive.metastore.sasl.enabledTurns on Kerberos for Thrift calls to the Metastore service.true
hive.metastore.kerberos.principalGives the Kerberos name of the Metastore service.hive/_HOST@EXAMPLE.COM

See it in PySpark

This code gets the table description from the Metastore service. Then it reads the data files from HDFS or S3. Replace mydb.sales with a table on the cluster.

from pyspark.sql import SparkSession

spark = (SparkSession.builder
         .config("spark.hadoop.hive.metastore.uris", "thrift://metastore-service-host:9083")
         .enableHiveSupport()
         .getOrCreate())

# Step 1: Ask the Metastore service. Where are the files for this table?
loc = (spark.sql("DESCRIBE FORMATTED mydb.sales")
       .where("col_name = 'Location'").first().data_type)
print(loc)                          # hdfs://... or s3a://...

# Step 2: Read by table name. The Metastore service gives the location. Spark reads the files.
print(spark.table("mydb.sales").count())

# Step 3: Read the same files by path. This step does not use the Metastore service.
print(spark.read.parquet(loc).count())    # use .orc() if the table is ORC