Skip to main content

SQL on Hadoop,
with ACID tables and interactive queries.

Hive reads, writes, and manages large volumes of data in distributed storage using SQL, with ACID transactions and interactive queries through LLAP. Follow a client query all the way down to the files.

Apache Hive is a data warehouse infrastructure built on Hadoop: it defines tables over files in HDFS and turns each SQL query into a plan of stages that runs on the cluster.

HiveServer2 receives the queries, and the Hive Metastore stores the table definitions.

In SQL

Tables over distributed files

Familiar SQL syntax serves data warehouse tasks, such as extract, transform, and load, reporting, and data analysis, over data that lives in distributed storage.

Plan and engine

Each query becomes a DAG of stages

The compiler generates the plan, and the execution engine hands it to Apache Tez, Apache Spark, or MapReduce, which run it on the cluster.

ACID and LLAP

Transactions and interactive queries

Transactional tables accept inserts, updates, and deletes, and LLAP brings interactive, sub-second SQL queries.

On this page

Watch a query happen​

Press Play or step through one at a time. Underlined names lead to the Glossary.

ClientWho sends the query
ClientBeeline, JDBC, or ODBC
HiveServer2Receives, compiles, and executes
Thrift serviceReceives the queries
CompilerGenerates the plan
Execution engineExecutes the plan
Hive MetastoreTable definitions
MetastoreTable metadata
ClusterRuns the stages
Tez, Spark, or MapReduceRun the stages
HDFSWhere the files live
FilesTable and temporary files
Step 1 of 7

1 The client sends the query

A Beeline, JDBC, or ODBC client sends the SQL query to the HiveServer2 Thrift service, which serves several clients at the same time.

  1. The client sends the query. A Beeline, JDBC, or ODBC client sends the SQL query to the HiveServer2 Thrift service, which serves several clients at the same time.
  2. The Driver opens a session. The Driver creates the session handle for the query and passes it to the compiler, which parses it and performs semantic analysis on the query blocks and expressions.
  3. The compiler queries the Metastore. To generate the plan, the compiler fetches from the Metastore the metadata of the referenced tables and partitions. It uses this metadata to type-check the expressions and to prune partitions based on the query predicates.
  4. The plan is a DAG of stages. The generated plan is a DAG of stages: map or reduce jobs, metadata operations, or HDFS operations. The execution engine manages the dependencies between the stages.
  5. The stages go to the cluster. The execution engine submits the stages to the appropriate component: Apache Tez, Apache Spark, or MapReduce. In each task, the table's deserializer reads the rows from the HDFS files.
  6. The output goes to a temporary file. The serializer writes the output to a temporary file in HDFS, and the temporary files feed the next stages of the plan.
  7. The result returns to the client. For a query, the execution engine reads the contents of the temporary file directly from HDFS, as part of the Driver's fetch call, and the result returns to the client. For a DML operation, the final temporary file is moved to the table's location.
Figure 1 - A Hive query. HiveServer2 receives the query, the compiler generates a plan using the metadata from the Metastore, and the execution engine submits the stages to the cluster, which reads and writes the files in HDFS.

Architecture​

HiveServer2 receives and compiles; the Metastore describes; the cluster executes; HDFS stores.

Hive architecture: clients, HiveServer2, Hive Metastore, execution engines and HDFSThe Beeline, JDBC and ODBC clients send queries to the HiveServer2 Thrift service. Inside HiveServer2, the Driver brings together the Compiler, which queries the Hive Metastore and generates the plan, and the Execution engine, which runs the plan. The Hive Metastore has a Thrift service and a metadata database. The Execution engine submits the stages to the cluster engines, Tez, Spark or MapReduce, which run in YARN containers or, optionally, in LLAP daemons, and which read and write the table files on HDFS.ClientsBeelineJDBCODBCHiveServer2Thrift serviceServes several clients at the same timeDriverExecution engineRuns the plan,a DAG of stagesCompilerParses the queryand generates the planHive MetastoreThrift serviceTable and partitionmetadataMetadata databaseCluster: Tez, Spark or MapReduceYARN containersRun the plan tasksLLAP daemonsOptional: cache and fragment executionHDFSTable files, read and written through the SerDe
Figure 2 - Hive architecture: clients, HiveServer2 with the Driver, the Compiler, and the Execution engine, the Hive Metastore, the cluster with the YARN containers and the LLAP daemons, and HDFS. Underlined terms lead to the Glossary.

What the figure shows:

  1. The Beeline, JDBC, and ODBC clients talk to HiveServer2.
  2. The HiveServer2 Thrift service serves several clients at the same time, in TCP mode or HTTP mode.
  3. The compiler, which the Driver brings together, queries the Metastore and generates the plan.
  4. The execution engine, also brought together by the Driver, executes the plan and submits the stages to the cluster engines, Tez, Spark, or MapReduce.
  5. The Hive Metastore is a Thrift service backed by a metadata database, and it stores the structure of the tables and partitions and the location of the files in HDFS.
  6. The metadata database is part of the Metastore, together with the Thrift service.
  7. The YARN containers run the tasks of the plan.
  8. The LLAP daemons are optional and perform caching and fragment execution.
  9. HDFS stores the table files, read and written through the SerDe.

The address of a connection​

A JDBC client reaches HiveServer2 through a URL with five parts.

Click each part of the address.

Prefix

The Hive JDBC connection URL format starts with this prefix. The rest of the URL indicates the host, the port, the database, and the HiveServer2 session options.

See in the Glossary
jdbc:hive2:// — Prefix
The Hive JDBC connection URL format starts with this prefix. The rest of the URL indicates the host, the port, the database, and the HiveServer2 session options.
server — Server
The host where the HiveServer2 process runs.
:port — Port
The HiveServer2 port the client connects to.
/database — Database
The session's database: the namespace that avoids naming conflicts between tables, views, partitions, and columns.
;options — Session options
The session options, which complete the URL.

From the table to the file​

Hive data is organized in levels: each level contains the next.

Level 1 of 5

Database

A namespace that avoids naming conflicts between tables, views, partitions, and columns.

  • MetastoreStores the structure of all the tables and partitions in the warehouse: the columns and types, the serializers and deserializers needed, and the HDFS files where the data resides.
  1. Database. A namespace that avoids naming conflicts between tables, views, partitions, and columns.
    • Metastore. Stores the structure of all the tables and partitions in the warehouse: the columns and types, the serializers and deserializers needed, and the HDFS files where the data resides.
  2. Table. A homogeneous unit of data, with a single schema, analogous to a table in a relational database. The data of each table lives in its own HDFS directory.
    • Managed table. Hive manages the data: dropping the table removes both the definition and the data.
    • External table. Created over files or directories that already exist, with the location given at creation. Dropping it removes only the definition.
    • Transactional table. Supports ACID transactions: inserts, updates, and deletes, and a consistent snapshot for each query.
  3. Partition. Each table can have one or more partition keys, which determine how the data is stored. Each distinct combination of values in the partition columns gets its own data directory.
  4. Bucket. The data in each partition can be divided into buckets, by the hash of a table column. Each bucket is a file in the partition directory.
  5. File. The file where the data lives, in the storage format chosen when the table is created, such as text, ORC, Parquet, or Avro.
    • SerDe. The piece that converts the table rows into files and back. A table uses a native SerDe or a custom one.
Figure 3 - The levels of a table in Hive. The terms beside each level go with that level.

When the table is updated​

In a transactional table, changes go to delta files, and compaction merges them later.

HiveServer2Receives the update
HiveServer2Update and read
Hive MetastoreLocks and transactions
Transaction managerControls locks and transactions
CompactionBackground processes
InitiatorDiscovers what needs compaction
WorkerRuns the compaction
CleanerDeletes the old deltas
HDFSTable files
Base filesTable or partition data
Delta filesNew records, updates, and deletes
Step 1 of 6

1 The table receives an update

A table with the transactional property can receive inserts, updates, and deletes. The transaction manager controls the locks and transactions in the Metastore.

  1. The table receives an update. A table with the transactional property can receive inserts, updates, and deletes. The transaction manager controls the locks and transactions in the Metastore.
  2. Changes go to delta files. New records, updates, and deletes are written to delta files. The table or partition data lives in the base files, each in its own directory.
  3. The read combines base and delta. When a query starts, it is given a consistent snapshot of the data. At read time, Hive merges the base and delta files, applying the updates and deletes.
  4. The Initiator notices the accumulated deltas. The Initiator discovers which tables and partitions are due for compaction. One of the criteria is the number of delta directories in a table or partition, which triggers a minor compaction.
  5. A Worker compacts. Each Worker handles a single compaction task. Minor compaction rewrites a set of deltas into a single delta per bucket, and major compaction is more expensive but more effective.
  6. The Cleaner deletes the leftovers. After compaction, and once they are no longer needed, the Cleaner deletes the old delta files.
Figure 4 - A transactional table in Hive. Changes are written to delta files, reads combine them with the base files, and compaction merges the deltas until the Cleaner deletes the leftovers.

Execution engines and LLAP​

The plan belongs to Hive. A cluster engine executes the stages, and LLAP is an optional layer of daemons.

Tez, Spark, or MapReduce

The execution engine hands the stages of the plan to one of these engines, which run them on the cluster.

What Tez changes

In Tez, built on YARN, several reduce sinks are linked directly, the entire query plan is sent at once, and small datasets can be handled entirely in memory. In MapReduce, each job in the chain rereads the output of the previous job from HDFS.

LLAP daemons

LLAP daemons are long-lived, optional, stateless processes that replace direct interaction with the HDFS DataNode. Each request to a daemon carries the location and metadata of the data. On failure, any data node can process any fragment of the input.

What the daemon takes over

Caching, pre-fetching, part of the query processing, and access control move into the daemon, which executes query fragments, such as filters, projections, and transformations. The unit of data in the cache is the column-chunk.

A complement, not an engine

LLAP is not an execution engine, like MapReduce or Tez: it is aware of transactions and is a natural place to enforce access control at a finer level than per file.

Why Hive​

SQL

Distributed data, familiar syntax

Reading, writing, and managing large volumes of data in distributed storage with SQL syntax, and extending Hive with user-defined functions (UDFs).

ACID

Transactions on tables

Full ACID support for ORC tables and insert-only support for all other formats, with data compaction.

LLAP

Interactive queries

Interactive, sub-second SQL queries through a persistent query infrastructure.

HiveServer2

Several clients at the same time

HiveServer2 serves JDBC and ODBC clients with concurrency and authentication.

Optimizer

Cost-based

The Apache Calcite optimizer and execution framework handle the plan of each query.

Sources and engines

HDFS, HBase, Tez, Spark, and MapReduce

Hive accesses files in HDFS or in other systems, such as Apache HBase, runs queries through Tez, Spark, or MapReduce, and offers the HPL-SQL procedural language.

Where it comes from

Hive is a data warehouse infrastructure based on Apache Hadoop, designed to make data summarization, ad hoc querying, and analysis of large volumes easy. Its table catalog, the Hive Metastore, is now also used by other engines.

In the TDP editions​

Available in the TDP editions
EditionVersionDocumentation
TDP Datacenter4.0.0TDP Datacenter components
TDP Kubernetes · Hive Metastore4.0.0 · chart tdp-hive-metastore 3.0.3Configuration in TDP Kubernetes

In TDP Datacenter​

In TDP Datacenter, Hive is the HIVE service, installed and managed by Ambari. The Hive Metastore is a component of that service, and it has its own page.

Service components

HIVE_CLIENT (client, one or more), HIVE_METASTORE (master, one or more), HIVE_SERVER (master, one or more), HIVE_SERVER_INTERACTIVE (master, optional) and MYSQL_SERVER (master, optional, one instance). The HIVE_SERVER requires ZOOKEEPER_SERVER (ZooKeeper), TEZ_CLIENT (Tez), YARN_CLIENT (YARN), MAPREDUCE2_CLIENT (MapReduce) and ZOOKEEPER_CLIENT (ZooKeeper). The HIVE_SERVER_INTERACTIVE requires ZOOKEEPER_SERVER (ZooKeeper), YARN_CLIENT (YARN), HDFS_CLIENT (HDFS), MAPREDUCE2_CLIENT (MapReduce) and TEZ_CLIENT (Tez). Component category.

HiveServer2

The transport mode is configurable in hive.server2.transport.mode, and the ports in hive.server2.thrift.port and hive.server2.thrift.http.port, in the hive-site configuration. SSL usage is configurable in hive.server2.use.SSL.

Authentication

The mechanism is configurable in hive.server2.authentication, with dedicated properties for Kerberos, such as hive.server2.authentication.kerberos.principal, for LDAP, such as hive.server2.authentication.ldap.url, for PAM, in hive.server2.authentication.pam.services, and for a custom class, in hive.server2.custom.authentication.class.

Authorization

The Ranger plugin can be enabled in ranger-hive-plugin-enabled, in the ranger-env configuration. Authorization is configurable in hive.security.authorization.enabled and hive.security.authorization.manager. See Security in TDP Datacenter.

Execution

The cost-based optimizer is configurable in hive.cbo.enable, and vectorized execution in hive.vectorized.execution.enabled. The service memory is configurable in hive.heapsize (hive-env).

Transactional tables

The transaction manager and concurrency are configurable in hive.txn.manager and hive.support.concurrency. Compaction is configurable in hive.compactor.initiator.on and hive.compactor.worker.threads (hivemetastore-site), and the number of delta directories that triggers it in hive.compactor.delta.num.threshold.

LLAP

The LLAP configuration lives in the service's hive-interactive-site and hive-interactive-env configurations, such as hive.llap.io.enabled, hive.llap.io.memory.size, num_llap_nodes, and llap_heap_size.

Warehouse

The directories of managed and external tables are configurable in hive.metastore.warehouse.dir and hive.metastore.warehouse.external.dir.

Lineage

Sending metadata to Atlas can be enabled in hive.atlas.hook (hive-env).

Dependencies

Ambari requires in the cluster ZooKeeper, HDFS, YARN and Tez.

Required by

These services require this one in the cluster: Spark.

Used by

Trino accesses it through the Hive connector, which can be enabled in use.hive (connectors-properties). Airflow reaches it through the apache-airflow-providers-apache-hive provider, JupyterLab through the impyla library, and NiFi through the Hive components, in nifi-hive3-nar.

In TDP Kubernetes​

In TDP Kubernetes, the Hive table catalog is delivered by the Hive Metastore, installed by its own chart. Its page describes the chart, the metadata database, the warehouse, and the engines that query it.

Next steps​

Sources​

Apache Hive

Apache Hive - Design

Apache Hive - HiveServer2 Overview

Apache Hive - Hive Transactions

Apache Hive - LLAP