> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# chDBでApache Arrowをクエリする方法

> このガイドでは、chDBでApache Arrowテーブルをクエリする方法を学びます

[Apache Arrow](https://arrow.apache.org/) は、データ分野で広く利用されている標準化された列指向メモリフォーマットです。
このガイドでは、`Python` テーブル関数を使って Apache Arrow をクエリする方法を学びます。

<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"
```

それでは、PyArrow、pandas、ipython をインストールしましょう。

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

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

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

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

<div id="creating-an-apache-arrow-table-from-a-file">
  ## ファイルから Apache Arrow テーブルを作成する
</div>

まず、[AWS CLI ツール](https://aws.amazon.com/cli/) を使って、[Ookla データセット](https://github.com/teamookla/ookla-open-data) の Parquet ファイルを 1 つダウンロードします。

```bash theme={null}
aws s3 cp \
  --no-sign \
  s3://ookla-open-data/parquet/performance/type=mobile/year=2023/quarter=2/2023-04-01_performance_mobile_tiles.parquet .
```

<Note>
  さらに多くのファイルをダウンロードする場合は、`aws s3 ls` を使用してすべてのファイルの一覧を取得し、上記のコマンドを適宜更新してください。
</Note>

次に、`pyarrow` パッケージから Parquet モジュールをインポートします。

```python theme={null}
import pyarrow.parquet as pq
```

次に、ParquetファイルをApache Arrowテーブルに読み込みます:

```python theme={null}
arrow_table = pq.read_table("./2023-04-01_performance_mobile_tiles.parquet")
```

スキーマを以下に示します。

```python theme={null}
arrow_table.schema
```

```text theme={null}
quadkey: string
tile: string
tile_x: double
tile_y: double
avg_d_kbps: int64
avg_u_kbps: int64
avg_lat_ms: int64
avg_lat_down_ms: int32
avg_lat_up_ms: int32
tests: int64
devices: int64
```

また、`shape` 属性を使うことで、行数とカラム数を取得できます:

```python theme={null}
arrow_table.shape
```

```text theme={null}
(3864546, 11)
```

<div id="querying-apache-arrow">
  ## Apache Arrow をクエリする
</div>

それでは、chDB から Arrow テーブルをクエリしてみましょう。
まず、chDB をインポートします：

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

次に、テーブルは次のように記述できます:

```python theme={null}
chdb.query("""
DESCRIBE Python(arrow_table)
SETTINGS describe_compact_output=1
""", "DataFrame")
```

```text theme={null}
               name     type
0           quadkey   String
1              tile   String
2            tile_x  Float64
3            tile_y  Float64
4        avg_d_kbps    Int64
5        avg_u_kbps    Int64
6        avg_lat_ms    Int64
7   avg_lat_down_ms    Int32
8     avg_lat_up_ms    Int32
9             tests    Int64
10          devices    Int64
```

行数をカウントすることもできます。

```python theme={null}
chdb.query("SELECT count() FROM Python(arrow_table)", "DataFrame")
```

```text theme={null}
   count()
0  3864546
```

では、もう少し面白いことをしてみましょう。
次のクエリでは、`quadkey` と `tile.*` のカラムを除外したうえで、残りのすべてのカラムについて平均値と最大値を計算します。

```python theme={null}
chdb.query("""
WITH numericColumns AS (
  SELECT * EXCEPT ('tile.*') EXCEPT(quadkey)
  FROM Python(arrow_table)
)
SELECT * APPLY(max), * APPLY(avg) APPLY(x -> round(x, 2))
FROM numericColumns
""", "Vertical")
```

```text theme={null}
Row 1:
──────
max(avg_d_kbps):                4155282
max(avg_u_kbps):                1036628
max(avg_lat_ms):                2911
max(avg_lat_down_ms):           2146959360
max(avg_lat_up_ms):             2146959360
max(tests):                     111266
max(devices):                   1226
round(avg(avg_d_kbps), 2):      84393.52
round(avg(avg_u_kbps), 2):      15540.4
round(avg(avg_lat_ms), 2):      41.25
round(avg(avg_lat_down_ms), 2): 554355225.76
round(avg(avg_lat_up_ms), 2):   552843178.3
round(avg(tests), 2):           6.31
round(avg(devices), 2):         2.88
```
