搭DescribeClusters配 AWS SDK或使用 CLI - AWS SDK 程式碼範例

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

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

DescribeClusters配 AWS SDK或使用 CLI

下列程式碼範例會示範如何使用DescribeClusters

CLI
AWS CLI

[取得全部說明] ClustersThis 範例會傳回該帳戶之所有叢集的說明。默認情況下,輸出JSON格式為。命令:

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
  • 如需詳API細資訊,請參閱AWS CLI 指令參考DescribeClusters中的。

Java
SDK對於爪哇 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 (3)
注意

還有更多關於 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對於科特林
注意

還有更多關於 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關詳細資訊,請參閱DescribeClustersAWS SDK的以取得 Kotlin API 的參考資料

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

還有更多關於 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細資訊,請參閱DescribeClustersAWS SDK的《Python (博多 3) API 參考資料》。