Skip to main content

PySpark with Snowflake, Databricks, and Hive Integration

Connecting PySpark to the Modern Data Ecosystem

At DataVerse Labs, different teams use different data stores:

  • Analytics team โ†’ Snowflake
  • Machine learning team โ†’ Databricks
  • Data warehouse team โ†’ Hive

PySpark acts as the unifying engine across these platforms, allowing teams to exchange data seamlessly.

This chapter shows you how to connect PySpark with Snowflake, Databricks, and Hive, with clean examples and real input/output.


1. Snowflake Integration with PySparkโ€‹

Snowflake is widely used for cloud analytics and BI dashboards.
PySpark integrates using the Snowflake Spark Connector.


1.1 Installing the Snowflake Connectorโ€‹

--packages net.snowflake:snowflake-jdbc:3.13.30,net.snowflake:spark-snowflake_2.12:2.12.0-spark_3.4

1.2 Reading from Snowflakeโ€‹

Example โ€” Loading Customer Table

sf_options = {
"sfURL": "account.snowflakecomputing.com",
"sfUser": "USER",
"sfPassword": "PASSWORD",
"sfDatabase": "DV_DB",
"sfSchema": "PUBLIC",
"sfWarehouse": "COMPUTE_WH"
}

df_sf = spark.read \
.format("snowflake") \
.options(**sf_options) \
.option("dbtable", "CUSTOMERS") \
.load()

df_sf.show()

Output Example

+---------+----------+--------+
|cust_id |name |country |
+---------+----------+--------+
|C101 |John Doe |USA |
|C102 |Maria Lee |Canada |
|C103 |Ishan Rao |India |
+---------+----------+--------+

1.3 Writing to Snowflakeโ€‹

df_sf.write \
.format("snowflake") \
.options(**sf_options) \
.option("dbtable", "CUSTOMERS_BACKUP") \
.mode("overwrite") \
.save()

2. Databricks Integration with PySparkโ€‹

Databricks is a managed platform for Spark with built-in:

  • Optimized runtimes
  • MLFlow
  • Delta Lake
  • Collaboration notebooks

You integrate PySpark with Databricks using:

โœ” Databricks Connect
โœ” DBFS data access
โœ” Delta Lake


2.1 Databricks Connect Setupโ€‹

Databricks Connect allows you to run PySpark from your laptop and execute on a remote cluster.

pip install databricks-connect==14.0.*

2.2 Configure:โ€‹

databricks-connect configure

You provide:

  • Workspace URL
  • Personal Access Token
  • Cluster ID

2.3 Using PySpark with Databricks Connectโ€‹

from pyspark.sql import SparkSession

spark = SparkSession.builder \
.appName("Local-to-Databricks") \
.getOrCreate()

df = spark.read.format("delta").load("/mnt/datalake/customers")
df.show()

Output Example

+---------+-----------+----------+
|cust_id |age |is_active |
+---------+-----------+----------+
|C1 |32 |true |
|C2 |41 |false |
|C3 |29 |true |
+---------+-----------+----------+

2.4 Writing to Delta Lakeโ€‹

df.write.format("delta") \
.mode("append") \
.save("/mnt/datalake/customers_new")

3. Hive Integration with PySparkโ€‹

Hive is a core warehouse in many enterprise systems.

PySpark connects to Hive using the Hive Metastore, allowing SQL queries and table management.


3.1 Enable Hive Supportโ€‹

spark = SparkSession.builder \
.appName("HiveIntegration") \
.enableHiveSupport() \
.getOrCreate()

3.2 Reading a Hive Tableโ€‹

df_hive = spark.sql("SELECT * FROM dv_db.customers")
df_hive.show()

Output Example

+---------+-------------+--------+
|cust_id |city |spend |
+---------+-------------+--------+
|C101 |New York |5600 |
|C102 |Toronto |3200 |
|C103 |Bangalore |4500 |
+---------+-------------+--------+

3.3 Writing Data to Hiveโ€‹

df_hive.write.saveAsTable("dv_db.customers_backup")

3.4 Creating External Hive Tablesโ€‹

CREATE EXTERNAL TABLE dv_db.logs_raw (
log STRING
)
LOCATION '/data/logs/raw';

4. When to Use What?โ€‹

PlatformBest For
SnowflakeBI analytics, dashboards, cost-efficient warehousing
DatabricksMachine learning, Delta Lake, advanced ETL, scalable compute
HiveLegacy warehouses, Hadoop ecosystems, batch pipelines

5. Best Practicesโ€‹

Snowflakeโ€‹

โœ” Use AUTOCOMMIT=OFF for batch jobs
โœ” Prefer COPY INTO for large writes

Databricksโ€‹

โœ” Store all data in Delta Lake
โœ” Enable auto-optimize and Z-ordering

Hiveโ€‹

โœ” Partition large tables
โœ” Use ORC/Parquet formats for storage efficiency


Summaryโ€‹

In this chapter, you learned how PySpark integrates seamlessly with:

๐Ÿ”ท Snowflakeโ€‹

  • Read/write tables
  • Use Snowflake connector

๐Ÿ”ท Databricksโ€‹

  • Connect via Databricks Connect
  • Read/write Delta Lake

๐Ÿ”ท Hiveโ€‹

  • Enable Hive support
  • Query and manage Hive tables

These integrations help enterprises like DataVerse Labs build scalable, multi-platform data pipelines.


Next Topic โ†’ Handling Semi-Structured Data (JSON, XML, Avro)