Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
使用 Amazon SDK 的 Amazon Redshift 代码示例
以下代码示例演示了如何将 Amazon Redshift 与 Amazon 软件开发工具包(SDK)结合使用。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。
场景是展示如何通过在同一服务中调用多个函数来完成特定任务任务的代码示例。
跨服务示例是指跨多个 Amazon Web Services工作的示例应用程序。
有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。
开始使用
以下代码示例展示了如何开始使用 Amazon Redshift。
- Java
-
- SDK for Java 2.x
-
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()));
}
}
- Python
-
- SDK for Python(Boto3)
-
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"))