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 开发工具包管理快照

下面的示例显示了涉及快照的常见操作:

  • 创建群集的手动快照。

  • 显示与群集的所有快照有关的信息。

  • 删除群集的手动快照。

在此示例中,系统启动了群集的快照。成功创建快照后,系统将删除在此新快照之前为该群集创建的所有手动快照。在启动创建手动快照的过程时,该快照不会立即可供使用。因此,此示例通过调用 describeClusterSnapshot 方法,使用循环来轮询快照的状态。通常,快照在启动片刻之后才可供使用。有关快照的更多信息,请参阅 Amazon Redshift 快照

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

Copy
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.PropertiesCredentials; import com.amazonaws.services.redshift.AmazonRedshiftClient; import com.amazonaws.services.redshift.model.CreateClusterSnapshotRequest; import com.amazonaws.services.redshift.model.DeleteClusterSnapshotRequest; import com.amazonaws.services.redshift.model.DescribeClusterSnapshotsRequest; import com.amazonaws.services.redshift.model.DescribeClusterSnapshotsResult; import com.amazonaws.services.redshift.model.Snapshot; public class CreateAndDescribeSnapshot { public static AmazonRedshiftClient client; public static String clusterIdentifier = "***provide cluster identifier***"; public static long sleepTime = 10; public static void main(String[] args) throws IOException { AWSCredentials credentials = new PropertiesCredentials( CreateAndDescribeSnapshot.class .getResourceAsStream("AwsCredentials.properties")); client = new AmazonRedshiftClient(credentials); try { // Unique snapshot identifier String snapshotId = "my-snapshot-" + (new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")).format(new Date()); Date createDate = createManualSnapshot(snapshotId); waitForSnapshotAvailable(snapshotId); describeSnapshots(); deleteManualSnapshotsBefore(createDate); describeSnapshots(); } catch (Exception e) { System.err.println("Operation failed: " + e.getMessage()); } } private static Date createManualSnapshot(String snapshotId) { CreateClusterSnapshotRequest request = new CreateClusterSnapshotRequest() .withClusterIdentifier(clusterIdentifier) .withSnapshotIdentifier(snapshotId); Snapshot snapshot = client.createClusterSnapshot(request); System.out.format("Created cluster snapshot: %s\n", snapshotId); return snapshot.getSnapshotCreateTime(); } private static void describeSnapshots() { DescribeClusterSnapshotsRequest request = new DescribeClusterSnapshotsRequest() .withClusterIdentifier(clusterIdentifier); DescribeClusterSnapshotsResult result = client.describeClusterSnapshots(request); printResultSnapshots(result); } private static void deleteManualSnapshotsBefore(Date creationDate) { DescribeClusterSnapshotsRequest request = new DescribeClusterSnapshotsRequest() .withEndTime(creationDate) .withClusterIdentifier(clusterIdentifier) .withSnapshotType("manual"); DescribeClusterSnapshotsResult result = client.describeClusterSnapshots(request); for (Snapshot s : result.getSnapshots()) { DeleteClusterSnapshotRequest deleteRequest = new DeleteClusterSnapshotRequest() .withSnapshotIdentifier(s.getSnapshotIdentifier()); Snapshot deleteResult = client.deleteClusterSnapshot(deleteRequest); System.out.format("Deleted snapshot %s\n", deleteResult.getSnapshotIdentifier()); } } private static void printResultSnapshots(DescribeClusterSnapshotsResult result) { System.out.println("\nSnapshot listing:"); for (Snapshot snapshot : result.getSnapshots()) { System.out.format("Identifier: %s\n", snapshot.getSnapshotIdentifier()); System.out.format("Snapshot type: %s\n", snapshot.getSnapshotType()); System.out.format("Snapshot create time: %s\n", snapshot.getSnapshotCreateTime()); System.out.format("Snapshot status: %s\n\n", snapshot.getStatus()); } } private static Boolean waitForSnapshotAvailable(String snapshotId) throws InterruptedException { Boolean snapshotAvailable = false; System.out.println("Wating for snapshot to become available."); while (!snapshotAvailable) { DescribeClusterSnapshotsResult result = client.describeClusterSnapshots(new DescribeClusterSnapshotsRequest() .withSnapshotIdentifier(snapshotId)); String status = (result.getSnapshots()).get(0).getStatus(); if (status.equalsIgnoreCase("available")) { snapshotAvailable = true; } else { System.out.print("."); Thread.sleep(sleepTime*1000); } } return snapshotAvailable; } }