将 CreateCluster 与 Amazon SDK 或命令行工具结合使用 - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

CreateCluster 与 Amazon SDK 或命令行工具结合使用

以下代码示例显示如何使用 CreateCluster

CLI
Amazon CLI

使用最少参数创建集群 此示例使用最少参数集创建集群。默认情况下,输出采用 JSON 格式。命令:

aws redshift create-cluster --node-type dw.hs1.xlarge --number-of-nodes 2 --master-username adminuser --master-user-password TopSecret1 --cluster-identifier mycluster

结果:

{ "Cluster": { "NodeType": "dw.hs1.xlarge", "ClusterVersion": "1.0", "PubliclyAccessible": "true", "MasterUsername": "adminuser", "ClusterParameterGroups": [ { "ParameterApplyStatus": "in-sync", "ParameterGroupName": "default.redshift-1.0" } ], "ClusterSecurityGroups": [ { "Status": "active", "ClusterSecurityGroupName": "default" } ], "AllowVersionUpgrade": true, "VpcSecurityGroups": \[], "PreferredMaintenanceWindow": "sat:03:30-sat:04:00", "AutomatedSnapshotRetentionPeriod": 1, "ClusterStatus": "creating", "ClusterIdentifier": "mycluster", "DBName": "dev", "NumberOfNodes": 2, "PendingModifiedValues": { "MasterUserPassword": "\****" } }, "ResponseMetadata": { "RequestId": "7cf4bcfc-64dd-11e2-bea9-49e0ce183f07" } }
  • 有关 API 详细信息,请参阅《Amazon CLI 命令参考》中的 CreateCluster

Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

创建集群。

public static void createCluster(RedshiftClient redshiftClient, String clusterId, String masterUsername, String masterUserPassword) { try { CreateClusterRequest clusterRequest = CreateClusterRequest.builder() .clusterIdentifier(clusterId) .masterUsername(masterUsername) .masterUserPassword(masterUserPassword) .nodeType("ra3.4xlarge") .publiclyAccessible(true) .numberOfNodes(2) .build(); CreateClusterResponse clusterResponse = redshiftClient.createCluster(clusterRequest); System.out.println("Created cluster " + clusterResponse.cluster().clusterIdentifier()); } catch (RedshiftException e) { System.err.println(e.getMessage()); System.exit(1); } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 CreateCluster

JavaScript
SDK for JavaScript (v3)
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

创建客户端。

const { RedshiftClient } = require("@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 { CreateClusterCommand } from "@aws-sdk/client-redshift"; import { redshiftClient } from "./libs/redshiftClient.js"; const params = { ClusterIdentifier: "CLUSTER_NAME", // Required NodeType: "NODE_TYPE", //Required MasterUsername: "MASTER_USER_NAME", // Required - must be lowercase MasterUserPassword: "MASTER_USER_PASSWORD", // Required - must contain at least one uppercase letter, and one number ClusterType: "CLUSTER_TYPE", // Required IAMRoleARN: "IAM_ROLE_ARN", // Optional - the ARN of an IAM role with permissions your cluster needs to access other AWS services on your behalf, such as Amazon S3. ClusterSubnetGroupName: "CLUSTER_SUBNET_GROUPNAME", //Optional - the name of a cluster subnet group to be associated with this cluster. Defaults to 'default' if not specified. DBName: "DATABASE_NAME", // Optional - defaults to 'dev' if not specified Port: "PORT_NUMBER", // Optional - defaults to '5439' if not specified }; const run = async () => { try { const data = await redshiftClient.send(new CreateClusterCommand(params)); console.log( "Cluster " + data.Cluster.ClusterIdentifier + " successfully created", ); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 CreateCluster

Kotlin
适用于 Kotlin 的 SDK
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

创建集群。

suspend fun createCluster(clusterId: String?, masterUsernameVal: String?, masterUserPasswordVal: String?) { val clusterRequest = CreateClusterRequest { clusterIdentifier = clusterId masterUsername = masterUsernameVal masterUserPassword = masterUserPasswordVal nodeType = "ds2.xlarge" publiclyAccessible = true numberOfNodes = 2 } RedshiftClient { region = "us-east-1" }.use { redshiftClient -> val clusterResponse = redshiftClient.createCluster(clusterRequest) println("Created cluster ${clusterResponse.cluster?.clusterIdentifier}") } }
  • 有关 API 详细信息,请参阅《Amazon SDK for Kotlin API 参考》中的 CreateCluster

Python
SDK for Python(Boto3)
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 Amazon 代码示例存储库中进行设置和运行。

class RedshiftWrapper: """ Encapsulates Amazon Redshift cluster operations. """ def __init__(self, redshift_client): """ :param redshift_client: A Boto3 Redshift client. """ self.client = redshift_client def create_cluster( self, cluster_identifier, node_type, master_username, master_user_password, publicly_accessible, number_of_nodes, ): """ Creates a cluster. :param cluster_identifier: The name of the cluster. :param node_type: The type of node in the cluster. :param master_username: The master username. :param master_user_password: The master user password. :param publicly_accessible: Whether the cluster is publicly accessible. :param number_of_nodes: The number of nodes in the cluster. :return: The cluster. """ try: cluster = self.client.create_cluster( ClusterIdentifier=cluster_identifier, NodeType=node_type, MasterUsername=master_username, MasterUserPassword=master_user_password, PubliclyAccessible=publicly_accessible, NumberOfNodes=number_of_nodes, ) return cluster except ClientError as err: logging.error( "Couldn't create 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 的详细信息,请参阅《Amazon SDK for Python(Boto3)API 参考》中的 CreateCluster

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。