

 Amazon Redshift は、パッチ 198 以降、新しい Python UDF の作成をサポートしなくなります。既存の Python UDF は、2026 年 6 月 30 日まで引き続き機能します。詳細については、[ブログ記事](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)を参照してください。

# Data API の呼び出し
<a name="data-api-calling"></a>

Data API または AWS CLI を呼び出して、クラスターまたはサーバーレスワークグループで SQL ステートメントを実行できます。SQL ステートメントを実行する主なオペレーションは、*Amazon Redshift Data API リファレンス*の [https://docs.aws.amazon.com/redshift-data/latest/APIReference/API_ExecuteStatement.html](https://docs.aws.amazon.com/redshift-data/latest/APIReference/API_ExecuteStatement.html) と [https://docs.aws.amazon.com/redshift-data/latest/APIReference/API_BatchExecuteStatement.html](https://docs.aws.amazon.com/redshift-data/latest/APIReference/API_BatchExecuteStatement.html) です。Data API は、AWSSDK でサポートされているプログラミング言語をサポートしています。詳細については、「[AWS での構築ツール](https://aws.amazon.com/tools/)」を参照してください。

Data API を呼び出すコードの例については、*GitHub* の「[Getting Started with Redshift Data API](https://github.com/aws-samples/getting-started-with-amazon-redshift-data-api#getting-started-with-redshift-data-api)」を参照してください。このリポジトリには、Amazon EC2、AWS Glue Data Catalog または Amazon SageMaker ランタイムからの Amazon Redshift データへのアクセスに AWS Lambda を使用する例があります。プログラミング言語の例としては、Python、Go、Java、Javascript があります。

Data API は、AWS CLIを使用して呼び出すことができます。

次の例では、AWS CLIを使用して Data API を呼び出します。例を実行するには、環境に合わせてパラメータ値を編集します。多くの例では、クラスターに対して実行するため、`cluster-identifier` が指定されています。サーバーレスワークグループに対して実行する場合は、代わりに `workgroup-name` を指定します。これらの例は、データ API 操作の一部を示しています。詳細については、*AWS CLIコマンドリファレンス*を参照してください。

次の例のコマンドは分割され、読みやすい形式になっています。すべてのパラメータとレスポンスがすべての例で表示されるとは限りません。完全なリクエスト構文、リクエストパラメータ、レスポンス構文、レスポンス要素の API 定義については、「[Amazon Redshift Data API リファレンス](https://docs.aws.amazon.com/redshift-data/latest/APIReference/)」を参照してください。

# SQL ステートメントを Amazon Redshift データウェアハウスに渡す
<a name="pass-sql-statements"></a>

このページの例は、SQL ステートメントをデータウェアハウスに渡すさまざまな方法について説明しています。

## SQL ステートメントを実行する
<a name="data-api-calling-cli-execute-statement"></a>

SQL ステートメントを実行するには、`aws redshift-data execute-statement` AWS CLI コマンドを使用します。

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、結果を取得する識別子を返します。この例では、AWS Secrets Managerの認証方法を使用します。

```
aws redshift-data execute-statement 
    --secret-arn arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn 
    --cluster-identifier mycluster-test 
    --sql "select * from stl_query limit 1" 
    --database dev
```

次は、レスポンスの例です。

```
{
    "ClusterIdentifier": "mycluster-test",
    "CreatedAt": 1598323175.823,
    "Database": "dev",
    "Id": "c016234e-5c6c-4bc5-bb16-2c5b8ff61814",
    "SecretArn": "arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn"
}
```

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、結果を取得する識別子を返します。この例では、一時的な認証情報認証方法を使用します。

```
aws redshift-data execute-statement 
    --db-user myuser 
    --cluster-identifier mycluster-test 
    --database dev 
    --sql "select * from stl_query limit 1"
```

次は、レスポンスの例です。

```
{
    "ClusterIdentifier": "mycluster-test",
    "CreatedAt": 1598306924.632,
    "Database": "dev",
    "DbUser": "myuser",
    "Id": "d9b6c0c9-0747-4bf4-b142-e8883122f766"
}
```

次の AWS CLI コマンドは、サーバーレスワークグループに対して SQL ステートメントを実行し、結果を取得する識別子を返します。この例では、一時的な認証情報認証方法を使用します。

```
aws redshift-data execute-statement 
    --database dev 
    --workgroup-name myworkgroup 
    --sql "select 1;"
```

次は、レスポンスの例です。

```
{
 "CreatedAt": "2022-02-11T06:25:28.748000+00:00",
 "Database": "dev",
 "DbUser": "IAMR:RoleName",
 "Id": "89dd91f5-2d43-43d3-8461-f33aa093c41e",
 "WorkgroupName": "myworkgroup"
}
```

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、結果を取得する識別子を返します。この例では、AWS Secrets Manager 認証方法と冪等性トークンを使用しています。

```
aws redshift-data execute-statement 
    --secret-arn arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn 
    --cluster-identifier mycluster-test 
    --sql "select * from stl_query limit 1" 
    --database dev 
    --client-token b855dced-259b-444c-bc7b-d3e8e33f94g1
```

次は、レスポンスの例です。

```
{
    "ClusterIdentifier": "mycluster-test",
    "CreatedAt": 1598323175.823,
    "Database": "dev",
    "Id": "c016234e-5c6c-4bc5-bb16-2c5b8ff61814",
    "SecretArn": "arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn"
}
```

## パラメータを使用して SQL ステートメントを実行する
<a name="data-api-calling-cli-execute-statement-parameters"></a>

SQL ステートメントを実行するには、`aws redshift-data execute-statement` AWS CLI コマンドを使用します。

 次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、結果を取得する識別子を返します。この例では、AWS Secrets Managerの認証方法を使用します。SQL テキストには、名前付きパラメータ `distance` があります。この場合、述部で使用される距離は `5` です。SELECT ステートメントでは、列名の名前付きパラメータは述部でのみ使用できます。SQL ステートメントの名前付きパラメータの値は、`parameters`オプションで指定できます。

```
aws redshift-data execute-statement 
    --secret-arn arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn 
    --cluster-identifier mycluster-test 
    --sql "SELECT ratecode FROM demo_table WHERE trip_distance > :distance"  
    --parameters "[{\"name\": \"distance\", \"value\": \"5\"}]"
    --database dev
```

次は、レスポンスの例です。

```
{
    "ClusterIdentifier": "mycluster-test",
    "CreatedAt": 1598323175.823,
    "Database": "dev",
    "Id": "c016234e-5c6c-4bc5-bb16-2c5b8ff61814",
    "SecretArn": "arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn"
}
```

次の例では、サンプルデータベースの `EVENT` テーブルを使用します。詳細については、*Amazon Redshift データベースデベロッパーガイド*の「[EVENT テーブル](https://docs.aws.amazon.com/redshift/latest/dg/r_eventtable.html)」を参照してください。

まだデータベースに `EVENT` テーブルにない場合、Data API を使用して次のように作成できます。

```
aws redshift-data execute-statement 
--database dev
--cluster-id mycluster-test
--db-user awsuser
--sql "create table event( eventid integer not null distkey, 
                           venueid smallint not null, 
                           catid smallint not null, 
                           dateid smallint not null sortkey, 
                           eventname varchar(200), 
                           starttime timestamp)"
```

次のコマンドは、1 つの行を `EVENT` テーブルに挿入します。

```
aws redshift-data execute-statement 
--database dev
--cluster-id mycluster-test
--db-user awsuser 
--sql "insert into event values(:eventid, :venueid::smallint, :catid, :dateid, :eventname, :starttime)" 
--parameters "[{\"name\": \"eventid\", \"value\": \"1\"}, {\"name\": \"venueid\", \"value\": \"1\"}, 
               {\"name\": \"catid\", \"value\": \"1\"}, 
               {\"name\": \"dateid\", \"value\": \"1\"}, 
               {\"name\": \"eventname\", \"value\": \"event 1\"}, 
               {\"name\": \"starttime\", \"value\": \"2022-02-22\"}]"
```

次のコマンドは、2 つ目の行を `EVENT` テーブルに挿入します。この例は以下を示します。
+ `id` という名前のパラメータは SQL テキストで 4 回使用されます。
+ 暗黙的な型変換は、パラメータ `starttime` を挿入する際に自動的に適用されます。
+ `venueid` 列は、SMALLINT データ型にタイプキャストされます。
+ DATE データ型を表す文字列は、TIMESTAMP データ型に暗黙的に変換されます。
+ コメントは SQL テキスト内で使用できます。

```
aws redshift-data execute-statement 
--database dev
--cluster-id mycluster-test
--db-user awsuser 
--sql "insert into event values(:id, :id::smallint, :id, :id, :eventname, :starttime) /*this is comment, and it won't apply parameterization for :id, :eventname or :starttime here*/" 
--parameters "[{\"name\": \"eventname\", \"value\": \"event 2\"}, 
               {\"name\": \"starttime\", \"value\": \"2022-02-22\"}, 
               {\"name\": \"id\", \"value\": \"2\"}]"
```

次に、挿入された 2 つの行を示します。

```
 eventid | venueid | catid | dateid | eventname |      starttime
---------+---------+-------+--------+-----------+---------------------
       1 |       1 |     1 |      1 | event 1   | 2022-02-22 00:00:00
       2 |       2 |     2 |      2 | event 2   | 2022-02-22 00:00:00
```

次のコマンドでは、WHERE 句で名前付きパラメータを使用して、`eventid`が `1` である行を取得します。

```
aws redshift-data execute-statement 
--database dev
--cluster-id mycluster-test
--db-user awsuser 
--sql "select * from event where eventid=:id"
--parameters "[{\"name\": \"id\", \"value\": \"1\"}]"
```

次のコマンドを実行して、前の SQL ステートメントの SQL 結果を取得します。

```
aws redshift-data get-statement-result --id 7529ad05-b905-4d71-9ec6-8b333836eb5a        
```

これにより次の結果が示されます。

```
{
    "Records": [
        [
            {
                "longValue": 1
            },
            {
                "longValue": 1
            },
            {
                "longValue": 1
            },
            {
                "longValue": 1
            },
            {
                "stringValue": "event 1"
            },
            {
                "stringValue": "2022-02-22 00:00:00.0"
            }
        ]
    ],
    "ColumnMetadata": [
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "eventid",
            "length": 0,
            "name": "eventid",
            "nullable": 0,
            "precision": 10,
            "scale": 0,
            "schemaName": "public",
            "tableName": "event",
            "typeName": "int4"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "venueid",
            "length": 0,
            "name": "venueid",
            "nullable": 0,
            "precision": 5,
            "scale": 0,
            "schemaName": "public",
            "tableName": "event",
            "typeName": "int2"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "catid",
            "length": 0,
            "name": "catid",
            "nullable": 0,
            "precision": 5,
            "scale": 0,
            "schemaName": "public",
            "tableName": "event",
            "typeName": "int2"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "dateid",
            "length": 0,
            "name": "dateid",
            "nullable": 0,
            "precision": 5,
            "scale": 0,
            "schemaName": "public",
            "tableName": "event",
            "typeName": "int2"
        },
        {
            "isCaseSensitive": true,
            "isCurrency": false,
            "isSigned": false,
            "label": "eventname",
            "length": 0,
            "name": "eventname",
            "nullable": 1,
            "precision": 200,
            "scale": 0,
            "schemaName": "public",
            "tableName": "event",
            "typeName": "varchar"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "label": "starttime",
            "length": 0,
            "name": "starttime",
            "nullable": 1,
            "precision": 29,
            "scale": 6,
            "schemaName": "public",
            "tableName": "event",
            "typeName": "timestamp"
        }
    ],
    "TotalNumRows": 1
}
```

## 複数の SQL ステートメントを実行する
<a name="data-api-calling-cli-batch-execute-statement"></a>

1 つのコマンドで複数の SQL ステートメントを実行するには、`aws redshift-data batch-execute-statement`AWS CLI コマンドを実行します。

次の AWS CLI コマンドは、クラスターに対して 3 つの SQL ステートメントを実行し、結果を取得する識別子を返します。この例では、一時的な認証情報認証方法を使用します。

```
aws redshift-data batch-execute-statement 
    --db-user myuser 
    --cluster-identifier mycluster-test 
    --database dev 
    --sqls "set timezone to BST" "select * from mytable" "select * from another_table"
```

次は、レスポンスの例です。

```
{
    "ClusterIdentifier": "mycluster-test",
    "CreatedAt": 1598306924.632,
    "Database": "dev",
    "DbUser": "myuser",
    "Id": "d9b6c0c9-0747-4bf4-b142-e8883122f766"
}
```

# SQL ステートメントに関するメタデータを一覧表示する
<a name="data-api-calling-cli-list-statements"></a>

`aws redshift-data list-statements` AWS CLI コマンドを使用して、SQL ステートメントに関するメタデータを一覧表示します。このコマンドの実行の承認は、呼び出し元の IAM アクセス許可に基づいています。

次の AWS CLI コマンドは、実行された SQL ステートメントを示しています。

```
aws redshift-data list-statements 
    --status ALL
```

次は、レスポンスの例です。

```
{
    "Statements": [
        {
            "CreatedAt": 1598306924.632,
            "Id": "d9b6c0c9-0747-4bf4-b142-e8883122f766",
            "QueryString": "select * from stl_query limit 1",
            "Status": "FINISHED",
            "UpdatedAt": 1598306926.667
        },
        {
            "CreatedAt": 1598311717.437,
            "Id": "e0ebd578-58b3-46cc-8e52-8163fd7e01aa",
            "QueryString": "select * from stl_query limit 1",
            "Status": "FAILED",
            "UpdatedAt": 1598311719.008
        },
        {
            "CreatedAt": 1598313683.65,
            "Id": "c361d4f7-8c53-4343-8c45-6b2b1166330c",
            "QueryString": "select * from stl_query limit 1",
            "Status": "ABORTED",
            "UpdatedAt": 1598313685.495
        },
        {
            "CreatedAt": 1598306653.333,
            "Id": "a512b7bd-98c7-45d5-985b-a715f3cfde7f",
            "QueryString": "select 1",
            "Status": "FINISHED",
            "UpdatedAt": 1598306653.992
        }
    ]
}
```

# SQL ステートメントに関するメタデータの記述を取得する
<a name="data-api-calling-cli-describe-statement"></a>

SQL ステートメントのメタデータの記述を取得するには、`aws redshift-data describe-statement` AWS CLI コマンドを使用します。このコマンドの実行の承認は、呼び出し元の IAM アクセス許可に基づいています。

次の AWS CLI コマンドは、SQL ステートメントを記述します。

```
aws redshift-data describe-statement 
    --id d9b6c0c9-0747-4bf4-b142-e8883122f766
```

次は、レスポンスの例です。

```
{
    "ClusterIdentifier": "mycluster-test",
    "CreatedAt": 1598306924.632,
    "Duration": 1095981511,
    "Id": "d9b6c0c9-0747-4bf4-b142-e8883122f766",
    "QueryString": "select * from stl_query limit 1",
    "RedshiftPid": 20859,
    "RedshiftQueryId": 48879,
    "ResultRows": 1,
    "ResultSize": 4489,
    "Status": "FINISHED",
    "UpdatedAt": 1598306926.667
}
```

次に、複数の SQL ステートメントで `batch-execute-statement` コマンドを実行した後の `describe-statement` レスポンスの例を示します。

```
{
    "ClusterIdentifier": "mayo",
    "CreatedAt": 1623979777.126,
    "Duration": 6591877,
    "HasResultSet": true,
    "Id": "b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652",
    "RedshiftPid": 31459,
    "RedshiftQueryId": 0,
    "ResultRows": 2,
    "ResultSize": 22,
    "Status": "FINISHED",
    "SubStatements": [
        {
            "CreatedAt": 1623979777.274,
            "Duration": 3396637,
            "HasResultSet": true,
            "Id": "b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652:1",
            "QueryString": "select 1;",
            "RedshiftQueryId": -1,
            "ResultRows": 1,
            "ResultSize": 11,
            "Status": "FINISHED",
            "UpdatedAt": 1623979777.903
        },
        {
            "CreatedAt": 1623979777.274,
            "Duration": 3195240,
            "HasResultSet": true,
            "Id": "b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652:2",
            "QueryString": "select 2;",
            "RedshiftQueryId": -1,
            "ResultRows": 1,
            "ResultSize": 11,
            "Status": "FINISHED",
            "UpdatedAt": 1623979778.076
        }
    ],
    "UpdatedAt": 1623979778.183
}
```

# SQL ステートメントの結果を取得する
<a name="data-api-calling-cli-get-statement-result"></a>

実行された SQL ステートメントの結果を取得するには、`redshift-data get-statement-result` または `redshift-data get-statement-result-v2` AWS CLI コマンドを使用します。`get-statement-result` の結果は JSON 形式です。`get-statement-result-v2` の結果は CSV 形式です。レスポンスで受け取った `Id` を、`execute-statement`または`batch-execute-statement` に提供できます。`batch-execute-statement` によって実行された SQL ステートメントの `Id` 値は、`describe-statement` の結果で取得できます。この値には、`b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652:2` のように、コロンとシーケンス番号が末尾に付きます。`batch-execute-statement` で複数の SQL ステートメントを実行する場合、各 SQL ステートメントには `describe-statement` に示されているような `Id` 値があります。このコマンドの実行の承認は、呼び出し元の IAM アクセス許可に基づいています。

次のステートメントは、`execute-statement` によって実行された SQL ステートメントの結果を返します。`ResultFormat` はデフォルトで `JSON` に設定されます。結果を取得するには、`get-statement-result` オペレーションを呼び出します。

```
aws redshift-data get-statement-result 
    --id d9b6c0c9-0747-4bf4-b142-e8883122f766
```

次のステートメントは、`batch-execute-statement`によって実行された 2 番目の SQL ステートメントの結果を返します。

```
aws redshift-data get-statement-result 
    --id b2906c76-fa6e-4cdf-8c5f-4de1ff9b7652:2
```

以下は、`get-statement-result` への呼び出しに対するレスポンスの例です。SQL 結果は、レスポンスの `Records` キーに JSON 形式で返されます。

```
{
    "ColumnMetadata": [
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "userid",
            "length": 0,
            "name": "userid",
            "nullable": 0,
            "precision": 10,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "int4"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "query",
            "length": 0,
            "name": "query",
            "nullable": 0,
            "precision": 10,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "int4"
        },
        {
            "isCaseSensitive": true,
            "isCurrency": false,
            "isSigned": false,
            "label": "label",
            "length": 0,
            "name": "label",
            "nullable": 0,
            "precision": 320,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "bpchar"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "xid",
            "length": 0,
            "name": "xid",
            "nullable": 0,
            "precision": 19,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "int8"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "pid",
            "length": 0,
            "name": "pid",
            "nullable": 0,
            "precision": 10,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "int4"
        },
        {
            "isCaseSensitive": true,
            "isCurrency": false,
            "isSigned": false,
            "label": "database",
            "length": 0,
            "name": "database",
            "nullable": 0,
            "precision": 32,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "bpchar"
        },
        {
            "isCaseSensitive": true,
            "isCurrency": false,
            "isSigned": false,
            "label": "querytxt",
            "length": 0,
            "name": "querytxt",
            "nullable": 0,
            "precision": 4000,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "bpchar"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "label": "starttime",
            "length": 0,
            "name": "starttime",
            "nullable": 0,
            "precision": 29,
            "scale": 6,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "timestamp"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "label": "endtime",
            "length": 0,
            "name": "endtime",
            "nullable": 0,
            "precision": 29,
            "scale": 6,
            "schemaName": "",
            "tableName": "stll_query",
            "type": 93,
            "typeName": "timestamp"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "aborted",
            "length": 0,
            "name": "aborted",
            "nullable": 0,
            "precision": 10,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "int4"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "insert_pristine",
            "length": 0,
            "name": "insert_pristine",
            "nullable": 0,
            "precision": 10,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "int4"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "concurrency_scaling_status",
            "length": 0,
            "name": "concurrency_scaling_status",
            "nullable": 0,
            "precision": 10,
            "scale": 0,
            "schemaName": "",
            "tableName": "stll_query",
            "typeName": "int4"
        }
    ],
    "Records": [
        [
            {
                "longValue": 1
            },
            {
                "longValue": 3
            },
            {
                "stringValue": "health"
            },
            {
                "longValue": 1023
            },
            {
                "longValue": 15279
            },
            {
                "stringValue": "dev"
            },
            {
                "stringValue": "select system_status from stv_gui_status;"
            },
            {
                "stringValue": "2020-08-21 17:33:51.88712"
            },
            {
                "stringValue": "2020-08-21 17:33:52.974306"
            },
            {
                "longValue": 0
            },
            {
                "longValue": 0
            },
            {
                "longValue": 6
            }
        ]
    ],
    "TotalNumRows": 1
}
```

次の例は、`execute-statement` によって実行され、結果を JSON 形式で返す SQL ステートメントを示しています。テーブル `testingtable` には整数列が 3 つ (col1、col2、col3) あり、3 つの行に値 (1、2、3)、(4、5、6)、および (7、8、9) が含まれています。

```
aws redshift-data execute-statement 
    --database dev 
    --sql "SELECT col1, col2, col3 FROM testingtable" 
    --cluster-id mycluster-test 
    --result-format JSON
```

```
{
    "ClusterIdentifier": "mycluster-test",
    "CreatedAt": "2024-04-02T16:45:25.144000+00:00",
    "Database": "dev",
    "DbUser": "IAMR:Administrator",
    "Id": "d468d942-6df9-4f85-8ae3-bac01a61aec3"
}
```

以下は、`get-statement-result` への呼び出しに対するレスポンスの例です。SQL 結果は、レスポンスの `Records` キーに JSON 形式で返されます。

```
aws redshift-data get-statement-result
    --id d468d942-6df9-4f85-8ae3-bac01a61aec3
```

```
{
    "Records": [
        [
            {
                "longValue": 1
            },
            {
                "longValue": 2
            },
            {
                "longValue": 3
            }
        ],
        [
            {
                "longValue": 4
            },
            {
                "longValue": 5
            },
            {
                "longValue": 6
            }
        ],
        [
            {
                "longValue": 7
            },
            {
                "longValue": 8
            },
            {
                "longValue": 9
            }
        ]
    ],
    "ColumnMetadata": [
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "col1",
            "name": "col1",
            "nullable": 1,
            "precision": 10,
            "scale": 0,
            "schemaName": "public",
            "tableName": "testingtable",
            "typeName": "int4",
            "length": 0
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "col2",
            "name": "col2",
            "nullable": 1,
            "precision": 10,
            "scale": 0,
            "schemaName": "public",
            "tableName": "testingtable",
            "typeName": "int4",
            "length": 0
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "col3",
            "name": "col3",
            "nullable": 1,
            "precision": 10,
            "scale": 0,
            "schemaName": "public",
            "tableName": "testingtable",
            "typeName": "int4",
            "length": 0
        }
    ],
    "TotalNumRows": 3
}
```

次の例は、`execute-statement` によって実行され、結果を CSV 形式で返す SQL ステートメントを示しています。テーブル `testingtable` には整数列が 3 つ (col1、col2、col3) あり、3 つの行に値 (1、2、3)、(4、5、6)、および (7、8、9) が含まれています。

```
aws redshift-data execute-statement 
    --database dev 
    --sql "SELECT col1, col2, col3 FROM testingtable" 
    --cluster-id mycluster-test 
    --result-format CSV
```

```
{
    "ClusterIdentifier": "mycluster-test",
    "CreatedAt": "2024-04-02T16:45:25.144000+00:00",
    "Database": "dev",
    "DbUser": "IAMR:Administrator",
    "Id": "d468d942-6df9-4f85-8ae3-bac01a61aec3"
}
```

以下は、`get-statement-result-v2` への呼び出しに対するレスポンスの例です。SQL 結果は、レスポンスの `Records` キーに CSV 形式で返されます。行はキャリッジリターンと改行 (\$1r\$1n) で区切られます。`Records` で返される最初の行は列ヘッダーです。CSV 形式で返される結果は 1 MB 単位で返され、各チャンクには最大 1MB 分の任意の数の行が含まれます。

```
aws redshift-data get-statement-result-v2
    --id d468d942-6df9-4f85-8ae3-bac01a61aec3
```

```
{
    "Records": [
        {
            "CSVRecords": "col1,col2,col3\r\n1,2,3\r\n4,5,6\r\n7,8,9\r\n"
        }
    ],
    "ColumnMetadata": [
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "col1",
            "name": "col1",
            "nullable": 1,
            "precision": 10,
            "scale": 0,
            "schemaName": "public",
            "tableName": "testingtable",
            "typeName": "int4",
            "length": 0
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "col2",
            "name": "col2",
            "nullable": 1,
            "precision": 10,
            "scale": 0,
            "schemaName": "public",
            "tableName": "testingtable",
            "typeName": "int4",
            "length": 0
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": true,
            "label": "col3",
            "name": "col3",
            "nullable": 1,
            "precision": 10,
            "scale": 0,
            "schemaName": "public",
            "tableName": "testingtable",
            "typeName": "int4",
            "length": 0
        }
    ],
    "TotalNumRows": 3,
    "ResultFormat": "csv"
}
```

# テーブルの説明
<a name="data-api-calling-cli-describe-table"></a>

`aws redshift-data describe-table` AWS CLI コマンドを使用して、テーブルを説明するメタデータを取得します。

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、テーブルを記述するメタデータを返します。この例では、AWS Secrets Managerの認証方法を使用します。

```
aws redshift-data describe-table  
    --cluster-identifier mycluster-test 
    --database dev 
    --schema information_schema 
    --table sql_features 
    --secret-arn arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn
```

次は、レスポンスの例です。

```
{
    "ColumnList": [
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "feature_id",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "feature_name",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        }     
    ]
}
```

次の AWS CLI コマンドは、テーブルを記述するクラスターに対して SQL ステートメントを実行します。この例では、一時的な認証情報認証方法を使用します。

```
aws redshift-data describe-table 
    --db-user myuser 
    --cluster-identifier mycluster-test 
    --database dev 
    --schema information_schema 
    --table sql_features
```

次は、レスポンスの例です。

```
{
    "ColumnList": [
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "feature_id",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "feature_name",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "sub_feature_id",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "sub_feature_name",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "is_supported",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "is_verified_by",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        },
        {
            "isCaseSensitive": false,
            "isCurrency": false,
            "isSigned": false,
            "length": 2147483647,
            "name": "comments",
            "nullable": 1,
            "precision": 2147483647,
            "scale": 0,
            "schemaName": "information_schema",
            "tableName": "sql_features",
            "typeName": "character_data"
        }
    ]
}
```

# クラスター内のデータベースの一覧表示
<a name="data-api-calling-cli-list-databases"></a>

`aws redshift-data list-databases` AWS CLI コマンドを使用して、クラスター内のデータベースを一覧表示します。

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、データベースを一覧表示します。この例では、AWS Secrets Managerの認証方法を使用します。

```
aws redshift-data list-databases  

    --secret-arn arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn 
    --cluster-identifier mycluster-test 
    --database dev
```

次は、レスポンスの例です。

```
{
    "Databases": [
        "dev"
    ]
}
```

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、データベースを一覧表示します。この例では、一時的な認証情報認証方法を使用します。

```
aws redshift-data list-databases  
    --db-user myuser 
    --cluster-identifier mycluster-test 
    --database dev
```

次は、レスポンスの例です。

```
{
    "Databases": [
        "dev"
    ]
}
```

# データベース内のスキーマの一覧表示
<a name="data-api-calling-cli-list-schemas"></a>

`aws redshift-data list-schemas` AWS CLI コマンドを使用して、データベース内のスキーマを一覧表示します。

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、データベース内のスキーマを一覧表示します。この例では、AWS Secrets Managerの認証方法を使用します。

```
aws redshift-data list-schemas 
    --secret-arn arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn 
    --cluster-identifier mycluster-test 
    --database dev
```

次は、レスポンスの例です。

```
{
    "Schemas": [
        "information_schema",
        "pg_catalog",
        "pg_internal",
        "public"
    ]
}
```

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、データベース内のスキーマを一覧表示します。この例では、一時的な認証情報認証方法を使用します。

```
aws redshift-data list-schemas 
    --db-user mysuser 
    --cluster-identifier mycluster-test 
    --database dev
```

次は、レスポンスの例です。

```
{
    "Schemas": [
        "information_schema",
        "pg_catalog",
        "pg_internal",
        "public"
    ]
}
```

# データベース内のテーブルの一覧表示
<a name="data-api-calling-cli-list-tables"></a>

`aws redshift-data list-tables` AWS CLI コマンドを使用して、データベース内のテーブルを一覧表示します。

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、データベース内のテーブルを一覧表示します。この例では、AWS Secrets Managerの認証方法を使用します。

```
aws redshift-data list-tables 
    --secret-arn arn:aws:secretsmanager:us-west-2:123456789012:secret:myuser-secret-hKgPWn 
    --cluster-identifier mycluster-test 
    --database dev 
    --schema information_schema
```

次は、レスポンスの例です。

```
{
    "Tables": [
        {
            "name": "sql_features",
            "schema": "information_schema",
            "type": "SYSTEM TABLE"
        },
        {
            "name": "sql_implementation_info",
            "schema": "information_schema",
            "type": "SYSTEM TABLE"
        }
}
```

次の AWS CLI コマンドは、クラスターに対して SQL ステートメントを実行し、データベース内のテーブルを一覧表示します。この例では、一時的な認証情報認証方法を使用します。

```
aws redshift-data list-tables  

     --db-user myuser 
     --cluster-identifier mycluster-test 
     --database dev 
     --schema information_schema
```

次は、レスポンスの例です。

```
{
    "Tables": [
        {
            "name": "sql_features",
            "schema": "information_schema",
            "type": "SYSTEM TABLE"
        },
        {
            "name": "sql_implementation_info",
            "schema": "information_schema",
            "type": "SYSTEM TABLE"
        }
    ]
}
```