Skip to content

Commit

Permalink
updated v1 Redshift Java files
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon authored and DavidSouther committed Oct 8, 2024
1 parent 422a468 commit f2c0cbf
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 778 deletions.
71 changes: 6 additions & 65 deletions java/example_code/redshift/ConnectToCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,15 @@
// snippet-start:[redshift.java.ConnectToCluster.complete]

package connection;

import java.sql.*;
import java.util.Properties;

public class ConnectToCluster {
// Redshift driver:
// "jdbc:redshift://x.y.us-west-2.redshift.amazonaws.com:5439/dev";
static final String dbURL = "***jdbc cluster connection string ****";
static final String MasterUsername = "***master user name***";
static final String MasterUserPassword = "***master user password***";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// Dynamically load driver at runtime.
// Redshift JDBC 4.1 driver: com.amazon.redshift.jdbc41.Driver
// Redshift JDBC 4 driver: com.amazon.redshift.jdbc4.Driver
Class.forName("com.amazon.redshift.jdbc.Driver");

// Open a connection and define properties.
System.out.println("Connecting to database...");
Properties props = new Properties();

// Uncomment the following line if using a keystore.
// props.setProperty("ssl", "true");
props.setProperty("user", MasterUsername);
props.setProperty("password", MasterUserPassword);
conn = DriverManager.getConnection(dbURL, props);
/*
The AWS SDK for Java v1 is on path to deprection. See:
https://aws.amazon.com/blogs/developer/announcing-end-of-support-for-aws-sdk-for-java-v1-x-on-december-31-2025/
// Try a simple query.
System.out.println("Listing system tables...");
stmt = conn.createStatement();
String sql;
sql = "select * from information_schema.tables;";
ResultSet rs = stmt.executeQuery(sql);
See the V2 version here:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/redshift
*/

// Get the data from the result set.
while (rs.next()) {
// Retrieve two columns.
String catalog = rs.getString("table_catalog");
String name = rs.getString("table_name");

// Display values.
System.out.print("Catalog: " + catalog);
System.out.println(", Name: " + name);
}
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
// For convenience, handle all errors here.
ex.printStackTrace();
} finally {
// Finally block to close resources.
try {
if (stmt != null)
stmt.close();
} catch (Exception ex) {
} // nothing we can do
try {
if (conn != null)
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
System.out.println("Finished connectivity test.");
}
}
// snippet-end:[redshift.java.ConnectToCluster.complete]
75 changes: 0 additions & 75 deletions java/example_code/redshift/ConnectToClusterExample.java

This file was deleted.

100 changes: 6 additions & 94 deletions java/example_code/redshift/CreateAndDescribeSnapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,14 @@

// snippet-start:[redshift.java.CreateAndDescribeSnapshot.complete]

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.amazonaws.services.redshift.model.*;

public class CreateAndDescribeSnapshot {
/*
The AWS SDK for Java v1 is on path to deprection. See:
https://aws.amazon.com/blogs/developer/announcing-end-of-support-for-aws-sdk-for-java-v1-x-on-december-31-2025/
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 {
// 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("Waiting 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;
}
See the V2 version here:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/redshift
*/

}
// snippet-end:[redshift.java.CreateAndDescribeSnapshot.complete]
95 changes: 6 additions & 89 deletions java/example_code/redshift/CreateAndModifyCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,98 +16,15 @@
*/
// 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 {
/*
The AWS SDK for Java v1 is on path to deprection. See:
https://aws.amazon.com/blogs/developer/announcing-end-of-support-for-aws-sdk-for-java-v1-x-on-december-31-2025/
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("Waiting for cluster to become available.");
while (!clusterReady) {
DescribeClustersResult result = client.describeClusters(new DescribeClustersRequest()
.withClusterIdentifier(clusterIdentifier));
See the V2 version here:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/redshift
*/

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]
Loading

0 comments on commit f2c0cbf

Please sign in to comment.