

Doc AWS SDK Examples GitHub リポジトリには、他にも SDK の例があります。 [AWS](https://github.com/awsdocs/aws-doc-sdk-examples)

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# SDK for Kotlin を使用した Amazon Redshift の例
<a name="kotlin_1_redshift_code_examples"></a>

次のコード例は、Amazon Redshift で AWS SDK for Kotlin を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

*アクション*はより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

*シナリオ*は、1 つのサービス内から、または他の AWS のサービスと組み合わせて複数の関数を呼び出し、特定のタスクを実行する方法を示すコード例です。

各例には完全なソースコードへのリンクが含まれており、コードの設定方法と実行方法に関する手順を確認できます。

**Topics**
+ [アクション](#actions)
+ [シナリオ](#scenarios)

## アクション
<a name="actions"></a>

### `CreateCluster`
<a name="redshift_CreateCluster_kotlin_1_topic"></a>

次のコード例は、`CreateCluster` を使用する方法を示しています。

**SDK for Kotlin**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/redshift#code-examples)での設定と実行の方法を確認してください。
クラスターを作成します。  

```
suspend fun createCluster(
    clusterId: String?,
    masterUsernameVal: String?,
    masterUserPasswordVal: String?,
) {
    val clusterRequest =
        CreateClusterRequest {
            clusterIdentifier = clusterId
            availabilityZone = "us-east-1a"
            masterUsername = masterUsernameVal
            masterUserPassword = masterUserPasswordVal
            nodeType = "ra3.4xlarge"
            publiclyAccessible = true
            numberOfNodes = 2
        }

    RedshiftClient.fromEnvironment { region = "us-east-1" }.use { redshiftClient ->
        val clusterResponse = redshiftClient.createCluster(clusterRequest)
        println("Created cluster ${clusterResponse.cluster?.clusterIdentifier}")
    }
}
```
+  API の詳細については、*AWS SDK for Kotlin API リファレンス*の「[CreateCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

### `DeleteCluster`
<a name="redshift_DeleteCluster_kotlin_1_topic"></a>

次のコード例は、`DeleteCluster` を使用する方法を示しています。

**SDK for Kotlin**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/redshift#code-examples)での設定と実行の方法を確認してください。
クラスターを削除します。  

```
suspend fun deleteRedshiftCluster(clusterId: String?) {
    val request =
        DeleteClusterRequest {
            clusterIdentifier = clusterId
            skipFinalClusterSnapshot = true
        }

    RedshiftClient.fromEnvironment { region = "us-west-2" }.use { redshiftClient ->
        val response = redshiftClient.deleteCluster(request)
        println("The status is ${response.cluster?.clusterStatus}")
    }
}
```
+  API の詳細については、*AWS SDK for Kotlin API リファレンス*の「[DeleteCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

### `DescribeClusters`
<a name="redshift_DescribeClusters_kotlin_1_topic"></a>

次のコード例は、`DescribeClusters` を使用する方法を示しています。

**SDK for Kotlin**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/redshift#code-examples)での設定と実行の方法を確認してください。
クラスターを記述します。  

```
suspend fun describeRedshiftClusters() {
    RedshiftClient.fromEnvironment { region = "us-west-2" }.use { redshiftClient ->
        val clusterResponse = redshiftClient.describeClusters(DescribeClustersRequest {})
        val clusterList = clusterResponse.clusters

        if (clusterList != null) {
            for (cluster in clusterList) {
                println("Cluster database name is ${cluster.dbName}")
                println("Cluster status is ${cluster.clusterStatus}")
            }
        }
    }
}
```
+  API の詳細については、*AWS SDK for Kotlin API リファレンス*の「[DescribeClusters](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

### `ModifyCluster`
<a name="redshift_ModifyCluster_kotlin_1_topic"></a>

次のコード例は、`ModifyCluster` を使用する方法を示しています。

**SDK for Kotlin**  
 GitHub には、その他のリソースもあります。用例一覧を検索し、[AWS コード例リポジトリ](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/redshift#code-examples)での設定と実行の方法を確認してください。
クラスターを変更します。  

```
suspend fun modifyCluster(clusterId: String?) {
    val modifyClusterRequest =
        ModifyClusterRequest {
            clusterIdentifier = clusterId
            preferredMaintenanceWindow = "wed:07:30-wed:08:00"
        }

    RedshiftClient { region = "us-west-2" }.use { redshiftClient ->
        val clusterResponse = redshiftClient.modifyCluster(modifyClusterRequest)
        println(
            "The modified cluster was successfully modified and has ${clusterResponse.cluster?.preferredMaintenanceWindow} as the maintenance window",
        )
    }
}
```
+  API の詳細については、*AWS SDK for Kotlin API リファレンス*の「[ModifyCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)」を参照してください。

## シナリオ
<a name="scenarios"></a>

### Amazon Redshift データ追跡用のウェブアプリケーションの作成
<a name="cross_RedshiftDataTracker_kotlin_1_topic"></a>

次のコード例は、Amazon Redshift データベースを使用して、作業項目を追跡してレポートするウェブアプリケーションを作成する方法を示しています。

**SDK for Kotlin**  
 Amazon Redshift データベースに保存されている作業項目を追跡してレポートするウェブアプリケーションを作成する方法を説明します。  
 Amazon Redshift サーバーレスデータをクエリする Spring REST API と React アプリケーションで使用するための完全なソースコードと設定方法については、[GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/usecases/creating_redshift_application) にある詳しい事例を参照してください。  

**この例で使用されているサービス**
+ Amazon Redshift
+ Amazon SES