1
- package com .example .neptune ;
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
2
3
3
- import software .amazon .awssdk .auth .credentials .DefaultCredentialsProvider ;
4
- import software .amazon .awssdk .core .client .config .ClientOverrideConfiguration ;
5
- import software .amazon .awssdk .http .apache .ApacheHttpClient ;
6
- import software .amazon .awssdk .regions .Region ;
7
- import software .amazon .awssdk .services .neptunedata .NeptunedataClient ;
8
- import software .amazon .awssdk .services .neptunedata .model .ExecuteGremlinExplainQueryRequest ;
9
- import software .amazon .awssdk .services .neptunedata .model .ExecuteGremlinExplainQueryResponse ;
10
- import software .amazon .awssdk .services .neptunedata .model .ExecuteGremlinProfileQueryRequest ;
11
- import software .amazon .awssdk .services .neptunedata .model .ExecuteGremlinProfileQueryResponse ;
12
- import software .amazon .awssdk .services .neptunedata .model .NeptunedataException ;
4
+ package com .example .neptune ;
13
5
14
- import java .net .URI ;
15
- import java .time .Duration ;
6
+ import software .amazon .awssdk .core .async .SdkPublisher ;
7
+ import software .amazon .awssdk .services .neptune .NeptuneAsyncClient ;
8
+ import software .amazon .awssdk .services .neptune .model .DescribeDbClustersRequest ;
9
+ import software .amazon .awssdk .services .neptune .model .DescribeDbClustersResponse ;
10
+ import java .util .concurrent .CompletableFuture ;
11
+ import java .util .concurrent .ExecutionException ;
16
12
13
+ // snippet-start:[neptune.java2.hello.main]
17
14
/**
18
- * This example demonstrates how to run a Gremlin Explain and Profile query on an Amazon Neptune database
19
- * using the AWS SDK for Java V2.
20
- *
21
- * VPC NETWORKING REQUIREMENT:
22
- * ----------------------------------------------------------------------
23
- * Amazon Neptune is designed to be **accessed from within an Amazon VPC**.
24
- * It does not expose a public endpoint. This means:
25
- *
26
- * 1. Your Java application must run **within the same VPC** (e.g., via an EC2 instance, Lambda function, ECS task,
27
- * or AWS Cloud9 environment), or from a peered VPC that has network access to Neptune.
28
- *
29
- * 2. You cannot run this example directly from your local machine (e.g., via IntelliJ or PyCharm on your laptop)
30
- * unless you set up a VPN or AWS Direct Connect that bridges your local environment to your VPC.
31
- *
32
- * 3. You must ensure the **VPC Security Group** attached to your Neptune cluster allows **inbound access on port 8182**
33
- * from the instance or environment where this Java code runs.
15
+ * Before running this Java V2 code example, set up your development
16
+ * environment, including your credentials.
34
17
*
35
- * 4. The `endpointOverride()` must use the **HTTPS Neptune endpoint** including the `:8182` port.
18
+ * For more information, see the following documentation topic:
36
19
*
37
- * TIP:
38
- * You can test connectivity using `curl` or `telnet` from your instance to:
39
- * curl https://<neptune-endpoint>:8182/status
40
- * If this fails, it’s likely a networking or security group issue.
41
- *
42
- * ----------------------------------------------------------------------
20
+ * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
43
21
*/
44
22
public class HelloNeptune {
45
23
46
24
private static final String NEPTUNE_ENDPOINT = "https://[Specify-Your-Endpoint]:8182" ;
47
25
48
26
public static void main (String [] args ) {
49
-
50
- NeptunedataClient client = NeptunedataClient .builder ()
51
- .credentialsProvider (DefaultCredentialsProvider .create ())
52
- .region (Region .US_EAST_1 )
53
- .endpointOverride (URI .create (NEPTUNE_ENDPOINT ))
54
- .httpClientBuilder (ApacheHttpClient .builder ()
55
- .connectionTimeout (Duration .ofSeconds (10 ))
56
- .socketTimeout (Duration .ofSeconds (30 )))
57
- .overrideConfiguration (ClientOverrideConfiguration .builder ()
58
- .apiCallAttemptTimeout (Duration .ofSeconds (30 ))
59
- .build ())
60
- .build ();
61
-
62
- try {
63
- runExplainQuery (client );
64
- runProfileQuery (client );
65
- } catch (NeptunedataException e ) {
66
- System .err .println ("Neptune error: " + e .awsErrorDetails ().errorMessage ());
67
- } catch (Exception e ) {
68
- System .err .println ("Unexpected error: " + e .getMessage ());
69
- } finally {
70
- client .close ();
71
- }
27
+ NeptuneAsyncClient neptuneClient = NeptuneAsyncClient .create ();
28
+ describeDbCluster (neptuneClient );
72
29
}
73
30
74
- private static void runExplainQuery (NeptunedataClient client ) {
75
- System .out .println ("Running Gremlin EXPLAIN query..." );
76
-
77
- ExecuteGremlinExplainQueryRequest explainRequest = ExecuteGremlinExplainQueryRequest .builder ()
78
- .gremlinQuery ("g.V().has('code', 'ANC')" )
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 ) {
37
+ DescribeDbClustersRequest request = DescribeDbClustersRequest .builder ()
38
+ .maxRecords (20 ) // Optional: limit per page
79
39
.build ();
80
40
81
- ExecuteGremlinExplainQueryResponse explainResponse = client .executeGremlinExplainQuery (explainRequest );
82
-
83
- System .out .println ("Explain Query Result:" );
84
- if (explainResponse .output () != null ) {
85
- System .out .println (explainResponse .output ());
86
- } else {
87
- System .out .println ("No explain output returned." );
88
- }
89
- }
90
-
91
- private static void runProfileQuery (NeptunedataClient client ) {
92
- System .out .println ("Running Gremlin PROFILE query..." );
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
+ }
48
+ });
93
49
94
- ExecuteGremlinProfileQueryRequest profileRequest = ExecuteGremlinProfileQueryRequest .builder ()
95
- .gremlinQuery ("g.V().has('code', 'ANC')" )
96
- .build ();
97
-
98
- ExecuteGremlinProfileQueryResponse profileResponse = client .executeGremlinProfileQuery (profileRequest );
99
-
100
- System .out .println ("Profile Query Result:" );
101
- if (profileResponse .output () != null ) {
102
- System .out .println (profileResponse .output ());
103
- } else {
104
- System .out .println ("No profile output returned." );
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 {
56
+ neptuneClient .close ();
105
57
}
106
58
}
107
59
}
60
+ // snippet-end:[neptune.java2.hello.main]
0 commit comments