Skip to content

Commit 88377b0

Browse files
committed
added lambda
1 parent 170121c commit 88377b0

File tree

9 files changed

+1095
-0
lines changed

9 files changed

+1095
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.example.neptune.analytics;
2+
3+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
4+
import software.amazon.awssdk.regions.Region;
5+
import software.amazon.awssdk.services.neptunedata.NeptunedataClient;
6+
import software.amazon.awssdk.services.neptunegraph.NeptuneGraphClient;
7+
import software.amazon.awssdk.services.neptunegraph.model.CreateGraphRequest;
8+
import software.amazon.awssdk.services.neptunegraph.model.CreateGraphResponse;
9+
import software.amazon.awssdk.services.neptunegraph.model.GraphStatus;
10+
import software.amazon.awssdk.services.neptunegraph.model.NeptuneGraphException;
11+
12+
public class CreateNeptuneGraphExample {
13+
14+
public static void main(String[] args) {
15+
// Set the desired region
16+
Region region = Region.US_EAST_1;
17+
18+
// Set the name for your new graph
19+
String graphName = "sample-analytics-graph";
20+
21+
// Create the NeptuneGraph client
22+
NeptuneGraphClient client = NeptuneGraphClient.builder()
23+
.region(region)
24+
.credentialsProvider(DefaultCredentialsProvider.create())
25+
.build();
26+
27+
executeCreateGraph(client, graphName);
28+
29+
}
30+
31+
/**
32+
* Executes the process of creating a new Neptune graph.
33+
*
34+
* @param client the Neptune graph client used to interact with the Neptune service
35+
* @param graphName the name of the graph to be created
36+
* @throws NeptuneGraphException if an error occurs while creating the graph
37+
*/
38+
public static void executeCreateGraph(NeptuneGraphClient client, String graphName) {
39+
try {
40+
// Create the graph request
41+
CreateGraphRequest request = CreateGraphRequest.builder()
42+
.graphName(graphName)
43+
.provisionedMemory(16)
44+
.build();
45+
46+
// Create the graph
47+
CreateGraphResponse response = client.createGraph(request);
48+
49+
// Extract the graph name and ARN
50+
String createdGraphName = response.name();
51+
String graphArn = response.arn();
52+
String graphEndpoint = response.endpoint();
53+
54+
System.out.println("Graph created successfully!");
55+
System.out.println("Graph Name: " + createdGraphName);
56+
System.out.println("Graph ARN: " + graphArn);
57+
System.out.println("Graph Endpoint: " +graphEndpoint );
58+
59+
} catch (NeptuneGraphException e) {
60+
System.err.println("Failed to create graph: " + e.awsErrorDetails().errorMessage());
61+
} finally {
62+
client.close();
63+
}
64+
}
65+
}
66+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.neptune.analytics;
5+
6+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
7+
import software.amazon.awssdk.core.ResponseInputStream;
8+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
9+
import software.amazon.awssdk.http.apache.ApacheHttpClient;
10+
import software.amazon.awssdk.regions.Region;
11+
import software.amazon.awssdk.services.neptunedata.NeptunedataClient;
12+
import software.amazon.awssdk.services.neptunegraph.NeptuneGraphClient;
13+
import software.amazon.awssdk.services.neptunegraph.model.ExecuteQueryRequest;
14+
import software.amazon.awssdk.services.neptunegraph.model.ExecuteQueryResponse;
15+
import software.amazon.awssdk.services.neptunegraph.model.NeptuneGraphException;
16+
import java.io.BufferedReader;
17+
import java.io.InputStreamReader;
18+
import java.net.URI;
19+
import java.nio.charset.StandardCharsets;
20+
import java.time.Duration;
21+
import java.util.stream.Collectors;
22+
23+
/**
24+
* This Java example demonstrates how to query Amazon Neptune Analytics (Neptune Graph) using the AWS SDK for Java V2.
25+
*
26+
* VPC NETWORKING REQUIREMENT:
27+
* ----------------------------------------------------------------------
28+
* Amazon Neptune Analytics must be accessed from within an Amazon VPC. This means:
29+
*
30+
* 1. Your application must run within a VPC environment such as EC2, Lambda, ECS, Cloud9, or an AWS managed notebook.
31+
* 2. You **cannot run this code from your local machine** unless you are connected via a VPN or Direct Connect.
32+
* 3. Ensure that your Neptune Graph cluster endpoint is accessible and security groups allow inbound access from your client.
33+
* 4. Always use the HTTPS endpoint when setting the `endpointOverride()` value.
34+
*
35+
* You can test access by running:
36+
* curl https://<graph-endpoint>:8182/status
37+
* ----------------------------------------------------------------------
38+
*/
39+
40+
public class NeptuneAnalyticsQueryExample {
41+
42+
public static void main(String[] args) {
43+
44+
// Replace with your Neptune Analytics graph endpoint (including port 8182)
45+
// You can get the Endpoint value by running CreateNeptuneGraphExample
46+
String neptuneAnalyticsEndpoint = "https://<your-neptune-analytics-endpoint>:8182";
47+
String graphId = "<your-graph-id>";
48+
49+
NeptuneGraphClient client = NeptuneGraphClient.builder()
50+
.region(Region.US_EAST_1)
51+
.endpointOverride(URI.create(neptuneAnalyticsEndpoint))
52+
.httpClientBuilder(ApacheHttpClient.builder()
53+
.connectionTimeout(Duration.ofSeconds(10))
54+
.socketTimeout(Duration.ofSeconds(0)) // No socket timeout (read_timeout=None)
55+
)
56+
.overrideConfiguration(ClientOverrideConfiguration.builder()
57+
.apiCallAttemptTimeout(Duration.ofSeconds(0)) // No total timeout
58+
.retryPolicy(b -> b.numRetries(0)) // Disable retries (total_max_attempts=1)
59+
.build())
60+
.build();
61+
62+
executeGremlinProfileQuery(client, graphId);
63+
}
64+
65+
/**
66+
* Executes a Gremlin profile query on the Neptune Analytics graph.
67+
*
68+
* @param client the {@link NeptuneGraphClient} instance to use for the query
69+
* @param graphId the identifier of the graph to execute the query on
70+
*
71+
* @throws NeptuneGraphException if an error occurs while executing the query on the Neptune Graph
72+
* @throws Exception if an unexpected error occurs
73+
*/
74+
public static void executeGremlinProfileQuery(NeptuneGraphClient client, String graphId) {
75+
76+
try {
77+
System.out.println("Running openCypher query on Neptune Analytics...");
78+
79+
ExecuteQueryRequest request = ExecuteQueryRequest.builder()
80+
.graphIdentifier(graphId)
81+
.queryString("MATCH (n {code: 'ANC'}) RETURN n")
82+
.language("OPEN_CYPHER")
83+
.build();
84+
85+
ResponseInputStream<ExecuteQueryResponse> response = client.executeQuery(request);
86+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response, StandardCharsets.UTF_8))) {
87+
String result = reader.lines().collect(Collectors.joining("\n"));
88+
System.out.println("Query Result:");
89+
System.out.println(result);
90+
} catch (Exception e) {
91+
System.err.println("Error reading response: " + e.getMessage());
92+
}
93+
94+
} catch (NeptuneGraphException e) {
95+
System.err.println("NeptuneGraph error: " + e.awsErrorDetails().errorMessage());
96+
} catch (Exception e) {
97+
System.err.println("Unexpected error: " + e.getMessage());
98+
} finally {
99+
client.close();
100+
}
101+
}
102+
}
103+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.neptune.data;
5+
6+
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
7+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
8+
import software.amazon.awssdk.http.apache.ApacheHttpClient;
9+
import software.amazon.awssdk.regions.Region;
10+
import software.amazon.awssdk.services.neptunedata.NeptunedataClient;
11+
12+
13+
import software.amazon.awssdk.services.neptunedata.model.ExecuteGremlinProfileQueryRequest;
14+
import software.amazon.awssdk.services.neptunedata.model.ExecuteGremlinProfileQueryResponse;
15+
import software.amazon.awssdk.services.neptunedata.model.NeptunedataException;
16+
17+
import java.net.URI;
18+
import java.time.Duration;
19+
20+
/**
21+
* Example: Running a Gremlin Profile query using the AWS SDK for Java V2.
22+
*
23+
* ----------------------------------------------------------------------------------
24+
* VPC Networking Requirement:
25+
* ----------------------------------------------------------------------------------
26+
* Amazon Neptune must be accessed from **within the same VPC** as the Neptune cluster.
27+
* It does not expose a public endpoint, so this code must be executed from:
28+
* - An EC2 instance, Lambda function, ECS task, Cloud9 IDE, or
29+
* - A connected environment (VPN, Direct Connect, or peered VPC).
30+
*
31+
* To see an example, see Creating an AWS Lambda function that queries Neptune graph data within the VPC
32+
* in the AWS Code Library.
33+
*
34+
* Notes:
35+
* - Ensure Neptune's security group allows inbound traffic on port 8182.
36+
* - Replace the endpoint below with your Neptune cluster's HTTPS endpoint.
37+
* - To verify access, run: curl https://<your-neptune-endpoint>:8182/status
38+
* ----------------------------------------------------------------------------------
39+
*/
40+
public class GremlinProfileQueryExample {
41+
42+
// Specify the endpoint. You can obtain an endpoint by running
43+
// the main scenario.
44+
private static final String NEPTUNE_ENDPOINT = "https://<your-neptune-endpoint>:8182";
45+
46+
public static void main(String[] args) {
47+
NeptunedataClient client = NeptunedataClient.builder()
48+
.credentialsProvider(DefaultCredentialsProvider.create())
49+
.region(Region.US_EAST_1)
50+
.endpointOverride(URI.create(NEPTUNE_ENDPOINT))
51+
.httpClientBuilder(ApacheHttpClient.builder()
52+
.connectionTimeout(Duration.ofSeconds(10))
53+
.socketTimeout(Duration.ofSeconds(30)))
54+
.overrideConfiguration(ClientOverrideConfiguration.builder()
55+
.apiCallAttemptTimeout(Duration.ofSeconds(30))
56+
.build())
57+
.build();
58+
59+
try {
60+
executeGremlinProfileQuery(client);
61+
} catch (NeptunedataException e) {
62+
System.err.println("Neptune error: " + e.awsErrorDetails().errorMessage());
63+
} catch (Exception e) {
64+
System.err.println("Unexpected error: " + e.getMessage());
65+
} finally {
66+
client.close();
67+
}
68+
}
69+
70+
/**
71+
* Executes a Gremlin PROFILE query using the provided NeptunedataClient.
72+
*
73+
* @param client The NeptunedataClient instance to be used for executing the Gremlin PROFILE query.
74+
*/
75+
private static void executeGremlinProfileQuery(NeptunedataClient client) {
76+
System.out.println("Executing Gremlin PROFILE query...");
77+
78+
ExecuteGremlinProfileQueryRequest request = ExecuteGremlinProfileQueryRequest.builder()
79+
.gremlinQuery("g.V().has('code', 'ANC')")
80+
.build();
81+
82+
ExecuteGremlinProfileQueryResponse response = client.executeGremlinProfileQuery(request);
83+
if (response.output() != null) {
84+
System.out.println("Query Profile Output:");
85+
System.out.println(response.output());
86+
} else {
87+
System.out.println("No output returned from the profile query.");
88+
}
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.neptune.data;
5+
6+
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
7+
import software.amazon.awssdk.http.apache.ApacheHttpClient;
8+
import software.amazon.awssdk.regions.Region;
9+
import software.amazon.awssdk.services.neptunedata.NeptunedataClient;
10+
import software.amazon.awssdk.services.neptunedata.model.ExecuteGremlinExplainQueryRequest;
11+
import software.amazon.awssdk.services.neptunedata.model.ExecuteGremlinExplainQueryResponse;
12+
import software.amazon.awssdk.services.neptunedata.model.ExecuteGremlinProfileQueryRequest;
13+
import software.amazon.awssdk.services.neptunedata.model.ExecuteGremlinProfileQueryResponse;
14+
import software.amazon.awssdk.services.neptunedata.model.NeptunedataException;
15+
import java.net.URI;
16+
import java.time.Duration;
17+
18+
/**
19+
* This example demonstrates how to run a Gremlin Explain and Profile query on an Amazon Neptune database
20+
* using the AWS SDK for Java V2.
21+
*
22+
* VPC NETWORKING REQUIREMENT:
23+
* ----------------------------------------------------------------------
24+
* Amazon Neptune is designed to be **accessed from within an Amazon VPC**.
25+
* It does not expose a public endpoint. This means:
26+
*
27+
* 1. Your Java application must run **within the same VPC** (e.g., via an EC2 instance, Lambda function, ECS task,
28+
* or AWS Cloud9 environment), or from a peered VPC that has network access to Neptune.
29+
*
30+
* 2. You cannot run this example directly from your local machine (e.g., via IntelliJ or PyCharm on your laptop)
31+
* unless you set up a VPN or AWS Direct Connect that bridges your local environment to your VPC.
32+
*
33+
* 3. You must ensure the **VPC Security Group** attached to your Neptune cluster allows **inbound access on port 8182**
34+
* from the instance or environment where this Java code runs.
35+
*
36+
* 4. The `endpointOverride()` must use the **HTTPS Neptune endpoint** including the `:8182` port.
37+
*
38+
* To see an example, see Creating an AWS Lambda function that queries Neptune graph data within the VPC
39+
* in the AWS Code Library.
40+
*
41+
* TIP:
42+
* You can test connectivity using `curl` or `telnet` from your instance to:
43+
* curl https://<neptune-endpoint>:8182/status
44+
* If this fails, it’s likely a networking or security group issue.
45+
*
46+
* ----------------------------------------------------------------------
47+
*/
48+
public class NeptuneGremlinExplainAndProfileExample {
49+
50+
// Specify the endpoint. You can obtain an endpoint by running
51+
// the main scenario.
52+
private static final String NEPTUNE_ENDPOINT = "https://[Specify-Your-Endpoint]:8182";
53+
54+
public static void main(String[] args) {
55+
NeptunedataClient client = NeptunedataClient.builder()
56+
.region(Region.US_EAST_1)
57+
.endpointOverride(URI.create(NEPTUNE_ENDPOINT))
58+
.httpClientBuilder(ApacheHttpClient.builder()
59+
.connectionTimeout(Duration.ofSeconds(10))
60+
.socketTimeout(Duration.ofSeconds(30)))
61+
.overrideConfiguration(ClientOverrideConfiguration.builder()
62+
.apiCallAttemptTimeout(Duration.ofSeconds(30))
63+
.build())
64+
.build();
65+
66+
executeGremlinExplainQuery(NeptunedataClient client)
67+
}
68+
69+
/**
70+
* Executes a Gremlin explain query and a Gremlin profile query using the provided Neptune data client.
71+
*
72+
* @param client the Neptune data client to use for executing the Gremlin queries
73+
* @throws NeptunedataException if an error occurs while executing the Gremlin queries on the Neptune data client
74+
* @throws Exception if an unexpected error occurs during the execution
75+
*/
76+
public static void executeGremlinExplainQuery(NeptunedataClient client) {
77+
try {
78+
runExplainQuery(client);
79+
runProfileQuery(client);
80+
} catch (NeptunedataException e) {
81+
System.err.println("Neptune error: " + e.awsErrorDetails().errorMessage());
82+
} catch (Exception e) {
83+
System.err.println("Unexpected error: " + e.getMessage());
84+
} finally {
85+
client.close();
86+
}
87+
}
88+
89+
/**
90+
* Runs an EXPLAIN query on the Neptune graph database using the provided NeptunedataClient.
91+
*
92+
* @param client The NeptunedataClient instance to use for executing the EXPLAIN query.
93+
*/
94+
private static void runExplainQuery(NeptunedataClient client) {
95+
System.out.println("Running Gremlin EXPLAIN query...");
96+
ExecuteGremlinExplainQueryRequest explainRequest = ExecuteGremlinExplainQueryRequest.builder()
97+
.gremlinQuery("g.V().has('code', 'ANC')")
98+
.build();
99+
100+
ExecuteGremlinExplainQueryResponse explainResponse = client.executeGremlinExplainQuery(explainRequest);
101+
102+
System.out.println("Explain Query Result:");
103+
if (explainResponse.output() != null) {
104+
System.out.println(explainResponse.output());
105+
} else {
106+
System.out.println("No explain output returned.");
107+
}
108+
}
109+
110+
/**
111+
* Runs a Gremlin PROFILE query using the provided NeptunedataClient instance.
112+
*
113+
* @param client the NeptunedataClient instance to use for executing the Gremlin query
114+
*/
115+
private static void runProfileQuery(NeptunedataClient client) {
116+
System.out.println("Running Gremlin PROFILE query...");
117+
118+
ExecuteGremlinProfileQueryRequest profileRequest = ExecuteGremlinProfileQueryRequest.builder()
119+
.gremlinQuery("g.V().has('code', 'ANC')")
120+
.build();
121+
122+
ExecuteGremlinProfileQueryResponse profileResponse = client.executeGremlinProfileQuery(profileRequest);
123+
124+
System.out.println("Profile Query Result:");
125+
if (profileResponse.output() != null) {
126+
System.out.println(profileResponse.output());
127+
} else {
128+
System.out.println("No profile output returned.");
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)