Back to Apache Spark: Core
Q48Question 48 of 72

Integrating Spark with Hive

Key Concepts
  • Integration points Spark at the Hive metastore so Spark SQL can read and write Hive tables as if they were native.
  • Copy hive-site.xml (and core-site.xml, hdfs-site.xml) to Spark's conf directory for metastore connection details.
  • Build SparkSession with enableHiveSupport() to switch the catalog implementation to Hive.
  • Set spark.sql.warehouse.dir to the managed-table warehouse path for consistent table locations.
  • This gives access to Hive metastore tables, Hive UDFs, and HiveQL, while queries execute on Spark's faster engine.
  • Match the Hive metastore version with spark.sql.hive.metastore.version to avoid incompatibility.

Definition

Point Spark at the Hive metastore via hive-site.xml, turn on enableHiveSupport, set the warehouse dir, and Spark can read, write, and query Hive tables with HiveQL on Spark's engine.
  • Hive metastore – a relational database (MySQL, Postgres) storing table definitions, schemas, partitions, and storage locations; the catalog.
  • Configuration – copy hive-site.xml, core-site.xml, and hdfs-site.xml to Spark's conf directory so Spark finds the metastore connection.
  • enableHiveSupport() – switches SparkSession's catalog implementation to Hive.
  • spark.sql.warehouse.dir – sets the path for managed tables.
  • Result – Spark can query Hive tables, use Hive UDFs, and run HiveQL, with execution on Spark's Catalyst and Tungsten engine.

The National Bank already has a big catalog (the Hive metastore) listing where every data table lives and its schema. Spark does not want to rebuild that catalog; it just wants the address book. Copying hive-site.xml hands Spark that address book, enableHiveSupport tells Spark to actually use it, and the warehouse directory is the main storage room.

Understanding the Concepts

What is the Hive metastore?

The Hive metastore is a relational database that stores table definitions, schemas, partitions, and storage locations. It is the catalog; Hive's execution engine is separate. Spark can use that same catalog so it sees all existing Hive tables without redefining them.

PYTHON
# The metastore is typically MySQL or PostgreSQL
# It stores:
# - Database names and properties
# - Table names, schemas, and column types
# - Partition metadata (column=value directories)
# - Storage locations (HDFS/S3 paths)
# - SerDe (serialization/deserialization) information

How do you configure Spark to use the Hive metastore?

The configuration steps are concrete. Copy the configuration files, enable Hive support, and set the warehouse directory.

BASH
# Step 1: Copy Hive and Hadoop config files to Spark's conf directory
cp /etc/hive/conf/hive-site.xml $SPARK_HOME/conf/
cp /etc/hadoop/conf/core-site.xml $SPARK_HOME/conf/
cp /etc/hadoop/conf/hdfs-site.xml $SPARK_HOME/conf/

# hive-site.xml contains the metastore URI:
# <property>
#   <name>hive.metastore.uris</name>
#   <value>thrift://metastore.nbi.local:9083</value>
# </property>
PYTHON
from pyspark.sql import SparkSession

# Step 2: Build SparkSession with Hive support
spark = SparkSession.builder \
    .appName("NBI-Hive-Integration") \
    .config("spark.sql.warehouse.dir", "hdfs:///bank/warehouse") \
    .enableHiveSupport() \
    .getOrCreate()

# Step 3: Verify Hive is connected
print("Catalog:", spark.conf.get("spark.sql.catalogImplementation"))
print("Warehouse:", spark.conf.get("spark.sql.warehouse.dir"))
TEXT
Catalog: hive
Warehouse: hdfs:///bank/warehouse

What does Hive integration give you?

With Hive support enabled, you can query existing Hive tables, create and drop tables, read and write partitions, call Hive UDFs, and run HiveQL. Crucially, queries execute on Spark's Catalyst and Tungsten engine, not Hive's slower MapReduce.

PYTHON
import pyspark.sql.functions as F

# Query existing Hive tables directly
txn = spark.sql("SELECT * FROM bank.transactions WHERE branch = 'Mumbai'")
txn.show(3)

# List tables in the catalog
tables = spark.catalog.listTables("bank")
for t in tables:
    print(f"  {t.name}: {t.tableType}")

# Create a new Hive table
spark.sql("""
    CREATE TABLE IF NOT EXISTS bank.monthly_totals (
        branch STRING,
        month STRING,
        total DECIMAL(18,2)
    )
    STORED AS PARQUET
""")
TEXT
+----------+--------+----------+------+
|account_id|  amount|  txn_date|branch|
+----------+--------+----------+------+
|   SAV2001| 50000.0|2026-01-15|Mumbai|
|   SAV2001| 75000.0|2026-01-16|Mumbai|
|   SAV2003| 25000.0|2026-01-15|Mumbai|
+----------+--------+----------+------+

  transactions: MANAGED
  accounts: MANAGED
  dim_branches: EXTERNAL

How do you use Hive UDFs from Spark?

Hive UDFs registered in the metastore are available through spark.sql. You can also register temporary UDFs.

PYTHON
# Call a Hive UDF registered in the metastore
result = spark.sql("""
    SELECT account_id, 
           bank_udf.mask_account(account_id) as masked_id,
           amount
    FROM bank.transactions
    WHERE amount > 100000
""")
result.show(3)

# Register a temporary UDF
spark.sql("CREATE TEMPORARY FUNCTION format_inr AS 'com.nbi.udf.FormatINR'")
formatted = spark.sql("""
    SELECT account_id, format_inr(amount) as formatted_amount
    FROM bank.transactions LIMIT 5
""")
formatted.show()

How do you manage partitions through Hive?

With Hive support, Spark can read and write Hive-partitioned tables, and partition metadata is tracked in the metastore.

PYTHON
# Write partitioned data to a Hive table
txn_df = spark.read.parquet("hdfs:///bank/incoming/2026-01/")
txn_df.write.mode("append") \
    .partitionBy("branch") \
    .saveAsTable("bank.transactions")

# Query with partition pruning
mumbai = spark.sql("""
    SELECT * FROM bank.transactions 
    WHERE branch = 'Mumbai' AND txn_date = '2026-01-15'
""")
mumbai.explain()
# Shows PartitionFilters: [branch = Mumbai]

# Repair partitions (sync metastore with file system)
spark.sql("MSCK REPAIR TABLE bank.transactions")

What are the version compatibility considerations?

Match the Hive metastore version with Spark's settings to avoid incompatibility.

PYTHON
# Match metastore version
spark = SparkSession.builder \
    .appName("NBI-Hive") \
    .config("spark.sql.hive.metastore.version", "3.1.3") \
    .config("spark.sql.hive.metastore.jars", "maven") \
    .config("spark.sql.warehouse.dir", "hdfs:///bank/warehouse") \
    .enableHiveSupport() \
    .getOrCreate()

# Ensure warehouse.dir is consistent across all jobs
# so managed tables always land in the expected location

What are common mistakes?

The most common mistakes are forgetting to copy hive-site.xml, using inconsistent warehouse directories, and metastore version mismatches.

PYTHON
# WRONG: missing hive-site.xml
# Spark creates a local Derby metastore instead of connecting to shared metastore
# Tables created by other applications are invisible

# WRONG: inconsistent warehouse.dir across jobs
# Job A: warehouse.dir = hdfs:///warehouse-a/
# Job B: warehouse.dir = hdfs:///warehouse-b/
# Managed tables scatter across different locations

# WRONG: metastore version mismatch
# Spark built with Hive 2.3, metastore running Hive 3.1
# Causes: ClassNotFoundException, method signature errors

# CORRECT: all three aligned
# hive-site.xml → points to shared metastore
# warehouse.dir → consistent across all jobs
# metastore.version → matches the running metastore

What should a senior engineer know?

A senior engineer ensures the metastore version matches, the warehouse directory is consistent, and permissions are correct on the underlying storage.

PYTHON
# Senior checklist for Hive integration:
# 1. hive-site.xml in Spark's conf → shared metastore connection
# 2. enableHiveSupport() → Hive catalog implementation
# 3. spark.sql.warehouse.dir → consistent across all jobs
# 4. spark.sql.hive.metastore.version → matches running metastore
# 5. HDFS/S3 permissions → Spark user can read/write table locations
# 6. SerDe jars → available if custom SerDe is used

Visual Overview

DIAGRAM
Spark-Hive Integration:

  ┌───────────────────────┐      ┌──────────────────────────┐
  │  Spark Application    │      │  Hive Metastore          │
  │  ┌─────────────────┐  │      │  (MySQL/Postgres)        │
  │  │ SparkSession     │  │      │                          │
  │  │ enableHiveSupport│──┼──────│  Table definitions       │
  │  │                  │  │      │  Schemas, partitions     │
  │  └─────────────────┘  │      │  Storage locations       │
  │                       │      └──────────────────────────┘
  │  Reads:               │                │
  │  hive-site.xml ───────┤                │
  │  core-site.xml        │                │
  │  hdfs-site.xml        │                ▼
  │                       │      ┌──────────────────────────┐
  │  Queries execute on   │      │  HDFS / S3               │
  │  Spark's Catalyst +   │      │  warehouse.dir           │
  │  Tungsten engine      │      │  /bank/warehouse/        │
  │  (NOT Hive MapReduce) │      │    bank.db/              │
  └───────────────────────┘      │      transactions/       │
                                 │        branch=Mumbai/    │
                                 │        branch=Delhi/     │
                                 └──────────────────────────┘

Key Takeaway

Integrating Spark with Hive means pointing Spark at the Hive metastore so Spark SQL can treat Hive tables as native. The metastore is just a relational database storing table definitions, schemas, partitions, and storage locations; the catalog is separate from Hive's execution engine. The steps are concrete: copy hive-site.xml into Spark's conf directory along with core-site.xml and hdfs-site.xml, build the SparkSession with enableHiveSupport, and set spark.sql.warehouse.dir to the warehouse path.

What this gives you is access to all existing Hive metastore tables without redefining them, the ability to create, drop, read, and write Hive tables and partitions, the ability to call Hive UDFs, and the ability to run HiveQL. The crucial payoff is that although the catalog is Hive, the queries execute on Spark's Catalyst optimizer and Tungsten engine, so you get Spark's speed over Hive-cataloged data.

At the National Bank of India this was exactly how the analytics team plugged into years of existing transaction tables. Those tables were defined in a shared Hive metastore used by many teams, so rather than rebuild them, I enabled Hive support and ran our queries on Spark. Reports that other teams still ran on Hive and the ones we ran on Spark saw the same tables and the same data.

The caution I always add is to pin the metastore version with spark.sql.hive.metastore settings so there is no version mismatch, and to keep the warehouse directory consistent across jobs so managed tables never scatter.

Remember one sentence: copy hive-site.xml, enable Hive support, set the warehouse dir, and Spark can query Hive tables with Spark's fast engine while sharing the same metastore catalog.

---

Share
Use ← → arrow keys to navigate