๐Ÿ“Š Data Engineering

Apache Iceberg

Apache Iceberg

A table format for large-scale analytic datasets. Schema evolution.

Detailed Explanation

Apache Iceberg is an open table format developed at Netflix that provides high-performance table management for large-scale analytic datasets. Overcoming the limitations of traditional Hive tables, it supports modern data lakehouse features such as ACID transactions, schema evolution, partition evolution, and time travel.

The core of Iceberg is its metadata layer, which guarantees data consistency through snapshot-based versioning. Each snapshot represents the complete state of a table, and manifest files efficiently manage the locations and statistics of data files. Thanks to this structure, query planning is possible without file listing, greatly improving performance on object storage such as S3.

Schema Evolution is one of Iceberg's powerful features, allowing you to add, drop, rename, and change the type of columns without rewriting data. Partition Evolution likewise lets you change the partitioning strategy without touching existing data, minimizing the operational burden of data structure changes.

Iceberg is compatible with a variety of compute engines such as Spark, Flink, Trino, Presto, Hive, and Dremio, and multiple engines can read and write the same table at the same time. It is natively supported by the major cloud services of AWS, Azure, and GCP, and has become a key component of data lakehouse architecture.

Code Example

# Apache Iceberg ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋ฐ ๊ด€๋ฆฌ (PySpark)
from pyspark.sql import SparkSession
from pyspark.sql.types import *
from pyspark.sql.functions import col, lit, current_timestamp

# Spark ์„ธ์…˜ ์„ค์ • (Iceberg ์นดํƒˆ๋กœ๊ทธ ํฌํ•จ)
spark = SparkSession.builder \
    .appName("IcebergExample") \
    .config("spark.sql.extensions",
            "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions") \
    .config("spark.sql.catalog.iceberg_catalog",
            "org.apache.iceberg.spark.SparkCatalog") \
    .config("spark.sql.catalog.iceberg_catalog.type", "hadoop") \
    .config("spark.sql.catalog.iceberg_catalog.warehouse",
            "s3://data-lake/iceberg/warehouse") \
    .config("spark.sql.defaultCatalog", "iceberg_catalog") \
    .getOrCreate()

# ============================================
# ํ…Œ์ด๋ธ” ์ƒ์„ฑ (SQL)
# ============================================
spark.sql("""
    CREATE TABLE IF NOT EXISTS iceberg_catalog.analytics.user_events (
        event_id STRING,
        user_id STRING,
        event_type STRING,
        event_data STRING,
        event_timestamp TIMESTAMP,
        created_at TIMESTAMP
    )
    USING iceberg
    PARTITIONED BY (days(event_timestamp))
    TBLPROPERTIES (
        'format-version' = '2',
        'write.format.default' = 'parquet',
        'write.parquet.compression-codec' = 'zstd',
        'write.metadata.compression-codec' = 'gzip',
        'write.target-file-size-bytes' = '134217728'
    )
""")

# ============================================
# ๋ฐ์ดํ„ฐ ์‚ฝ์ž…
# ============================================
data = [
    ("evt001", "user_1", "click", '{"page": "home"}',
     "2024-01-15 10:00:00", "2024-01-15 10:00:00"),
    ("evt002", "user_2", "purchase", '{"product_id": "P001", "amount": 100}',
     "2024-01-15 10:05:00", "2024-01-15 10:05:00"),
    ("evt003", "user_1", "view", '{"page": "product"}',
     "2024-01-15 10:10:00", "2024-01-15 10:10:00"),
]

schema = StructType([
    StructField("event_id", StringType(), False),
    StructField("user_id", StringType(), False),
    StructField("event_type", StringType(), False),
    StructField("event_data", StringType(), True),
    StructField("event_timestamp", TimestampType(), False),
    StructField("created_at", TimestampType(), False),
])

df = spark.createDataFrame(data, schema)
df.writeTo("iceberg_catalog.analytics.user_events").append()

# ============================================
# ์Šคํ‚ค๋งˆ ์ง„ํ™” (Schema Evolution)
# ============================================
# ์ƒˆ ์ปฌ๋Ÿผ ์ถ”๊ฐ€
spark.sql("""
    ALTER TABLE iceberg_catalog.analytics.user_events
    ADD COLUMN session_id STRING AFTER user_id
""")

# ์ปฌ๋Ÿผ ์ด๋ฆ„ ๋ณ€๊ฒฝ
spark.sql("""
    ALTER TABLE iceberg_catalog.analytics.user_events
    RENAME COLUMN event_data TO event_payload
""")

# ์ปฌ๋Ÿผ ํƒ€์ž… ๋ณ€๊ฒฝ (widening๋งŒ ๊ฐ€๋Šฅ)
spark.sql("""
    ALTER TABLE iceberg_catalog.analytics.user_events
    ALTER COLUMN event_payload TYPE STRING
""")

# ์ปฌ๋Ÿผ ์‚ญ์ œ
spark.sql("""
    ALTER TABLE iceberg_catalog.analytics.user_events
    DROP COLUMN created_at
""")

# ============================================
# ํŒŒํ‹ฐ์…˜ ์ง„ํ™” (Partition Evolution)
# ============================================
# ๊ธฐ์กด ํŒŒํ‹ฐ์…˜ ์ „๋žต ๋ณ€๊ฒฝ (days -> months)
# ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€๋˜๊ณ , ์ƒˆ ๋ฐ์ดํ„ฐ๋งŒ ์ƒˆ ์ „๋žต ์ ์šฉ
spark.sql("""
    ALTER TABLE iceberg_catalog.analytics.user_events
    ADD PARTITION FIELD months(event_timestamp)
""")

# ํžˆ๋“  ํŒŒํ‹ฐ์…˜ ์ถ”๊ฐ€ (user_id ๊ธฐ๋ฐ˜ ๋ฒ„ํ‚ท)
spark.sql("""
    ALTER TABLE iceberg_catalog.analytics.user_events
    ADD PARTITION FIELD bucket(16, user_id)
""")

# ============================================
# MERGE INTO (Upsert)
# ============================================
spark.sql("""
    MERGE INTO iceberg_catalog.analytics.user_events target
    USING (
        SELECT 'evt001' as event_id, 'user_1' as user_id,
               'click_confirmed' as event_type,
               '{"page": "home", "confirmed": true}' as event_payload,
               TIMESTAMP '2024-01-15 10:00:00' as event_timestamp
    ) source
    ON target.event_id = source.event_id
    WHEN MATCHED THEN
        UPDATE SET event_type = source.event_type,
                   event_payload = source.event_payload
    WHEN NOT MATCHED THEN
        INSERT (event_id, user_id, event_type, event_payload, event_timestamp)
        VALUES (source.event_id, source.user_id, source.event_type,
                source.event_payload, source.event_timestamp)
""")

# ============================================
# ํƒ€์ž„ ํŠธ๋ž˜๋ธ” (Time Travel)
# ============================================
# ์Šค๋ƒ…์ƒท ํžˆ์Šคํ† ๋ฆฌ ์กฐํšŒ
spark.sql("""
    SELECT * FROM iceberg_catalog.analytics.user_events.history
""").show()

# ํŠน์ • ์Šค๋ƒ…์ƒท ID๋กœ ์กฐํšŒ
spark.sql("""
    SELECT * FROM iceberg_catalog.analytics.user_events
    VERSION AS OF 1234567890123456789
""")

# ํŠน์ • ์‹œ๊ฐ„์œผ๋กœ ์กฐํšŒ
spark.sql("""
    SELECT * FROM iceberg_catalog.analytics.user_events
    TIMESTAMP AS OF '2024-01-15 10:00:00'
""")

# ์ด์ „ ์Šค๋ƒ…์ƒท์œผ๋กœ ๋กค๋ฐฑ
spark.sql("""
    CALL iceberg_catalog.system.rollback_to_snapshot(
        'analytics.user_events',
        1234567890123456789
    )
""")

# ============================================
# ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ฟผ๋ฆฌ
# ============================================
# ์Šค๋ƒ…์ƒท ์ •๋ณด
spark.sql("SELECT * FROM iceberg_catalog.analytics.user_events.snapshots").show()

# ๋งค๋‹ˆํŽ˜์ŠคํŠธ ํŒŒ์ผ ์ •๋ณด
spark.sql("SELECT * FROM iceberg_catalog.analytics.user_events.manifests").show()

# ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ •๋ณด
spark.sql("SELECT * FROM iceberg_catalog.analytics.user_events.files").show()

# ํŒŒํ‹ฐ์…˜ ์ •๋ณด
spark.sql("SELECT * FROM iceberg_catalog.analytics.user_events.partitions").show()

# ============================================
# ํ…Œ์ด๋ธ” ์œ ์ง€๋ณด์ˆ˜ (Maintenance)
# ============================================
# ์˜ค๋ž˜๋œ ์Šค๋ƒ…์ƒท ๋งŒ๋ฃŒ
spark.sql("""
    CALL iceberg_catalog.system.expire_snapshots(
        'analytics.user_events',
        TIMESTAMP '2024-01-01 00:00:00',
        100  -- retain_last
    )
""")

# ๊ณ ์•„ ํŒŒ์ผ ์ •๋ฆฌ
spark.sql("""
    CALL iceberg_catalog.system.remove_orphan_files(
        'analytics.user_events',
        TIMESTAMP '2024-01-01 00:00:00'
    )
""")

# ๋งค๋‹ˆํŽ˜์ŠคํŠธ ํŒŒ์ผ ๋ฆฌ๋ผ์ดํŠธ (์ฟผ๋ฆฌ ์„ฑ๋Šฅ ์ตœ์ ํ™”)
spark.sql("""
    CALL iceberg_catalog.system.rewrite_manifests('analytics.user_events')
""")

# ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์ปดํŒฉ์…˜
spark.sql("""
    CALL iceberg_catalog.system.rewrite_data_files(
        table => 'analytics.user_events',
        options => map(
            'target-file-size-bytes', '134217728',
            'min-file-size-bytes', '104857600',
            'max-file-size-bytes', '180355072'
        )
    )
""")

# ============================================
# Row-Level Delete (Position Delete vs Equality Delete)
# ============================================
# ์กฐ๊ฑด๋ถ€ ์‚ญ์ œ
spark.sql("""
    DELETE FROM iceberg_catalog.analytics.user_events
    WHERE user_id = 'user_1' AND event_type = 'view'
""")

# ์กฐ๊ฑด๋ถ€ ์—…๋ฐ์ดํŠธ
spark.sql("""
    UPDATE iceberg_catalog.analytics.user_events
    SET event_type = 'click_v2'
    WHERE event_type = 'click'
""")

print("Iceberg ํ…Œ์ด๋ธ” ์ž‘์—… ์™„๋ฃŒ")

How it is used in practice

Data engineer: "To change the schema of a Hive table, we have to reload all the data, and that takes too long."

Data architect: "If you migrate to Iceberg, schema evolution is handled at the metadata level, so you can add or change columns without rewriting data. You can also change the partition strategy while keeping the existing data."

Data engineer: "Then do we have to modify our existing Spark queries?"

Data architect: "If you just add the Iceberg Spark extension, you can use most SQL as is. You'll even find it easier, since you can use standard SQL syntax such as MERGE INTO."

Interviewer: "Please explain how Apache Iceberg's snapshot-based architecture works and why it matters."

Candidate: "Iceberg creates a new snapshot for each write operation. A snapshot consists of a metadata file, a manifest list, and manifest files, and each manifest contains the locations of data files and per-column statistics (min/max, null count). Thanks to this structure, at query time it can pinpoint exactly the files it needs without file system listing, which greatly improves performance on S3. Snapshot isolation also keeps reads and writes from interfering with each other, and time travel and rollback are naturally supported."

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

Reviewer: "You set format-version to 1 when creating the table, but I think it would be better to change it to v2."

Developer: "What changes in v2?"

Reviewer: "v2 handles row-level deletes as position deletes, so delete performance is better. v1 uses copy-on-write, so deleting requires rewriting the entire file. Also, v2 lets you specify a sort order, which helps optimize query performance."

Cautions

Related Terms

Learn More