与 AWS SDK或DescribeClusters一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

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

与 AWS SDK或DescribeClusters一起使用 CLI

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

CLI
AWS CLI

获取全部描述 ClustersThis 示例返回该账户所有集群的描述。默认情况下,输出采用 JSON format.Command:

aws redshift describe-clusters

结果:

{ "Clusters": [ { "NodeType": "dw.hs1.xlarge", "Endpoint": { "Port": 5439, "Address": "mycluster.coqoarplqhsn.us-east-1.redshift.amazonaws.com" }, "ClusterVersion": "1.0", "PubliclyAccessible": "true", "MasterUsername": "adminuser", "ClusterParameterGroups": [ { "ParameterApplyStatus": "in-sync", "ParameterGroupName": "default.redshift-1.0" } ], "ClusterSecurityGroups": [ { "Status": "active", "ClusterSecurityGroupName": "default" } ], "AllowVersionUpgrade": true, "VpcSecurityGroups": \[], "AvailabilityZone": "us-east-1a", "ClusterCreateTime": "2013-01-22T21:59:29.559Z", "PreferredMaintenanceWindow": "sat:03:30-sat:04:00", "AutomatedSnapshotRetentionPeriod": 1, "ClusterStatus": "available", "ClusterIdentifier": "mycluster", "DBName": "dev", "NumberOfNodes": 2, "PendingModifiedValues": {} } ], "ResponseMetadata": { "RequestId": "65b71cac-64df-11e2-8f5b-e90bd6c77476" } }

您也可以使用 --output text 选项以文本格式获取相同的信息。命令:

--output text 选项。命令:

选项。命令:

aws redshift describe-clusters --output text

结果:

dw.hs1.xlarge 1.0 true adminuser True us-east-1a 2013-01-22T21:59:29.559Z sat:03:30-sat:04:00 1 available mycluster dev 2 ENDPOINT 5439 mycluster.coqoarplqhsn.us-east-1.redshift.amazonaws.com in-sync default.redshift-1.0 active default PENDINGMODIFIEDVALUES RESPONSEMETADATA 934281a8-64df-11e2-b07c-f7fbdd006c67
Java
SDK适用于 Java 2.x
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

描述集群。

public static void waitForClusterReady(RedshiftClient redshiftClient, String clusterId) { boolean clusterReady = false; String clusterReadyStr; System.out.println("Waiting for cluster to become available. This may take a few mins."); try { DescribeClustersRequest clustersRequest = DescribeClustersRequest.builder() .clusterIdentifier(clusterId) .build(); long startTime = System.currentTimeMillis(); // Loop until the cluster is ready. while (!clusterReady) { DescribeClustersResponse clusterResponse = redshiftClient.describeClusters(clustersRequest); List<Cluster> clusterList = clusterResponse.clusters(); for (Cluster cluster : clusterList) { clusterReadyStr = cluster.clusterStatus(); if (clusterReadyStr.contains("available")) clusterReady = true; else { long elapsedTimeMillis = System.currentTimeMillis() - startTime; long elapsedSeconds = elapsedTimeMillis / 1000; long minutes = elapsedSeconds / 60; long seconds = elapsedSeconds % 60; System.out.printf("Elapsed Time: %02d:%02d - Waiting for cluster... %n", minutes, seconds); TimeUnit.SECONDS.sleep(5); } } } long elapsedTimeMillis = System.currentTimeMillis() - startTime; long elapsedSeconds = elapsedTimeMillis / 1000; long minutes = elapsedSeconds / 60; long seconds = elapsedSeconds % 60; System.out.println(String.format("Cluster is available! Total Elapsed Time: %02d:%02d", minutes, seconds)); } catch (RedshiftException | InterruptedException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DescribeClusters” 中的。

JavaScript
SDK对于 JavaScript (v3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

创建客户端。

import { RedshiftClient } from "@aws-sdk/client-redshift"; // Set the AWS Region. const REGION = "REGION"; //Set the Redshift Service Object const redshiftClient = new RedshiftClient({ region: REGION }); export { redshiftClient };

描述集群。

// Import required AWS SDK clients and commands for Node.js import { DescribeClustersCommand } from "@aws-sdk/client-redshift"; import { redshiftClient } from "./libs/redshiftClient.js"; const params = { ClusterIdentifier: "CLUSTER_NAME", }; const run = async () => { try { const data = await redshiftClient.send(new DescribeClustersCommand(params)); console.log("Success", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 有关API详细信息,请参阅 “AWS SDK for JavaScript API参考 DescribeClusters” 中的。

Kotlin
SDK对于 Kotlin 来说
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

描述集群。

suspend fun describeRedshiftClusters() { RedshiftClient { 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中的 Kotlin AWS SDK API 参考

Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class RedshiftWrapper: """ Encapsulates Amazon Redshift cluster operations. """ def __init__(self, redshift_client): """ :param redshift_client: A Boto3 Redshift client. """ self.client = redshift_client def describe_clusters(self, cluster_identifier): """ Describes a cluster. :param cluster_identifier: The cluster identifier. :return: A list of clusters. """ try: kwargs = {} if cluster_identifier: kwargs["ClusterIdentifier"] = cluster_identifier paginator = self.client.get_paginator("describe_clusters") clusters = [] for page in paginator.paginate(**kwargs): clusters.extend(page["Clusters"]) return clusters except ClientError as err: logging.error( "Couldn't describe a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise

以下代码实例化对象。 RedshiftWrapper

client = boto3.client("redshift") redhift_wrapper = RedshiftWrapper(client)
  • 有关API详细信息,请参阅DescribeClusters中的 AWS SDKPython (Boto3) API 参考。