๐Ÿ“Š Data Engineering

Databricks

Databricks

A unified data analytics platform. Founded by the creators of Spark.

๐Ÿ“– Detailed Explanation

Databricks is a unified data analytics platform founded by the creators of Apache Spark. It allows data engineering, data science, machine learning, and business analytics to be performed on a single platform, and it is leading the "data lakehouse" architecture.

The core components are Unity Catalog, Delta Lake, and MLflow. Unity Catalog is responsible for metadata management and data governance. It finely controls access permissions for tables, views, and functions, and tracks data lineage. It allows multiple workspaces to access the same data while maintaining security.

Delta Lake provides ACID transactions, schema evolution, and Time Travel capabilities for data lakes. Databricks is tightly integrated with Delta Lake, so these features can be used without separate configuration. In particular, data pipelines can be defined declaratively through Delta Live Tables.

Databricks Workflows is responsible for job scheduling and orchestration. It connects notebooks, Python scripts, JAR files, and more in the form of a DAG to build complex data pipelines. It optimizes costs with automatic cluster management and provides automatic retries and notifications on failure.

๐Ÿ’ป Code Example

# Databricks ๋…ธํŠธ๋ถ ์˜ˆ์ œ: Delta Lake + Unity Catalog ํ™œ์šฉ

# 1. Unity Catalog์—์„œ ์นดํƒˆ๋กœ๊ทธ/์Šคํ‚ค๋งˆ ์„ค์ •
spark.sql("USE CATALOG main")
spark.sql("USE SCHEMA sales_analytics")

# 2. Delta Lake ํ…Œ์ด๋ธ” ์ƒ์„ฑ (Unity Catalog ๊ด€๋ฆฌ)
spark.sql("""
CREATE TABLE IF NOT EXISTS bronze_orders (
    order_id STRING,
    customer_id STRING,
    product_id STRING,
    quantity INT,
    price DECIMAL(10, 2),
    order_date TIMESTAMP,
    _ingest_timestamp TIMESTAMP
)
USING DELTA
PARTITIONED BY (DATE(order_date))
TBLPROPERTIES (
    'delta.autoOptimize.optimizeWrite' = 'true',
    'delta.autoOptimize.autoCompact' = 'true'
)
""")

# 3. Auto Loader๋กœ S3/ADLS์—์„œ ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆฌ๋ฐ ์ˆ˜์ง‘
raw_orders = (spark.readStream
    .format("cloudFiles")
    .option("cloudFiles.format", "json")
    .option("cloudFiles.schemaLocation", "/checkpoints/orders_schema")
    .load("s3://data-lake/raw/orders/"))

# ๋ณ€ํ™˜ ๋ฐ Bronze ํ…Œ์ด๋ธ”์— ์ €์žฅ
from pyspark.sql.functions import current_timestamp

bronze_stream = (raw_orders
    .withColumn("_ingest_timestamp", current_timestamp())
    .writeStream
    .format("delta")
    .outputMode("append")
    .option("checkpointLocation", "/checkpoints/bronze_orders")
    .trigger(availableNow=True)
    .toTable("bronze_orders"))

# 4. Delta Lake Time Travel - ๊ณผ๊ฑฐ ๋ฒ„์ „ ์กฐํšŒ
# ํŠน์ • ๋ฒ„์ „์œผ๋กœ ์กฐํšŒ
df_v5 = spark.read.format("delta").option("versionAsOf", 5).table("bronze_orders")

# ํŠน์ • ์‹œ์ ์œผ๋กœ ์กฐํšŒ
df_yesterday = spark.read.format("delta").option("timestampAsOf", "2024-01-15").table("bronze_orders")

# 5. Unity Catalog - ํ…Œ์ด๋ธ” ๊ถŒํ•œ ๊ด€๋ฆฌ
spark.sql("GRANT SELECT ON TABLE bronze_orders TO `data-analysts@company.com`")
spark.sql("GRANT MODIFY ON TABLE bronze_orders TO `data-engineers@company.com`")

# 6. ๋ฐ์ดํ„ฐ ๋ฆฌ๋‹ˆ์ง€ ํ™•์ธ (Unity Catalog)
# Databricks UI์—์„œ ํ…Œ์ด๋ธ” -> Lineage ํƒญ์—์„œ ์‹œ๊ฐ์ ์œผ๋กœ ํ™•์ธ ๊ฐ€๋Šฅ

print("Delta ํ…Œ์ด๋ธ” ํžˆ์Šคํ† ๋ฆฌ:")
display(spark.sql("DESCRIBE HISTORY bronze_orders"))

๐Ÿ—ฃ๏ธ How It's Said in Practice

Data Engineer: "As we adopt Unity Catalog, we need to migrate the existing Hive Metastore tables. We plan to register them as external tables first and then gradually convert them to managed tables."

Junior: "What happens to the parts of our existing notebooks that reference tables?"

Senior: "It changes to a 3-level namespace, so you need to change them to the 'catalog.schema.table' format. If you set USE CATALOG, you can minimize changes to the existing code."

Interviewer: "Do you have experience with cost optimization in Databricks?"

Candidate: "I set up auto-termination to automatically shut down idle clusters, and I reduced Interactive Cluster costs by using Job Clusters. I also enabled the Photon engine, which cut the execution time of the same workload by 40% and reduced DBU costs. Delta Lake OPTIMIZE and ZORDER were also effective in improving query performance."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "In this notebook you're using show() instead of display(), but in Databricks, display() provides richer visualizations."

Author: "I'll fix that. And would it be better to orchestrate in Workflows instead of dbutils.notebook.run()?"

โš ๏ธ Cautions

๐Ÿ”ฅ Real-World Incidents

Financial Company Case 2023

Monthly costs exploded due to continuous operation of Interactive Clusters

Cause: The development team ran a test All-Purpose Cluster 24 hours a day, with auto-termination not set

Impact: Billed 5 times the expected cost, with spending exceeding $50,000 per month

Resolution: Enforced auto-termination of 15 minutes, and switched production to Job Clusters

Lesson: Limit costs with cluster policies, set up cost alerts, and prioritize Job Clusters

E-commerce Case 2022

Data governance failure due to not adopting Unity Catalog

Cause: Duplicate data storage across workspaces, fragmented permission management, and unintegrated audit logs

Impact: Failed to submit data access history in a GDPR audit, risk of fines

Resolution: Urgently adopted Unity Catalog and migrated existing tables

Lesson: In a multi-team environment, adopt Unity Catalog from the start and tracking data lineage is essential

๐Ÿ“ Comprehension Quiz

Q1. What is the core of Databricks' "data lakehouse" architecture?

Q2. Which of the following is NOT a way to reduce costs in Databricks?

Q3. What is the main role of Unity Catalog?

๐Ÿ“š Learn More

๐Ÿ’ฌ Questions & Contributions

If this page has any errors or you would like to add something, please let us know!