๐ Data Engineering
Hudi
Apache Hudi
A data lake table format. Supports incremental processing and upserts.
Apache Hudi
A data lake table format. Supports incremental processing and upserts.
Apache Hudi (Hadoop Upserts Deletes and Incrementals) is an open-source table format that supports record-level updates, deletes, and incremental processing in data lakes. It was developed at Uber and is used in large-scale analytics workloads.
| Type | Features | Use case |
|---|---|---|
| Copy on Write (CoW) | Rewrites entire files on write | Read-oriented, batch processing |
| Merge on Read (MoR) | Writes to delta logs, merges on read | Write-oriented, real-time processing |
| Feature | Hudi | Delta Lake | Iceberg |
|---|---|---|---|
| Upsert performance | Excellent (indexing) | Good | Good |
| Incremental queries | Native support | Requires CDF | Limited |
| Streaming | Spark/Flink | Spark-centric | Flink excels |
| Indexing | Various indexes | Limited | Limited |
# PySpark๋ก Hudi ํ
์ด๋ธ Upsert
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, lit, current_timestamp
# Spark ์ธ์
์์ฑ (Hudi ํจํค์ง ํฌํจ)
spark = SparkSession.builder \
.appName("HudiUpsertExample") \
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer") \
.config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.hudi.catalog.HoodieCatalog") \
.config("spark.sql.extensions", "org.apache.spark.sql.hudi.HoodieSparkSessionExtension") \
.getOrCreate()
# Hudi ํ
์ด๋ธ ์ค์
hudi_options = {
'hoodie.table.name': 'user_events',
'hoodie.datasource.write.recordkey.field': 'event_id',
'hoodie.datasource.write.partitionpath.field': 'event_date',
'hoodie.datasource.write.precombine.field': 'updated_at',
'hoodie.datasource.write.operation': 'upsert',
'hoodie.datasource.write.table.type': 'COPY_ON_WRITE',
'hoodie.upsert.shuffle.parallelism': 100,
'hoodie.insert.shuffle.parallelism': 100,
# ์ธ๋ฑ์ฑ ์ค์ (Upsert ์ฑ๋ฅ ํฅ์)
'hoodie.index.type': 'BLOOM',
'hoodie.bloom.index.filter.dynamic.max.entries': 100000
}
# ์ด๊ธฐ ๋ฐ์ดํฐ ์ฝ์
initial_data = [
('evt_001', 'user_1', 'click', '2024-01-15', '2024-01-15 10:00:00'),
('evt_002', 'user_2', 'purchase', '2024-01-15', '2024-01-15 11:00:00'),
('evt_003', 'user_1', 'view', '2024-01-15', '2024-01-15 12:00:00')
]
df_initial = spark.createDataFrame(
initial_data,
['event_id', 'user_id', 'event_type', 'event_date', 'updated_at']
)
# Hudi ํ
์ด๋ธ์ ์ฐ๊ธฐ
df_initial.write \
.format('hudi') \
.options(**hudi_options) \
.mode('overwrite') \
.save('/data/hudi/user_events')
print("์ด๊ธฐ ๋ฐ์ดํฐ ์ฝ์
์๋ฃ")
# Upsert ๋ฐ์ดํฐ (๊ธฐ์กด ๋ ์ฝ๋ ์
๋ฐ์ดํธ + ์ ๊ท ์ฝ์
)
upsert_data = [
# ๊ธฐ์กด ๋ ์ฝ๋ ์
๋ฐ์ดํธ (event_type ๋ณ๊ฒฝ)
('evt_001', 'user_1', 'purchase', '2024-01-15', '2024-01-15 14:00:00'),
# ์ ๊ท ๋ ์ฝ๋ ์ฝ์
('evt_004', 'user_3', 'signup', '2024-01-15', '2024-01-15 15:00:00'),
('evt_005', 'user_2', 'view', '2024-01-15', '2024-01-15 16:00:00')
]
df_upsert = spark.createDataFrame(
upsert_data,
['event_id', 'user_id', 'event_type', 'event_date', 'updated_at']
)
# Upsert ์คํ
df_upsert.write \
.format('hudi') \
.options(**hudi_options) \
.mode('append') \
.save('/data/hudi/user_events')
print("Upsert ์๋ฃ")
# ๊ฒฐ๊ณผ ํ์ธ
df_result = spark.read.format('hudi').load('/data/hudi/user_events')
df_result.show()
# Hudi ์ฆ๋ถ ์ฟผ๋ฆฌ - ๋ณ๊ฒฝ๋ ๋ฐ์ดํฐ๋ง ์กฐํ
from datetime import datetime, timedelta
# ์ต๊ทผ ์ปค๋ฐ ํ์์คํฌํ ์กฐํ
commits_df = spark.read \
.format('hudi') \
.load('/data/hudi/user_events') \
.select('_hoodie_commit_time') \
.distinct() \
.orderBy('_hoodie_commit_time')
commits_df.show()
# ํน์ ์์ ์ดํ ๋ณ๊ฒฝ ๋ฐ์ดํฐ ์กฐํ (์ฆ๋ถ ์ฟผ๋ฆฌ)
begin_time = '20240115100000' # yyyyMMddHHmmss ํ์
incremental_df = spark.read \
.format('hudi') \
.option('hoodie.datasource.query.type', 'incremental') \
.option('hoodie.datasource.read.begin.instanttime', begin_time) \
.load('/data/hudi/user_events')
print(f"{begin_time} ์ดํ ๋ณ๊ฒฝ๋ ๋ ์ฝ๋:")
incremental_df.show()
# ์ค์๊ฐ ํ์ดํ๋ผ์ธ์์ ์ฆ๋ถ ์ฒ๋ฆฌ
def process_incremental_batch(last_commit_time):
"""๋ง์ง๋ง ์ฒ๋ฆฌ ์์ ์ดํ ๋ฐ์ดํฐ๋ง ์ฒ๋ฆฌ"""
incremental_df = spark.read \
.format('hudi') \
.option('hoodie.datasource.query.type', 'incremental') \
.option('hoodie.datasource.read.begin.instanttime', last_commit_time) \
.load('/data/hudi/user_events')
if incremental_df.count() > 0:
# ๋น์ฆ๋์ค ๋ก์ง ์ฒ๋ฆฌ
processed_df = incremental_df.groupBy('user_id') \
.count() \
.withColumnRenamed('count', 'event_count')
# ๊ฒฐ๊ณผ ์ ์ฅ ๋๋ ์ ์ก
processed_df.write.mode('append').parquet('/data/processed/events')
# ์๋ก์ด ์ปค๋ฐ ํ์์คํฌํ ๋ฐํ
new_commit_time = incremental_df.select(
max('_hoodie_commit_time')
).collect()[0][0]
return new_commit_time
return last_commit_time
# Merge on Read ํ
์ด๋ธ ์ค์
mor_options = {
'hoodie.table.name': 'realtime_events',
'hoodie.datasource.write.recordkey.field': 'event_id',
'hoodie.datasource.write.partitionpath.field': 'event_date',
'hoodie.datasource.write.precombine.field': 'event_timestamp',
'hoodie.datasource.write.operation': 'upsert',
'hoodie.datasource.write.table.type': 'MERGE_ON_READ',
# ๋น๋๊ธฐ ์ปดํฉ์
์ค์
'hoodie.compact.inline': 'false',
'hoodie.compact.inline.max.delta.commits': 5,
# ํด๋ฆฌ๋ ์ค์
'hoodie.cleaner.policy': 'KEEP_LATEST_COMMITS',
'hoodie.cleaner.commits.retained': 10
}
# MoR ํ
์ด๋ธ์ ์คํธ๋ฆฌ๋ฐ ๋ฐ์ดํฐ ์ฐ๊ธฐ
streaming_df.writeStream \
.format('hudi') \
.options(**mor_options) \
.option('checkpointLocation', '/checkpoints/realtime_events') \
.outputMode('append') \
.start('/data/hudi/realtime_events')
# MoR ํ
์ด๋ธ ์ฝ๊ธฐ - Snapshot Query (๋ณํฉ๋ ์ต์ ๋ฐ์ดํฐ)
snapshot_df = spark.read \
.format('hudi') \
.option('hoodie.datasource.query.type', 'snapshot') \
.load('/data/hudi/realtime_events')
# MoR ํ
์ด๋ธ ์ฝ๊ธฐ - Read Optimized Query (์ปดํฉ์
๋ ๋ฐ์ดํฐ๋ง)
read_optimized_df = spark.read \
.format('hudi') \
.option('hoodie.datasource.query.type', 'read_optimized') \
.load('/data/hudi/realtime_events')
# ์๋ ์ปดํฉ์
์คํ
from hudi.spark import HoodieSparkClient
hudi_client = HoodieSparkClient(spark, '/data/hudi/realtime_events')
hudi_client.schedule_compaction()
hudi_client.run_compaction()
-- Hudi ํ
์ด๋ธ ์์ฑ (Spark SQL)
CREATE TABLE user_profiles (
user_id STRING,
name STRING,
email STRING,
status STRING,
updated_at TIMESTAMP
) USING hudi
TBLPROPERTIES (
'hoodie.table.name' = 'user_profiles',
'hoodie.datasource.write.recordkey.field' = 'user_id',
'hoodie.datasource.write.precombine.field' = 'updated_at',
'hoodie.datasource.write.operation' = 'upsert',
'type' = 'cow'
)
PARTITIONED BY (status)
LOCATION '/data/hudi/user_profiles';
-- Upsert (MERGE INTO)
MERGE INTO user_profiles AS target
USING source_updates AS source
ON target.user_id = source.user_id
WHEN MATCHED THEN
UPDATE SET
name = source.name,
email = source.email,
status = source.status,
updated_at = source.updated_at
WHEN NOT MATCHED THEN
INSERT (user_id, name, email, status, updated_at)
VALUES (source.user_id, source.name, source.email, source.status, source.updated_at);
-- ์ญ์
DELETE FROM user_profiles WHERE status = 'deleted';
-- Time Travel ์ฟผ๋ฆฌ
SELECT * FROM user_profiles TIMESTAMP AS OF '2024-01-15 10:00:00';
-- ์ปค๋ฐ ์ด๋ ฅ ์กฐํ
CALL show_commits('user_profiles', 10);