Amazon Redshift 使用的代碼示例 AWS SDKs - AWS SDK 程式碼範例

AWS 文檔 AWS SDK示例 GitHub 回購中有更多SDK示例

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

Amazon Redshift 使用的代碼示例 AWS SDKs

下列程式碼範例會示範如何搭配 AWS 軟體開發套件使用 Amazon Redshift (SDK)。

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

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

Cross-service examples (跨服務範例) 是跨多個 AWS 服務執行的應用程式範例。

其他 資源

開始使用

下列程式碼範例顯示如何開始使用 Amazon Redshift。

Java
SDK對於爪哇 2.x
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.redshift.RedshiftClient; import software.amazon.awssdk.services.redshift.paginators.DescribeClustersIterable; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class HelloRedshift { public static void main(String[] args) { Region region = Region.US_EAST_1; RedshiftClient redshiftClient = RedshiftClient.builder() .region(region) .build(); listClustersPaginator(redshiftClient); } public static void listClustersPaginator(RedshiftClient redshiftClient) { DescribeClustersIterable clustersIterable = redshiftClient.describeClustersPaginator(); clustersIterable.stream() .flatMap(r -> r.clusters().stream()) .forEach(cluster -> System.out .println(" Cluster identifier: " + cluster.clusterIdentifier() + " status = " + cluster.clusterStatus())); } }
  • 如需詳API細資訊,請參閱AWS SDK for Java 2.x API參考describeClusters中的。

Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

import boto3 def hello_redshift(redshift_client): """ Use the AWS SDK for Python (Boto3) to create an Amazon Redshift client and list the clusters in your account. This list might be empty if you haven't created any clusters. This example uses the default settings specified in your shared credentials and config files. :param redshift_client: A Boto3 Redshift Client object. """ print("Hello, Redshift! Let's list your clusters:") paginator = redshift_client.get_paginator("describe_clusters") clusters = [] for page in paginator.paginate(): clusters.extend(page["Clusters"]) print(f"{len(clusters)} cluster(s) were found.") for cluster in clusters: print(f" {cluster['ClusterIdentifier']}") if __name__ == "__main__": hello_redshift(boto3.client("redshift"))
  • 如需詳API細資訊,請參閱describeClustersAWS SDK的〈〉以取得 Python (Boto3) API 參考資料。