

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用適用於 Kotlin 的 SDK 的 Amazon Redshift 範例
<a name="kotlin_1_redshift_code_examples"></a>

下列程式碼範例示範如何使用適用於 Kotlin 的 AWS SDK 搭配 Amazon Redshift 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

*案例*是向您展示如何呼叫服務中的多個函數或與其他 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 詳細資訊，請參閱《適用於 Kotlin 的AWS SDK API 參考》**中的 [CreateCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `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 詳細資訊，請參閱《適用於 Kotlin 的AWS SDK API 參考》**中的 [DeleteCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `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 詳細資訊，請參閱《適用於 Kotlin 的 AWS  SDK API 參考》**中的 [DescribeClusters](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `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 詳細資訊，請參閱《適用於 Kotlin 的AWS SDK API 參考》**中的 [ModifyCluster](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

## 案例
<a name="scenarios"></a>

### 建立用於追蹤 Amazon Redshift 資料的 Web 應用程式
<a name="cross_RedshiftDataTracker_kotlin_1_topic"></a>

下列程式碼範例示範如何使用 Amazon Redshift 資料庫，建立可追蹤和報告工作項目的 Web 應用程式。

**SDK for Kotlin**  
 說明如何建立可追蹤和報告存放在 Amazon Redshift 資料庫中的工作項目的 Web 應用程式。  
 如需完整的原始碼以及如何設定 Spring REST API 以查詢 Amazon Redshift 資料並供 React 應用程式使用的說明，請參閱 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/usecases/creating_redshift_application) 上的完整範例。  

**此範例中使用的服務**
+ Amazon Redshift
+ Amazon SES