> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Parquetファイルをクエリする方法

> chDBでParquetファイルをクエリする方法を学びます。

世界中の多くのデータは、Amazon S3バケットに保存されています。
このガイドでは、chDBを使ってそれらのデータをクエリする方法を学びます。

<div id="setup">
  ## 準備
</div>

まず、仮想環境を作成します。

```bash theme={null}
python -m venv .venv
source .venv/bin/activate
```

それでは、chDB をインストールします。
バージョン 2.0.2 以上であることを確認してください。

```bash theme={null}
pip install "chdb>=2.0.2"
```

次に、IPython をインストールします:

```bash theme={null}
pip install ipython
```

このガイドの以降では、コマンドの実行に `ipython` を使用します。次を実行して起動できます。

```bash theme={null}
ipython
```

このコードは、Python スクリプトや普段お使いのノートブックでも使用できます。

<div id="exploring-parquet-metadata">
  ## Parquetメタデータの確認
</div>

[Amazon reviews](/docs/ja/get-started/sample-datasets/amazon-reviews) データセットのParquetファイルを確認していきます。
その前に、まず `chDB` をインストールしましょう。

```python theme={null}
import chdb
```

Parquetファイルをクエリする際は、[`ParquetMetadata`](/docs/ja/reference/formats/Parquet/ParquetMetadata)入力フォーマットを使うことで、ファイルの内容ではなくParquetメタデータを返せます。
このフォーマットを使用した際に返されるフィールドを確認するため、`DESCRIBE`句を使ってみましょう。

```python theme={null}
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')
```

```text theme={null}
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` はどちらも、多数のプロパティを持つタプルの配列を含んでいるため、ここではひとまず除外します。

```python theme={null}
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')
```

```text theme={null}
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,000万を超える行があり、42の行グループに分割され、各行には15個のカラムのデータがあることがわかります。
行グループは、データを行単位で論理的に水平分割したものです。
各行グループには関連するメタデータがあり、クエリツールはそのメタデータを利用してファイルを効率的にクエリできます。

それでは、行グループの1つを見てみましょう。

```python theme={null}
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')
```

```text theme={null}
                 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                                                                                              🚅 +🐧=💥 😀
```

<div id="querying-parquet-files">
  ## Parquetファイルのクエリ
</div>

次に、ファイルの内容をクエリしてみましょう。
これを行うには、上記のクエリから `ParquetMetadata` を除き、たとえば全レビューで最も多い `star_rating` を算出します。

```python theme={null}
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')
```

```text theme={null}
   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 の商品は好評なようで、そうでない場合は、そもそも評価自体を投稿しないのでしょう。
