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 的 AWS 开发工具包执行以下操作:

  • 列出现有的预留节点。

  • 根据指定的节点标准搜索新的预留节点产品。

  • 购买预留节点。

此示例首先选择与特定的节点类型和固定价格值相匹配的所有预留节点产品。然后,此示例遍历找到的每个产品,并允许您购买产品。

重要

如果您运行此示例并接受购买预留节点产品的提议,您将被收取与该产品相关的费用。

有关运行此示例的分步说明,请参阅使用 Eclipse 运行 Amazon Redshift 的 Java 示例。要获得这里未列出的其他节点类型和固定价格的信息,请更新代码并提供相应的节点类型和固定价格。

Copy
import java.io.DataInput; import java.io.DataInputStream; import java.io.IOException; import java.util.ArrayList; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.PropertiesCredentials; import com.amazonaws.services.redshift.AmazonRedshiftClient; import com.amazonaws.services.redshift.model.DescribeReservedNodeOfferingsRequest; import com.amazonaws.services.redshift.model.DescribeReservedNodeOfferingsResult; import com.amazonaws.services.redshift.model.DescribeReservedNodesResult; import com.amazonaws.services.redshift.model.PurchaseReservedNodeOfferingRequest; import com.amazonaws.services.redshift.model.ReservedNode; import com.amazonaws.services.redshift.model.ReservedNodeAlreadyExistsException; import com.amazonaws.services.redshift.model.ReservedNodeOffering; import com.amazonaws.services.redshift.model.ReservedNodeOfferingNotFoundException; import com.amazonaws.services.redshift.model.ReservedNodeQuotaExceededException; public class ListAndPurchaseReservedNodeOffering { public static AmazonRedshiftClient client; public static String nodeTypeToPurchase = "ds1.xlarge"; public static Double fixedPriceLimit = 10000.00; public static ArrayList<ReservedNodeOffering> matchingNodes = new ArrayList<ReservedNodeOffering>(); public static void main(String[] args) throws IOException { AWSCredentials credentials = new PropertiesCredentials( ListAndPurchaseReservedNodeOffering.class .getResourceAsStream("AwsCredentials.properties")); client = new AmazonRedshiftClient(credentials); try { listReservedNodes(); findReservedNodeOffer(); purchaseReservedNodeOffer(); } catch (Exception e) { System.err.println("Operation failed: " + e.getMessage()); } } private static void listReservedNodes() { DescribeReservedNodesResult result = client.describeReservedNodes(); System.out.println("Listing nodes already purchased."); for (ReservedNode node : result.getReservedNodes()) { printReservedNodeDetails(node); } } private static void findReservedNodeOffer() { DescribeReservedNodeOfferingsRequest request = new DescribeReservedNodeOfferingsRequest(); DescribeReservedNodeOfferingsResult result = client.describeReservedNodeOfferings(request); Integer count = 0; System.out.println("\nFinding nodes to purchase."); for (ReservedNodeOffering offering : result.getReservedNodeOfferings()) { if (offering.getNodeType().equals(nodeTypeToPurchase)){ if (offering.getFixedPrice() < fixedPriceLimit) { matchingNodes.add(offering); printOfferingDetails(offering); count +=1; } } } if (count == 0) { System.out.println("\nNo reserved node offering matches found."); } else { System.out.println("\nFound " + count + " matches."); } } private static void purchaseReservedNodeOffer() throws IOException { if (matchingNodes.size() == 0) { return; } else { System.out.println("\nPurchasing nodes."); for (ReservedNodeOffering offering : matchingNodes) { printOfferingDetails(offering); System.out.println("WARNING: purchasing this offering will incur costs."); System.out.println("Purchase this offering [Y or N]?"); DataInput in = new DataInputStream(System.in); String purchaseOpt = in.readLine(); if (purchaseOpt.equalsIgnoreCase("y")){ try { PurchaseReservedNodeOfferingRequest request = new PurchaseReservedNodeOfferingRequest() .withReservedNodeOfferingId(offering.getReservedNodeOfferingId()); ReservedNode reservedNode = client.purchaseReservedNodeOffering(request); printReservedNodeDetails(reservedNode); } catch (ReservedNodeAlreadyExistsException ex1){ } catch (ReservedNodeOfferingNotFoundException ex2){ } catch (ReservedNodeQuotaExceededException ex3){ } catch (Exception ex4){ } } } System.out.println("Finished."); } } private static void printOfferingDetails( ReservedNodeOffering offering) { System.out.println("\nOffering Match:"); System.out.format("Id: %s\n", offering.getReservedNodeOfferingId()); System.out.format("Node Type: %s\n", offering.getNodeType()); System.out.format("Fixed Price: %s\n", offering.getFixedPrice()); System.out.format("Offering Type: %s\n", offering.getOfferingType()); System.out.format("Duration: %s\n", offering.getDuration()); } private static void printReservedNodeDetails(ReservedNode node) { System.out.println("\nPurchased Node Details:"); System.out.format("Id: %s\n", node.getReservedNodeOfferingId()); System.out.format("State: %s\n", node.getState()); System.out.format("Node Type: %s\n", node.getNodeType()); System.out.format("Start Time: %s\n", node.getStartTime()); System.out.format("Fixed Price: %s\n", node.getFixedPrice()); System.out.format("Offering Type: %s\n", node.getOfferingType()); System.out.format("Duration: %s\n", node.getDuration()); } }