전 세계 데이터의 상당수는 Amazon S3 버킷에 저장되어 있습니다. 이 가이드에서는 chDB를 사용해 해당 데이터를 쿼리하는 방법을 알아봅니다.
설정
먼저 가상 환경을 생성합니다:
python -m venv .venv
source .venv/bin/activate이제 chDB를 설치하겠습니다. 버전 2.0.2 이상인지 확인하십시오:
pip install "chdb>=2.0.2"이제 IPython을 설치합니다:
pip install ipython이 가이드의 나머지 부분에 나오는 명령은 ipython으로 실행하며, 다음 명령으로 시작할 수 있습니다:
ipythonPython 스크립트나 자주 사용하는 노트북에서도 이 코드를 사용할 수 있습니다.
Parquet 메타데이터 살펴보기
Amazon 리뷰 데이터셋의 Parquet 파일을 살펴보겠습니다.
먼저 chDB를 설치합니다:
import chdbParquet 파일을 쿼리할 때 ParquetMetadata 입력 형식을 사용하면 파일 내용 대신 Parquet 메타데이터를 반환할 수 있습니다.
이 포맷을 사용할 때 반환되는 필드를 확인하기 위해 DESCRIBE 절을 사용해 보겠습니다:
query = """
DESCRIBE s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2015.snappy.parquet',
ParquetMetadata
)
SETTINGS describe_compact_output=1
"""
chdb.query(query, 'TabSeparated')num_columns UInt64
num_rows UInt64
num_row_groups UInt64
format_version String
metadata_size UInt64
total_uncompressed_size UInt64
total_compressed_size UInt64
columns Array(Tuple(name String, path String, max_definition_level UInt64, max_repetition_level UInt64, physical_type String, logical_type String, compression String, total_uncompressed_size UInt64, total_compressed_size UInt64, space_saved String, encodings Array(String)))
row_groups Array(Tuple(num_columns UInt64, num_rows UInt64, total_uncompressed_size UInt64, total_compressed_size UInt64, columns Array(Tuple(name String, path String, total_compressed_size UInt64, total_uncompressed_size UInt64, have_statistics Bool, statistics Tuple(num_values Nullable(UInt64), null_count Nullable(UInt64), distinct_count Nullable(UInt64), min Nullable(String), max Nullable(String))))))이제 이 파일의 메타데이터를 살펴보겠습니다.
columns와 row_groups는 모두 많은 속성을 담은 튜플 배열을 포함하므로, 지금은 제외하겠습니다.
query = """
SELECT * EXCEPT(columns, row_groups)
FROM s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2015.snappy.parquet',
ParquetMetadata
)
"""
chdb.query(query, 'Vertical')Row 1:
──────
num_columns: 15
num_rows: 41905631
num_row_groups: 42
format_version: 2.6
metadata_size: 79730
total_uncompressed_size: 14615827169
total_compressed_size: 9272262304이 출력에서 이 Parquet 파일에는 4천만 개가 넘는 행이 있으며, 42개의 row group으로 나뉘어 있고 각 행에는 15개의 데이터 컬럼이 있음을 알 수 있습니다. row group은 데이터를 행 단위로 논리적으로 수평 분할한 단위입니다. 각 row group에는 관련 메타데이터가 있으며, 쿼리 도구는 이 메타데이터를 활용해 파일을 효율적으로 쿼리할 수 있습니다.
이제 row group 중 하나를 살펴보겠습니다:
query = """
WITH rowGroups AS (
SELECT rg
FROM s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2015.snappy.parquet',
ParquetMetadata
)
ARRAY JOIN row_groups AS rg
LIMIT 1
)
SELECT tupleElement(c, 'name') AS name, tupleElement(c, 'total_compressed_size') AS total_compressed_size,
tupleElement(c, 'total_uncompressed_size') AS total_uncompressed_size,
tupleElement(tupleElement(c, 'statistics'), 'min') AS min,
tupleElement(tupleElement(c, 'statistics'), 'max') AS max
FROM rowGroups
ARRAY JOIN tupleElement(rg, 'columns') AS c
"""
chdb.query(query, 'DataFrame') name total_compressed_size total_uncompressed_size min max
0 review_date 493 646 16455 16472
1 marketplace 66 64 US US
2 customer_id 5207967 7997207 10049 53096413
3 review_id 14748425 17991290 R10004U8OQDOGE RZZZUTBAV1RYI
4 product_id 8003456 13969668 0000032050 BT00DDVMVQ
5 product_parent 5758251 7974737 645 999999730
6 product_title 41068525 63355320 ! Small S 1pc Black 1pc Navy (Blue) Replacemen... 🌴 Vacation On The Beach
7 product_category 1726 1815 Apparel Pet Products
8 star_rating 369036 374046 1 5
9 helpful_votes 538940 1022990 0 3440
10 total_votes 610902 1080520 0 3619
11 vine 11426 125999 0 1
12 verified_purchase 102634 125999 0 1
13 review_headline 16538189 27634740 🤹🏽♂️🎤Great product. Practice makes perfect. D...
14 review_body 145886383 232457911 🚅 +🐧=💥 😀Parquet 파일 쿼리하기
다음으로 파일의 내용을 쿼리해 보겠습니다.
이를 위해 앞의 쿼리에서 ParquetMetadata를 제거한 뒤, 예를 들어 모든 리뷰에서 가장 많은 star_rating을 계산할 수 있습니다:
query = """
SELECT star_rating, count() AS count, formatReadableQuantity(count)
FROM s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/amazon_reviews/amazon_reviews_2015.snappy.parquet'
)
GROUP BY ALL
ORDER BY star_rating
"""
chdb.query(query, 'DataFrame') star_rating count formatReadableQuantity(count())
0 1 3253070 3.25 million
1 2 1865322 1.87 million
2 3 3130345 3.13 million
3 4 6578230 6.58 million
4 5 27078664 27.08 million흥미롭게도 별 5개 리뷰가 다른 모든 평점을 합친 것보다 더 많습니다! 사람들이 Amazon의 상품을 좋아하는 듯하며, 그렇지 않더라도 아예 평점을 남기지 않는 것 같습니다.