

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 SDK for Kotlin 的 Amazon Redshift 示例
<a name="kotlin_1_redshift_code_examples"></a>

以下代码示例向您展示了如何使用适用于 Kotlin 的 AWS 软件开发工具包和 Amazon Redshift 来执行操作和实现常见场景。

*操作*是大型程序的代码摘录，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

*场景*是向您演示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。

每个示例都包含一个指向完整源代码的链接，您可以从中找到有关如何在上下文中设置和运行代码的说明。

**Topics**
+ [操作](#actions)
+ [场景](#scenarios)

## 操作
<a name="actions"></a>

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

以下代码示例演示了如何使用 `CreateCluster`。

**适用于 Kotlin 的 SDK**  
 还有更多相关信息 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 的详细信息，请参阅适用[CreateCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)于 K *otlin 的AWS SDK API 参考*。

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

以下代码示例演示了如何使用 `DeleteCluster`。

**适用于 Kotlin 的 SDK**  
 还有更多相关信息 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 的详细信息，请参阅适用[DeleteCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)于 K *otlin 的AWS SDK API 参考*。

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

以下代码示例演示了如何使用 `DescribeClusters`。

**适用于 Kotlin 的 SDK**  
 还有更多相关信息 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 的详细信息，请参阅适用[DescribeClusters](https://sdk.amazonaws.com/kotlin/api/latest/index.html)于 K *otlin 的AWS SDK API 参考*。

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

以下代码示例演示了如何使用 `ModifyCluster`。

**适用于 Kotlin 的 SDK**  
 还有更多相关信息 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 的详细信息，请参阅适用[ModifyCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)于 K *otlin 的AWS SDK API 参考*。

## 场景
<a name="scenarios"></a>

### 创建 Web 应用程序来跟踪 Amazon Redshift 数据
<a name="cross_RedshiftDataTracker_kotlin_1_topic"></a>

以下代码示例演示如何使用 Amazon Redshift 数据库创建用于跟踪和报告工作项的 Web 应用程序。

**适用于 Kotlin 的 SDK**  
 展示如何创建 Web 应用程序来跟踪与报告存储与 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