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

> CapnProto に関するドキュメント

# CapnProto

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            ClickHouse Cloud では利用できません
        </div>;
};

| 入力 | 出力 | エイリアス |
| -- | -- | ----- |
| ✔  | ✔  |       |

<div id="description">
  ## 説明
</div>

`CapnProto` フォーマットは、[`Protocol Buffers`](https://developers.google.com/protocol-buffers/) や [Thrift](https://en.wikipedia.org/wiki/Apache_Thrift) に似たバイナリメッセージ形式ですが、[JSON](/ja/reference/formats/JSON/JSON) や [MessagePack](https://msgpack.org/) のような自己記述型の形式ではありません。
CapnProto メッセージは厳密に型付けされており、自己記述型ではないため、外部のスキーマ記述が必要です。スキーマはその場で適用され、各クエリごとにキャッシュされます。

関連項目: [フォーマットスキーマ](/ja/reference/formats/index#formatschema)。

<div id="data_types-matching-capnproto">
  ## データ型の対応
</div>

以下の表は、サポートされているデータ型と、`INSERT` および `SELECT` クエリにおいてそれらが ClickHouse の[データ型](/ja/reference/data-types/index)にどのように対応するかを示しています。

| CapnProto データ型 (`INSERT`)                            | ClickHouse データ型                                                                                                                            | CapnProto データ型 (`SELECT`)                            |
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------- |
| `UINT8`, `BOOL`                                      | [UInt8](/ja/reference/data-types/int-uint)                                                                                                 | `UINT8`                                              |
| `INT8`                                               | [Int8](/ja/reference/data-types/int-uint)                                                                                                  | `INT8`                                               |
| `UINT16`                                             | [UInt16](/ja/reference/data-types/int-uint), [Date](/ja/reference/data-types/date)                                                         | `UINT16`                                             |
| `INT16`                                              | [Int16](/ja/reference/data-types/int-uint)                                                                                                 | `INT16`                                              |
| `UINT32`                                             | [UInt32](/ja/reference/data-types/int-uint), [DateTime](/ja/reference/data-types/datetime)                                                 | `UINT32`                                             |
| `INT32`                                              | [Int32](/ja/reference/data-types/int-uint), [Decimal32](/ja/reference/data-types/decimal)                                                  | `INT32`                                              |
| `UINT64`                                             | [UInt64](/ja/reference/data-types/int-uint)                                                                                                | `UINT64`                                             |
| `INT64`                                              | [Int64](/ja/reference/data-types/int-uint), [DateTime64](/ja/reference/data-types/datetime), [Decimal64](/ja/reference/data-types/decimal) | `INT64`                                              |
| `FLOAT32`                                            | [Float32](/ja/reference/data-types/float)                                                                                                  | `FLOAT32`                                            |
| `FLOAT64`                                            | [Float64](/ja/reference/data-types/float)                                                                                                  | `FLOAT64`                                            |
| `TEXT, DATA`                                         | [String](/ja/reference/data-types/string), [FixedString](/ja/reference/data-types/fixedstring)                                             | `TEXT, DATA`                                         |
| `union(T, Void), union(Void, T)`                     | [Nullable(T)](/ja/reference/data-types/date)                                                                                               | `union(T, Void), union(Void, T)`                     |
| `ENUM`                                               | [Enum(8/16)](/ja/reference/data-types/enum)                                                                                                | `ENUM`                                               |
| `LIST`                                               | [Array](/ja/reference/data-types/array)                                                                                                    | `LIST`                                               |
| `STRUCT`                                             | [Tuple](/ja/reference/data-types/tuple)                                                                                                    | `STRUCT`                                             |
| `UINT32`                                             | [IPv4](/ja/reference/data-types/ipv4)                                                                                                      | `UINT32`                                             |
| `DATA`                                               | [IPv6](/ja/reference/data-types/ipv6)                                                                                                      | `DATA`                                               |
| `DATA`                                               | [Int128/UInt128/Int256/UInt256](/ja/reference/data-types/int-uint)                                                                         | `DATA`                                               |
| `DATA`                                               | [Decimal128/Decimal256](/ja/reference/data-types/decimal)                                                                                  | `DATA`                                               |
| `STRUCT(entries LIST(STRUCT(key Key, value Value)))` | [Map](/ja/reference/data-types/map)                                                                                                        | `STRUCT(entries LIST(STRUCT(key Key, value Value)))` |

* 整数型は、入出力時に相互変換できます。
* CapnProto フォーマットで `Enum` を扱うには、[format\_capn\_proto\_enum\_comparising\_mode](/ja/reference/settings/formats#format_capn_proto_enum_comparising_mode) 設定を使用してください。
* `Array` はネストでき、引数として `Nullable` 型の値を取ることもできます。`Tuple` 型と `Map` 型もネストできます。

<div id="example-usage">
  ## 使用例
</div>

<div id="inserting-and-selecting-data-capnproto">
  ### データの挿入と取得
</div>

次のコマンドで、ファイルから CapnProto データを ClickHouse テーブルに挿入できます。

```bash theme={null}
$ cat capnproto_messages.bin | clickhouse-client --query "INSERT INTO test.hits SETTINGS format_schema = 'schema:Message' FORMAT CapnProto"
```

`schema.capnp` の内容は次のとおりです。

```capnp theme={null}
struct Message {
  SearchPhrase @0 :Text;
  c @1 :Uint64;
}
```

次のコマンドを使用すると、ClickHouseテーブルからデータを取得し、`CapnProto` フォーマットのファイルに保存できます。

```bash theme={null}
$ clickhouse-client --query = "SELECT * FROM test.hits FORMAT CapnProto SETTINGS format_schema = 'schema:Message'"
```

<div id="using-autogenerated-capn-proto-schema">
  ### 自動生成されたスキーマを使用する
</div>

データ用の外部 `CapnProto` スキーマがなくても、自動生成されたスキーマを使って `CapnProto` フォーマットでデータを出力/入力できます。

たとえば、次のようになります。

```sql theme={null}
SELECT * FROM test.hits 
FORMAT CapnProto 
SETTINGS format_capn_proto_use_autogenerated_schema=1
```

この場合、ClickHouse は関数 [structureToCapnProtoSchema](/ja/reference/functions/regular-functions/other-functions#structureToCapnProtoSchema) を使用してテーブル構造に基づく CapnProto スキーマを自動生成し、そのスキーマを使ってデータを CapnProto フォーマットでシリアライズします。

自動生成されたスキーマを使って CapnProto ファイルを読み込むこともできます (この場合、ファイルは同じスキーマを使用して作成されている必要があります) :

```bash theme={null}
$ cat hits.bin | clickhouse-client --query "INSERT INTO test.hits SETTINGS format_capn_proto_use_autogenerated_schema=1 FORMAT CapnProto"
```

<div id="format-settings">
  ## フォーマット設定
</div>

設定 [`format_capn_proto_use_autogenerated_schema`](/ja/reference/settings/formats#format_capn_proto_use_autogenerated_schema) はデフォルトで有効になっており、[`format_schema`](/ja/reference/formats/index#formatschema) が設定されていない場合に適用されます。

また、設定 [`output_format_schema`](/ja/reference/settings/formats#output_format_schema) を使用すると、入出力時に自動生成されたスキーマをファイルに保存できます。

例:

```sql theme={null}
SELECT * FROM test.hits 
FORMAT CapnProto 
SETTINGS 
    format_capn_proto_use_autogenerated_schema=1,
    output_format_schema='path/to/schema/schema.capnp'
```

この場合、自動生成された`CapnProto`スキーマは`path/to/schema/schema.capnp`ファイルに保存されます。
