|
3 | 3 |
|
4 | 4 | // snippet-start:[redshift.java.CreateAndDescribeSnapshot.complete] |
5 | 5 |
|
6 | | -import java.io.IOException; |
7 | | -import java.text.SimpleDateFormat; |
8 | | -import java.util.Date; |
9 | | - |
10 | | -import com.amazonaws.services.redshift.model.*; |
11 | | - |
12 | 6 | public class CreateAndDescribeSnapshot { |
| 7 | + /* |
| 8 | + The AWS SDK for Java v1 is approaching end-of-support. For more information, see: |
| 9 | + https://aws.amazon.com/blogs/developer/announcing-end-of-support-for-aws-sdk-for-java-v1-x-on-december-31-2025/ |
13 | 10 |
|
14 | | - public static AmazonRedshift client; |
15 | | - public static String clusterIdentifier = "***provide a cluster identifier***"; |
16 | | - public static long sleepTime = 20; |
17 | | - |
18 | | - public static void main(String[] args) throws IOException { |
19 | | - |
20 | | - // Default client using the {@link |
21 | | - // com.amazonaws.auth.DefaultAWSCredentialsProviderChain} |
22 | | - client = AmazonRedshiftClientBuilder.defaultClient(); |
23 | | - |
24 | | - try { |
25 | | - // Unique snapshot identifier |
26 | | - String snapshotId = "my-snapshot-" + (new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")).format(new Date()); |
27 | | - |
28 | | - Date createDate = createManualSnapshot(snapshotId); |
29 | | - waitForSnapshotAvailable(snapshotId); |
30 | | - describeSnapshots(); |
31 | | - deleteManualSnapshotsBefore(createDate); |
32 | | - describeSnapshots(); |
33 | | - |
34 | | - } catch (Exception e) { |
35 | | - System.err.println("Operation failed: " + e.getMessage()); |
36 | | - } |
37 | | - } |
38 | | - |
39 | | - private static Date createManualSnapshot(String snapshotId) { |
40 | | - |
41 | | - CreateClusterSnapshotRequest request = new CreateClusterSnapshotRequest() |
42 | | - .withClusterIdentifier(clusterIdentifier) |
43 | | - .withSnapshotIdentifier(snapshotId); |
44 | | - Snapshot snapshot = client.createClusterSnapshot(request); |
45 | | - System.out.format("Created cluster snapshot: %s\n", snapshotId); |
46 | | - return snapshot.getSnapshotCreateTime(); |
47 | | - } |
48 | | - |
49 | | - private static void describeSnapshots() { |
50 | | - |
51 | | - DescribeClusterSnapshotsRequest request = new DescribeClusterSnapshotsRequest() |
52 | | - .withClusterIdentifier(clusterIdentifier); |
53 | | - DescribeClusterSnapshotsResult result = client.describeClusterSnapshots(request); |
54 | | - |
55 | | - printResultSnapshots(result); |
56 | | - } |
57 | | - |
58 | | - private static void deleteManualSnapshotsBefore(Date creationDate) { |
59 | | - |
60 | | - DescribeClusterSnapshotsRequest request = new DescribeClusterSnapshotsRequest() |
61 | | - .withEndTime(creationDate) |
62 | | - .withClusterIdentifier(clusterIdentifier) |
63 | | - .withSnapshotType("manual"); |
64 | | - |
65 | | - DescribeClusterSnapshotsResult result = client.describeClusterSnapshots(request); |
66 | | - |
67 | | - for (Snapshot s : result.getSnapshots()) { |
68 | | - DeleteClusterSnapshotRequest deleteRequest = new DeleteClusterSnapshotRequest() |
69 | | - .withSnapshotIdentifier(s.getSnapshotIdentifier()); |
70 | | - Snapshot deleteResult = client.deleteClusterSnapshot(deleteRequest); |
71 | | - System.out.format("Deleted snapshot %s\n", deleteResult.getSnapshotIdentifier()); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - private static void printResultSnapshots(DescribeClusterSnapshotsResult result) { |
76 | | - System.out.println("\nSnapshot listing:"); |
77 | | - for (Snapshot snapshot : result.getSnapshots()) { |
78 | | - System.out.format("Identifier: %s\n", snapshot.getSnapshotIdentifier()); |
79 | | - System.out.format("Snapshot type: %s\n", snapshot.getSnapshotType()); |
80 | | - System.out.format("Snapshot create time: %s\n", snapshot.getSnapshotCreateTime()); |
81 | | - System.out.format("Snapshot status: %s\n\n", snapshot.getStatus()); |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - private static Boolean waitForSnapshotAvailable(String snapshotId) throws InterruptedException { |
86 | | - Boolean snapshotAvailable = false; |
87 | | - System.out.println("Waiting for snapshot to become available."); |
88 | | - while (!snapshotAvailable) { |
89 | | - DescribeClusterSnapshotsResult result = client |
90 | | - .describeClusterSnapshots(new DescribeClusterSnapshotsRequest() |
91 | | - .withSnapshotIdentifier(snapshotId)); |
92 | | - String status = (result.getSnapshots()).get(0).getStatus(); |
93 | | - if (status.equalsIgnoreCase("available")) { |
94 | | - snapshotAvailable = true; |
95 | | - } else { |
96 | | - System.out.print("."); |
97 | | - Thread.sleep(sleepTime * 1000); |
98 | | - } |
99 | | - } |
100 | | - return snapshotAvailable; |
101 | | - } |
| 11 | + See the V2 version here: |
| 12 | + https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/redshift |
| 13 | + */ |
102 | 14 |
|
103 | 15 | } |
104 | 16 | // snippet-end:[redshift.java.CreateAndDescribeSnapshot.complete] |
0 commit comments