Amazon Redshift
管理指南 (API Version 2012-12-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用适用于 Java 的 AWS 开发工具包管理群集子网组

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

  • 创建群集子网组。

  • 列出群集子网组的元数据。

  • 修改群集子网组。

有关运行以下示例的分步说明,请参阅 使用 Eclipse 运行 Amazon Redshift 的 Java 示例。您需要更新代码并提供群集子网组的名称和两个子网标识符。

Copy
import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.PropertiesCredentials; import com.amazonaws.services.redshift.AmazonRedshiftClient; import com.amazonaws.services.redshift.model.*; public class CreateAndModifyClusterSubnetGroup { public static AmazonRedshiftClient client; public static String clusterSubnetGroupName = "***provide a cluster subnet group name ****"; // You can use the VPC console to find subnet IDs to use. public static String subnetId1 = "***provide a subnet ID****"; public static String subnetId2 = "***provide a subnet ID****"; public static void main(String[] args) throws IOException { AWSCredentials credentials = new PropertiesCredentials( CreateAndModifyClusterSubnetGroup.class .getResourceAsStream("AwsCredentials.properties")); client = new AmazonRedshiftClient(credentials); try { createClusterSubnetGroup(); describeClusterSubnetGroups(); modifyClusterSubnetGroup(); } catch (Exception e) { System.err.println("Operation failed: " + e.getMessage()); } } private static void createClusterSubnetGroup() { CreateClusterSubnetGroupRequest request = new CreateClusterSubnetGroupRequest() .withClusterSubnetGroupName(clusterSubnetGroupName) .withDescription("my cluster subnet group") .withSubnetIds(subnetId1); client.createClusterSubnetGroup(request); System.out.println("Created cluster subnet group: " + clusterSubnetGroupName); } private static void modifyClusterSubnetGroup() { // Get existing subnet list. DescribeClusterSubnetGroupsRequest request1 = new DescribeClusterSubnetGroupsRequest() .withClusterSubnetGroupName(clusterSubnetGroupName); DescribeClusterSubnetGroupsResult result1 = client.describeClusterSubnetGroups(request1); List<String> subnetNames = new ArrayList<String>(); // We can work with just the first group returned since we requested info about one group. for (Subnet subnet : result1.getClusterSubnetGroups().get(0).getSubnets()) { subnetNames.add(subnet.getSubnetIdentifier()); } // Add to existing subnet list. subnetNames.add(subnetId2); ModifyClusterSubnetGroupRequest request = new ModifyClusterSubnetGroupRequest() .withClusterSubnetGroupName(clusterSubnetGroupName) .withSubnetIds(subnetNames); ClusterSubnetGroup result2 = client.modifyClusterSubnetGroup(request); System.out.println("\nSubnet group modified."); printResultSubnetGroup(result2); } private static void describeClusterSubnetGroups() { DescribeClusterSubnetGroupsRequest request = new DescribeClusterSubnetGroupsRequest() .withClusterSubnetGroupName(clusterSubnetGroupName); DescribeClusterSubnetGroupsResult result = client.describeClusterSubnetGroups(request); printResultSubnetGroups(result); } private static void printResultSubnetGroups(DescribeClusterSubnetGroupsResult result) { if (result == null) { System.out.println("\nDescribe cluster subnet groups result is null."); return; } for (ClusterSubnetGroup group : result.getClusterSubnetGroups()) { printResultSubnetGroup(group); } } private static void printResultSubnetGroup(ClusterSubnetGroup group) { System.out.format("Name: %s, Description: %s\n", group.getClusterSubnetGroupName(), group.getDescription()); for (Subnet subnet : group.getSubnets()) { System.out.format(" Subnet: %s, %s, %s\n", subnet.getSubnetIdentifier(), subnet.getSubnetAvailabilityZone().getName(), subnet.getSubnetStatus()); } } }