使用 Amazon SDK for Java 管理集群 - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

使用 Amazon SDK for Java 管理集群

以下 Java 代码示例演示了常见的集群管理操作,其中包括:

  • 创建集群。

  • 列出与集群有关的元数据。

  • 修改配置选项。

提出创建集群的请求之后,您必须等到集群处于 available 状态后才能对其进行修改。本示例通过 describeClusters 方法,使用循环来定期检查集群的状态。当集群可供使用时,集群的首选维护时段会发生变化。

有关运行以下示例的分步说明,请参阅 使用 Eclipse 运行 Amazon Redshift 的 Java 示例。您需要更新代码并指定集群标识符。

/** * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ // snippet-sourcedescription:[CreateAndModifyCluster demonstrates how to create and modify an Amazon Redshift cluster.] // snippet-service:[redshift] // snippet-keyword:[Java] // snippet-keyword:[Amazon Redshift] // snippet-keyword:[Code Sample] // snippet-keyword:[CreateCluster] // snippet-keyword:[DescribeClusters] // snippet-keyword:[ModifyCluster] // snippet-sourcetype:[full-example] // snippet-sourcedate:[2019-02-01] // snippet-sourceauthor:[AWS] // snippet-start:[redshift.java.CreateAndModifyCluster.complete] package com.amazonaws.services.redshift; import java.io.IOException; import com.amazonaws.services.redshift.AmazonRedshift; import com.amazonaws.services.redshift.AmazonRedshiftClientBuilder; import com.amazonaws.services.redshift.model.*; public class CreateAndModifyCluster { public static AmazonRedshift client; public static String clusterIdentifier = "***provide a cluster identifier***"; public static long sleepTime = 20; public static void main(String[] args) throws IOException { // Default client using the {@link com.amazonaws.auth.DefaultAWSCredentialsProviderChain} client = AmazonRedshiftClientBuilder.defaultClient(); try { createCluster(); waitForClusterReady(); describeClusters(); modifyCluster(); describeClusters(); } catch (Exception e) { System.err.println("Operation failed: " + e.getMessage()); } } private static void createCluster() { CreateClusterRequest request = new CreateClusterRequest() .withClusterIdentifier(clusterIdentifier) .withMasterUsername("masteruser") .withMasterUserPassword("12345678Aa") .withNodeType("ds2.xlarge") .withNumberOfNodes(2) .withClusterSubnetGroupName("subnetgroup1"); Cluster createResponse = client.createCluster(request); System.out.println("Created cluster " + createResponse.getClusterIdentifier()); } private static void describeClusters() { DescribeClustersRequest request = new DescribeClustersRequest() .withClusterIdentifier(clusterIdentifier); DescribeClustersResult result = client.describeClusters(request); printResult(result); } private static void modifyCluster() { ModifyClusterRequest request = new ModifyClusterRequest() .withClusterIdentifier(clusterIdentifier) .withPreferredMaintenanceWindow("wed:07:30-wed:08:00"); client.modifyCluster(request); System.out.println("Modified cluster " + clusterIdentifier); } private static void printResult(DescribeClustersResult result) { if (result == null) { System.out.println("Describe clusters result is null."); return; } System.out.println("Cluster property:"); System.out.format("Preferred Maintenance Window: %s\n", result.getClusters().get(0).getPreferredMaintenanceWindow()); } private static void waitForClusterReady() throws InterruptedException { Boolean clusterReady = false; System.out.println("Wating for cluster to become available."); while (!clusterReady) { DescribeClustersResult result = client.describeClusters(new DescribeClustersRequest() .withClusterIdentifier(clusterIdentifier)); String status = (result.getClusters()).get(0).getClusterStatus(); if (status.equalsIgnoreCase("available")) { clusterReady = true; } else { System.out.print("."); Thread.sleep(sleepTime*1000); } } } } // snippet-end:[redshift.java.CreateAndModifyCluster.complete]