> ## 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.

# clickhouse-local データベースを使用する

> chDB で clickhouse-local データベースを使用する方法を学びます

[clickhouse-local](/docs/ja/concepts/features/tools-and-utilities/clickhouse-local) は、ClickHouse を組み込んだ CLI ツールです。
サーバーをインストールしなくても、ClickHouse の機能を利用できます。
このガイドでは、chDB から clickhouse-local データベースを使用する方法を学びます。

<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](https://ipython.org/) をインストールしましょう。

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

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

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

<div id="installing-clickhouse-local">
  ## clickhouse-local のインストール
</div>

clickhouse-local のダウンロードとインストールは、[ClickHouse のダウンロードとインストール](/docs/ja/get-started/setup/install)と同じです。
次のコマンドを実行します。

```bash theme={null}
curl https://clickhouse.com/ | sh
```

データをディレクトリに永続化した状態で `clickhouse-local` を起動するには、`--path` を指定する必要があります:

```bash theme={null}
./clickhouse -m --path demo.chdb
```

<div id="ingesting-data-into-clickhouse-local">
  ## clickhouse-local にデータを取り込む
</div>

デフォルトのデータベースはデータをメモリ内にしか保存しないため、取り込んだデータが確実にディスクへ永続化されるよう、名前付きのデータベースを作成する必要があります。

```sql theme={null}
CREATE DATABASE foo;
```

テーブルを作成し、ランダムな数値をいくつか挿入してみましょう：

```sql theme={null}
CREATE TABLE foo.randomNumbers
ORDER BY number AS
SELECT rand() AS number
FROM numbers(10_000_000);
```

どのようなデータがあるかを確認するために、クエリを書いてみましょう。

```sql theme={null}
SELECT quantilesExact(0, 0.5, 0.75, 0.99)(number) AS quants
FROM foo.randomNumbers
```

```response theme={null}
┌─quants────────────────────────────────┐
│ [69,2147776478,3221525118,4252096960] │
└───────────────────────────────────────┘
```

それが終わったら、このディレクトリのロックを保持できるプロセスは1つだけなので、CLI で必ず `exit;` してください。
そうしないと、chDB からデータベースに接続しようとしたときに、次のエラーが発生します:

```text theme={null}
ChdbError: Code: 76. DB::Exception: Cannot lock file demo.chdb/status. Another server instance in same directory is already running. (CANNOT_OPEN_FILE)
```

<div id="connecting-to-a-clickhouse-local-database">
  ## clickhouse-local データベースへの接続
</div>

`ipython` シェルに戻り、chDB から `session` モジュールをインポートします。

```python theme={null}
from chdb import session as chs
```

`demo.chdb` を指定したセッションを初期化します:

```python theme={null}
sess = chs.Session("demo.chdb")
```

次に、数値の分位点を返す同じクエリを実行できます：

```python theme={null}
sess.query("""
SELECT quantilesExact(0, 0.5, 0.75, 0.99)(number) AS quants
FROM foo.randomNumbers
""", "Vertical")

Row 1:
──────
quants: [0,9976599,2147776478,4209286886]
```

chDB からこのデータベースにデータを挿入することもできます:

```python theme={null}
sess.query("""
INSERT INTO foo.randomNumbers
SELECT rand() AS number FROM numbers(10_000_000)
""")

Row 1:
──────
quants: [0,9976599,2147776478,4209286886]
```

その後、chDB または clickhouse-local で分位点クエリを再実行できます。
