반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

지구정복

[Iceberg] Table Maintenance | 테이블 관리방법 본문

데이터 엔지니어링 정복/Iceberg

[Iceberg] Table Maintenance | 테이블 관리방법

noohhee 2025. 5. 28. 13:12
728x90
반응형

 

 

현재 아이스버그 1.3.1을 사용중이다.

공식문서는 아래 1.3.1을 참고한다.

https://web.archive.org/web/20240826175720/https://iceberg.apache.org/docs/latest/maintenance/

 

스파크는 3.4.1

트리노는 402버전 사용중

 

 

정리해야하는 파일들은 다음과 같다.

-Old Metadata Files

-Expired Snapshot Files

-Manifests Files

-Data Files

-Orphan Files

 

위 파일들은 사용자가 어쩔 수 없이 수동으로 정리를 해줘야 한다.

 

1. Metadata Files 관리

아이스버그는 JSON포맷인 metadata file을 통해 테이블의 변경을 추적한다.

테이블의 어떠한 변경이라도 일어나면 metadata json파일은 새로 생성된다.

 

따라서 스트리밍 테이블이나 커밋이 자주 발생하는 테이블의 경우 수시로 메타데이터 파일을 정리해야한다.

이를 자동적으로 정리하려면 테이블 생성시 혹은 생성후 Alter명령어로 아래 옵션값들을 추가해줘야 한다.

-write.metadata.delete-after-commit.enabled=true
이 값이 True이면 write.metadata.previous-versions-max 이 설정값까지 metadata를 유지하고 오래된 파일은 자동으로 삭제해준다.

-write.metadata.previous-versions-max=10
유지할 메타데이터 파일 개수

 

아래는 예시이다.

Spark3
CREATE TABLE spark_catalog.iceberg_db.my_iceberg_table (
    id BIGINT,
    name STRING,
    timestamp TIMESTAMP
)
USING iceberg
PARTITIONED BY (days(timestamp))
TBLPROPERTIES (
    'read.parquet.vectorization.enabled' = 'true',
    'write.metadata.delete-after-commit.enabled' = 'true',
    'write.metadata.previous-versions-max' = '5',
    'history.expire.max-snapshot-age-ms' = '3600000',  -- 1 hour (3600000 ms)
    'history.expire.min-snapshots-to-keep' = '3',  -- Keep at least 3 snapshot
    'format-version' = '2',
    'format'='parquet'
);
Trino
Trino 402버전에서 미지원

 

 

history.expire.max-snapshot-age-ms는 스냅샷정보를 스냅샷 메타테이블에서 어느정도 유지할 지 정하는 것이고, 해당 시간이 지나면 해당 메타테이블에서 제외된다.

실제 스냅샷파일은 삭제되지 않고, orphan file이 된다.

 

history.expire.min-snapshots-to-keep는 최소한 가지고 있어야할 스냅샷 개수를 의미한다.

 

 

2. Data Files 관리

데이터 파일이 스몰 파일인 경우 주기적으로 compact해줘야지 쿼리 성능이 향상한다.


Spark3
CALL catalog.system.rewrite_data_files(
  table => 'db.table1',
  strategy => 'binpack',
  options => map(
  'min-input-files', '2',       <--- 정해야됨, 보통 2-10 사이가 좋다고 함
  'target-file-size-bytes', '134217728'   <--- 정해야됨, parquet file은 512MB일 때 가장 효율이 좋다(apache parquet공식문서) but HDFS block size 128MB이므로 128이 나아보임
    )
)
Trino
Trino 402버전에서 미지원

 

 

특정 컬럼에 대해 sort해야한다면 sort 전략을 사용한다.

근데 partition돼있는 컬럼을 넣으면 안된다.

Spark3
CALL spark_catalog.system.rewrite_data_files(
  table => 'db.table1',
  strategy => 'sort',
  sort_order => 'zorder(col1, col2)',
  options => map(
    'min-input-files', '1',
    'target-file-size-bytes', '134217728'
  )
);

 

return되는 Output값은 다음과 같다.

Output🔗

Output NameTypeDescription

rewritten_data_files_count int Number of data which were re-written by this command
added_data_files_count int Number of new data files which were written by this command
rewritten_bytes_count long Number of bytes which were written by this command
failed_data_files_count int Number of data files that failed to be rewritten when partial-progress.enabled is true

 

 

 

3. Manifest Files 관리

Small File인 manifest list file들을 합쳐줘야 한다.

Spark3
CALL catalog_name.system.rewrite_manifests('db_name.table_name')
Trino
Trino 402버전에서 미지원

 

Output🔗

Output NameTypeDescription

rewritten_manifests_count int Number of manifests which were re-written by this command
added_mainfests_count int Number of new manifest files which were written by this command

 

 

 

4. Snapshot Files 관리

아이스버그 테이블에 변경작업이 있을 때마다 새로운 스냅샷 파일이 생성된다.

(snap-*.avro 파일)

하지만 테이블 생성시 history.expire.max-snapshot-age-ms를 설정했거나 설정하지 않았더라도 catalog의 config값인 iceberg.expire_snapshots.min-retention=7일 (default) 에 따라서 스냅샷 파일들은 expire된다.

 

이는 metadata 디렉터리에 용량을 차지하고 HDFS의 경우 small file문제를 일으킬 수 있다.

따라서 time traveling query 혹은 roll back이 필요하지 않을 경우 이러한 만료된 스냅샷 파일들은 제거해줘야한다.

Spark3
CALL catalog_name.system.expire_snapshots(
  table => 'db_name.table_name',
  older_than => now(),
  retain_last => 5
)
Spark3
CALL catalog_name.system.expire_snapshots(
'db_name.table_name',
TIMESTAMP '2025-05-28 00:00:00.000',
5)
Trino
ALTER TABLE iceberg.db1_test.jh1 EXECUTE expire_snapshots;
ALTER TABLE iceberg.db1_test.jh1 EXECUTE expire_snapshots(retention_threshold => '1m');

 

Output🔗

Output NameTypeDescription
deleted_data_files_count long Number of data files deleted by this operation
deleted_position_delete_files_count long Number of position delete files deleted by this operation
deleted_equality_delete_files_count long Number of equality delete files deleted by this operation
deleted_manifest_files_count long Number of manifest files deleted by this operation
deleted_manifest_lists_count long Number of manifest List files deleted by this operation

 

5. Orphan Files 관리

마지막으로 앞선 메타데이터 관리 프로시저들로 인해 링크되어 있던 파일들이 새롭게 쓰여지면서 고아파일들이 생겼을 것이다.

예를 들면 metadata -> manifest list -> manifest file(snapshot) -> data files 이러한 링크들이 새롭게 쓰여지고, 

링크가 사라진 파일들은 모두 고아파일이 된 것이다.

 

혹은 쓰기작업 도중 무슨 문제로 인해 링크가 제대로 안걸린 파일들도 고아파일이 된다.

 

따라서 이러한 고아파일들을 삭제해줘야 한다.

Spark3
CALL catalog_name.system.remove_orphan_files(
  table => 'db_name.table_name'
)
 
 
#특정 로케이션의 고아파일을 지우고 싶은 경우
CALL catalog_name.system.remove_orphan_files(
  table => 'db_name.table_name',
  location => 'table_location/data'
)
Trino
ALTER TABLE iceberg.db1_test.jh1 EXECUTE remove_orphan_files;
ALTER TABLE iceberg.db1_test.jh1 EXECUTE remove_orphan_files(retention_threshold => '1m');

 

Iceberg 카탈로그 설정( iceberg.remove_orphan_files.min-retention =7d)의 기본값을 초과할 수 없다.

 

 

Output🔗

Output NameTypeDescription
orphan_file_location String The path to each file determined to be an orphan by this command

 

 

 

728x90
반응형
Comments