Skip to content

Commit 1ccb221

Browse files
committed
updated Actions
1 parent bd651d6 commit 1ccb221

File tree

3 files changed

+137
-85
lines changed

3 files changed

+137
-85
lines changed

javav2/example_code/neptune/src/main/java/com/example/neptune/HelloNeptune.java

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
package com.example.neptune;
55

6+
import org.reactivestreams.Subscriber;
7+
import org.reactivestreams.Subscription;
68
import software.amazon.awssdk.core.async.SdkPublisher;
79
import software.amazon.awssdk.services.neptune.NeptuneAsyncClient;
810
import software.amazon.awssdk.services.neptune.model.DescribeDbClustersRequest;
911
import software.amazon.awssdk.services.neptune.model.DescribeDbClustersResponse;
1012
import java.util.concurrent.CompletableFuture;
11-
import java.util.concurrent.ExecutionException;
1213

1314
// snippet-start:[neptune.java2.hello.main]
1415
/**
@@ -20,41 +21,52 @@
2021
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
2122
*/
2223
public class HelloNeptune {
23-
24-
private static final String NEPTUNE_ENDPOINT = "https://[Specify-Your-Endpoint]:8182";
25-
2624
public static void main(String[] args) {
2725
NeptuneAsyncClient neptuneClient = NeptuneAsyncClient.create();
28-
describeDbCluster(neptuneClient);
26+
describeDbCluster(neptuneClient).join(); // This ensures the async code runs to completion
2927
}
3028

31-
/**
32-
* Describes the Amazon Neptune DB clusters using a paginator.
33-
*
34-
* @param neptuneClient the Amazon Neptune asynchronous client
35-
*/
36-
public static void describeDbCluster(NeptuneAsyncClient neptuneClient) {
29+
public static CompletableFuture<Void> describeDbCluster(NeptuneAsyncClient neptuneClient) {
3730
DescribeDbClustersRequest request = DescribeDbClustersRequest.builder()
38-
.maxRecords(20) // Optional: limit per page
31+
.maxRecords(20)
3932
.build();
4033

41-
SdkPublisher<DescribeDbClustersResponse> paginator= neptuneClient.describeDBClustersPaginator(request);
42-
CompletableFuture<Void> future = paginator
43-
.subscribe(response -> {
44-
for (var cluster : response.dbClusters()) {
45-
System.out.println("Cluster Identifier: " + cluster.dbClusterIdentifier());
46-
System.out.println("Status: " + cluster.status());
47-
}
34+
SdkPublisher<DescribeDbClustersResponse> paginator = neptuneClient.describeDBClustersPaginator(request);
35+
CompletableFuture<Void> future = new CompletableFuture<>();
36+
37+
paginator.subscribe(new Subscriber<DescribeDbClustersResponse>() {
38+
private Subscription subscription;
39+
40+
@Override
41+
public void onSubscribe(Subscription s) {
42+
this.subscription = s;
43+
s.request(Long.MAX_VALUE); // request all items
44+
}
45+
46+
@Override
47+
public void onNext(DescribeDbClustersResponse response) {
48+
response.dbClusters().forEach(cluster -> {
49+
System.out.println("Cluster Identifier: " + cluster.dbClusterIdentifier());
50+
System.out.println("Status: " + cluster.status());
4851
});
52+
}
53+
54+
@Override
55+
public void onError(Throwable t) {
56+
future.completeExceptionally(t);
57+
}
58+
59+
@Override
60+
public void onComplete() {
61+
future.complete(null);
62+
}
63+
});
4964

50-
// Wait for completion and handle errors
51-
try {
52-
future.get(); // Waits for all pages to be processed
53-
} catch (InterruptedException | ExecutionException e) {
54-
System.err.println("Failed to describe DB clusters: " + e.getMessage());
55-
} finally {
65+
return future.whenComplete((result, throwable) -> {
5666
neptuneClient.close();
57-
}
67+
if (throwable != null) {
68+
System.err.println("Error describing DB clusters: " + throwable.getMessage());
69+
}
70+
});
5871
}
59-
}
60-
// snippet-end:[neptune.java2.hello.main]
72+
}// snippet-end:[neptune.java2.hello.main]

javav2/example_code/neptune/src/main/java/com/example/neptune/scenerio/NeptuneActions.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@
1313
import software.amazon.awssdk.services.ec2.Ec2Client;
1414
import software.amazon.awssdk.services.ec2.model.DescribeSubnetsRequest;
1515
import software.amazon.awssdk.services.ec2.model.DescribeSubnetsResponse;
16+
import software.amazon.awssdk.services.ec2.model.DescribeVpcsRequest;
17+
import software.amazon.awssdk.services.ec2.model.Filter;
18+
import software.amazon.awssdk.services.ec2.model.DescribeVpcsResponse;
1619
import software.amazon.awssdk.services.ec2.model.Subnet;
20+
import software.amazon.awssdk.services.ec2.model.Vpc;
1721
import software.amazon.awssdk.services.neptune.NeptuneAsyncClient;
1822
import software.amazon.awssdk.services.neptune.NeptuneClient;
1923
import software.amazon.awssdk.services.neptune.model.*;
2024
import software.amazon.awssdk.services.neptune.model.CreateDbClusterRequest;
21-
import software.amazon.awssdk.services.neptune.model.CreateDbClusterResponse;
2225
import software.amazon.awssdk.services.neptune.model.CreateDbInstanceRequest;
23-
import software.amazon.awssdk.services.neptune.model.CreateDbInstanceResponse;
2426
import software.amazon.awssdk.services.neptune.model.CreateDbSubnetGroupRequest;
25-
import software.amazon.awssdk.services.neptune.model.CreateDbSubnetGroupResponse;
2627
import software.amazon.awssdk.services.neptune.model.DBCluster;
2728
import software.amazon.awssdk.services.neptune.model.DBInstance;
2829
import software.amazon.awssdk.services.neptune.model.DeleteDbClusterRequest;
2930
import software.amazon.awssdk.services.neptune.model.DeleteDbInstanceRequest;
3031
import software.amazon.awssdk.services.neptune.model.DeleteDbSubnetGroupRequest;
31-
import software.amazon.awssdk.services.neptune.model.DescribeDbClustersResponse;
3232
import software.amazon.awssdk.services.neptune.model.DescribeDbInstancesRequest;
33-
import software.amazon.awssdk.services.neptune.model.DescribeDbInstancesResponse;
3433
import software.amazon.awssdk.services.neptune.model.DescribeDbClustersRequest;
3534
import software.amazon.awssdk.services.neptunegraph.model.ServiceQuotaExceededException;
3635

3736
import java.time.Duration;
37+
import java.util.ArrayList;
3838
import java.util.List;
3939
import java.util.concurrent.CompletableFuture;
4040
import java.util.concurrent.CompletionException;
@@ -44,13 +44,11 @@
4444
// snippet-start:[neptune.java2.actions.main]
4545
public class NeptuneActions {
4646
private CompletableFuture<Void> instanceCheckFuture;
47-
private long instanceCheckStartTime;
4847
private static NeptuneAsyncClient neptuneAsyncClient;
4948
private final Region region = Region.US_EAST_1;
5049
private static final Logger logger = LoggerFactory.getLogger(NeptuneActions.class);
5150
private final NeptuneClient neptuneClient = NeptuneClient.builder().region(region).build();
5251

53-
5452
/**
5553
* Retrieves an instance of the NeptuneAsyncClient.
5654
* <p>
@@ -487,7 +485,10 @@ public CompletableFuture<String> createDBClusterAsync(String dbName) {
487485
* @return a CompletableFuture that, when completed, returns the Amazon Resource Name (ARN) of the created subnet group
488486
* @throws CompletionException if the operation fails, with a cause that may be a ServiceQuotaExceededException if the request would exceed the maximum quota
489487
*/
490-
public CompletableFuture<String> createSubnetGroupAsync(String vpcId, String groupName) {
488+
public CompletableFuture<String> createSubnetGroupAsync(String vpcId2, String groupName) {
489+
490+
// Get the Amazon Virtual Private Cloud (VPC) where the Neptune cluster and resources will be created
491+
String vpcId = getDefaultVpcId();
491492
List<String> subnetList = getSubnetIds(vpcId);
492493
CreateDbSubnetGroupRequest request = CreateDbSubnetGroupRequest.builder()
493494
.dbSubnetGroupName(groupName)
@@ -526,5 +527,33 @@ private List<String> getSubnetIds(String vpcId) {
526527
.collect(Collectors.toList());
527528
}
528529
}
530+
531+
public static String getDefaultVpcId() {
532+
Ec2Client ec2 = Ec2Client.builder()
533+
.region(Region.US_EAST_1)
534+
.build();
535+
536+
Filter myFilter = Filter.builder()
537+
.name("isDefault")
538+
.values("true")
539+
.build();
540+
541+
List<Filter> filterList = new ArrayList<>();
542+
filterList.add(myFilter);
543+
544+
DescribeVpcsRequest request = DescribeVpcsRequest.builder()
545+
.filters(filterList)
546+
.build();
547+
548+
549+
DescribeVpcsResponse response = ec2.describeVpcs(request);
550+
if (!response.vpcs().isEmpty()) {
551+
Vpc defaultVpc = response.vpcs().get(0);
552+
return defaultVpc.vpcId();
553+
} else {
554+
throw new RuntimeException("No default VPC found in this region.");
555+
}
556+
}
529557
}
558+
530559
// snippet-end:[neptune.java2.actions.main]

javav2/example_code/neptune/src/main/java/com/example/neptune/scenerio/NeptuneScenario.java

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
8+
89
import java.util.Scanner;
910

1011
// snippet-start:[neptune.java2.scenario.main]
@@ -15,42 +16,52 @@ public class NeptuneScenario {
1516
static NeptuneActions neptuneActions = new NeptuneActions();
1617

1718
public static void main(String[] args) {
18-
String subnetGroupName = "neptuneSubnetGroup56" ;
19-
String vpcId = "vpc-e97a4393" ;
20-
String clusterName = "neptuneCluster56" ;
21-
String dbInstanceId = "neptuneDB56" ;
19+
final String usage =
20+
"""
21+
Usage:
22+
<subnetGroupName> <clusterName> <dbInstanceId>
23+
24+
Where:
25+
subnetGroupName - The name of an existing Neptune DB subnet group that includes subnets in at least two Availability Zones.
26+
vpcId - The ID of the Amazon Virtual Private Cloud (VPC) where the Neptune cluster and resources will be created.
27+
clusterName - The unique identifier for the Neptune DB cluster.
28+
dbInstanceId - The identifier for a specific Neptune DB instance within the cluster.
29+
""";
30+
String subnetGroupName = "neptuneSubnetGroup56";
31+
String clusterName = "neptuneCluster56";
32+
String dbInstanceId = "neptuneDB56";
2233

2334
logger.info("""
24-
Amazon Neptune is a fully managed graph
25-
database service by AWS, designed specifically
26-
for handling complex relationships and connected
27-
datasets at scale. It supports two popular graph models:
28-
property graphs (via openCypher and Gremlin) and RDF
29-
graphs (via SPARQL). This makes Neptune ideal for
30-
use cases such as knowledge graphs, fraud detection,
31-
social networking, recommendation engines, and
32-
network management, where relationships between
33-
entities are central to the data.
34-
35-
Being fully managed, Neptune handles database
36-
provisioning, patching, backups, and replication,
37-
while also offering high availability and durability
38-
within AWS's infrastructure.
39-
40-
For developers, programming with Neptune allows
41-
for building intelligent, relationship-aware
42-
applications that go beyond traditional tabular
43-
databases. Developers can use the AWS SDK for Java
44-
V2 to automate infrastructure operations
45-
(via NeptuneClient).
46-
47-
Let's get started...
48-
""");
35+
Amazon Neptune is a fully managed graph
36+
database service by AWS, designed specifically
37+
for handling complex relationships and connected
38+
datasets at scale. It supports two popular graph models:
39+
property graphs (via openCypher and Gremlin) and RDF
40+
graphs (via SPARQL). This makes Neptune ideal for
41+
use cases such as knowledge graphs, fraud detection,
42+
social networking, recommendation engines, and
43+
network management, where relationships between
44+
entities are central to the data.
45+
46+
Being fully managed, Neptune handles database
47+
provisioning, patching, backups, and replication,
48+
while also offering high availability and durability
49+
within AWS's infrastructure.
50+
51+
For developers, programming with Neptune allows
52+
for building intelligent, relationship-aware
53+
applications that go beyond traditional tabular
54+
databases. Developers can use the AWS SDK for Java
55+
V2 to automate infrastructure operations
56+
(via NeptuneClient).
57+
58+
Let's get started...
59+
""");
4960
waitForInputToContinue(scanner);
50-
runScenario(subnetGroupName, vpcId, dbInstanceId, clusterName);
61+
runScenario(subnetGroupName, vpcId, dbInstanceId, clusterName);
5162
}
5263

53-
public static void runScenario(String subnetGroupName, String vpcId, String dbInstanceId, String clusterName) {
64+
public static void runScenario(String subnetGroupName, String vpcId, String dbInstanceId, String clusterName) {
5465
logger.info(DASHES);
5566
logger.info("1. Create a Neptune DB Subnet Group");
5667
logger.info("The Neptune DB subnet group is used when launching a Neptune cluster");
@@ -78,9 +89,9 @@ public static void runScenario(String subnetGroupName, String vpcId, String dbI
7889
logger.info(DASHES);
7990
logger.info("4. Check the status of the Neptune DB Instance");
8091
logger.info("""
81-
In this step, we will wait until the DB instance
82-
becomes available. This may take around 10 minutes.
83-
""");
92+
In this step, we will wait until the DB instance
93+
becomes available. This may take around 10 minutes.
94+
""");
8495
waitForInputToContinue(scanner);
8596
neptuneActions.checkInstanceStatus(dbInstanceId, "available").join();
8697
waitForInputToContinue(scanner);
@@ -96,25 +107,25 @@ public static void runScenario(String subnetGroupName, String vpcId, String dbI
96107
logger.info(DASHES);
97108
logger.info("6. Stop the Amazon Neptune cluster");
98109
logger.info("""
99-
Once stopped, this step polls the status
100-
until the cluster is in a stopped state.
101-
""");
110+
Once stopped, this step polls the status
111+
until the cluster is in a stopped state.
112+
""");
102113
waitForInputToContinue(scanner);
103114
neptuneActions.stopDBCluster(dbClusterId);
104-
neptuneActions.waitForClusterStatus(dbClusterId,"stopped");
115+
neptuneActions.waitForClusterStatus(dbClusterId, "stopped");
105116
waitForInputToContinue(scanner);
106117
logger.info(DASHES);
107118

108119
logger.info(DASHES);
109120
logger.info("7. Start the Amazon Neptune cluster");
110121
logger.info("""
111-
Once started, this step polls the clusters
112-
status until it's in an available state.
113-
We will also poll the instance status.
114-
""");
122+
Once started, this step polls the clusters
123+
status until it's in an available state.
124+
We will also poll the instance status.
125+
""");
115126
waitForInputToContinue(scanner);
116127
neptuneActions.startDBCluster(dbClusterId);
117-
neptuneActions.waitForClusterStatus(dbClusterId,"available");
128+
neptuneActions.waitForClusterStatus(dbClusterId, "available");
118129
neptuneActions.checkInstanceStatus(dbInstanceId, "available").join();
119130
logger.info(DASHES);
120131
logger.info(DASHES);
@@ -133,12 +144,12 @@ public static void runScenario(String subnetGroupName, String vpcId, String dbI
133144

134145
logger.info(DASHES);
135146
logger.info(
136-
"""
137-
Thank you for checking out the Amazon Neptune Service Use demo. We hope you
138-
learned something new, or got some inspiration for your own apps today.
139-
For more AWS code examples, have a look at:
140-
https://docs.aws.amazon.com/code-library/latest/ug/what-is-code-library.html
141-
""");
147+
"""
148+
Thank you for checking out the Amazon Neptune Service Use demo. We hope you
149+
learned something new, or got some inspiration for your own apps today.
150+
For more AWS code examples, have a look at:
151+
https://docs.aws.amazon.com/code-library/latest/ug/what-is-code-library.html
152+
""");
142153
logger.info(DASHES);
143154
}
144155

0 commit comments

Comments
 (0)