Skip to content

Commit f502fff

Browse files
committed
add nepune Java examples
1 parent 0d513cb commit f502fff

13 files changed

+1338
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
aws neptune delete-db-subnet-group --db-subnet-group-name my-neptune-subnet-group
2+
3+
aws neptune delete-db-cluster --db-cluster-identifier my-neptune-cluster --skip-final-snapshot
4+
5+
aws neptune describe-db-instances --query "DBInstances[?Engine=='neptune']"
6+
7+
aws neptune delete-db-instance --db-instance-identifier my-neptune-db --skip-final-snapshot
8+
9+
10+
aws iam create-role --role-name NeptuneLoadFromS3Role --assume-role-policy-document file://trust-policy.json
11+
12+

javav2/example_code/neptune/pom.xml

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>neptune</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<java.version>21</java.version>
14+
<maven.compiler.target>21</maven.compiler.target>
15+
<maven.compiler.source>21</maven.compiler.source>
16+
</properties>
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-surefire-plugin</artifactId>
22+
<version>3.5.2</version>
23+
</plugin>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-compiler-plugin</artifactId>
27+
<version>3.11.0</version>
28+
<configuration>
29+
<release>${java.version}</release>
30+
<source>21</source>
31+
<target>21</target>
32+
<compilerArgs>--enable-preview</compilerArgs>
33+
</configuration>
34+
</plugin>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-resources-plugin</artifactId>
38+
<version>3.3.1</version>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
<dependencyManagement>
43+
<dependencies>
44+
<dependency>
45+
<groupId>software.amazon.awssdk</groupId>
46+
<artifactId>bom</artifactId>
47+
<version>2.31.8</version>
48+
<type>pom</type>
49+
<scope>import</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.apache.logging.log4j</groupId>
53+
<artifactId>log4j-bom</artifactId>
54+
<version>2.23.1</version>
55+
<type>pom</type>
56+
<scope>import</scope>
57+
</dependency>
58+
</dependencies>
59+
</dependencyManagement>
60+
<dependencies>
61+
<dependency>
62+
<groupId>org.junit.jupiter</groupId>
63+
<artifactId>junit-jupiter</artifactId>
64+
<version>5.11.4</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>software.amazon.awssdk</groupId>
69+
<artifactId>neptune</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>software.amazon.awssdk</groupId>
73+
<artifactId>ec2</artifactId>
74+
</dependency>
75+
<dependency>
76+
<groupId>software.amazon.awssdk</groupId>
77+
<artifactId>neptunedata</artifactId>
78+
</dependency>
79+
<dependency>
80+
<groupId>software.amazon.awssdk</groupId>
81+
<artifactId>neptunegraph</artifactId>
82+
</dependency>
83+
<dependency>
84+
<groupId>software.amazon.awssdk</groupId>
85+
<artifactId>apache-client</artifactId>
86+
<version>2.25.38</version> <!-- Or the latest available version -->
87+
</dependency>
88+
89+
<dependency>
90+
<groupId>software.amazon.awssdk</groupId>
91+
<artifactId>secretsmanager</artifactId>
92+
</dependency>
93+
<dependency>
94+
<groupId>com.google.code.gson</groupId>
95+
<artifactId>gson</artifactId>
96+
<version>2.10.1</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>software.amazon.awssdk</groupId>
100+
<artifactId>sns</artifactId>
101+
</dependency>
102+
<dependency>
103+
<groupId>software.amazon.awssdk</groupId>
104+
<artifactId>sqs</artifactId>
105+
</dependency>
106+
<dependency>
107+
<groupId>software.amazon.awssdk</groupId>
108+
<artifactId>ssooidc</artifactId>
109+
</dependency>
110+
<dependency>
111+
<groupId>software.amazon.awssdk</groupId>
112+
<artifactId>sso</artifactId>
113+
</dependency>
114+
<dependency>
115+
<groupId>software.amazon.awssdk</groupId>
116+
<artifactId>iam-policy-builder</artifactId>
117+
</dependency>
118+
<dependency>
119+
<groupId>software.amazon.awssdk</groupId>
120+
<artifactId>sts</artifactId>
121+
</dependency>
122+
<dependency>
123+
<groupId>org.apache.logging.log4j</groupId>
124+
<artifactId>log4j-core</artifactId>
125+
</dependency>
126+
<dependency>
127+
<groupId>org.slf4j</groupId>
128+
<artifactId>slf4j-api</artifactId>
129+
<version>2.0.13</version>
130+
</dependency>
131+
<dependency>
132+
<groupId>org.apache.logging.log4j</groupId>
133+
<artifactId>log4j-slf4j2-impl</artifactId>
134+
</dependency>
135+
<dependency>
136+
<groupId>org.apache.logging.log4j</groupId>
137+
<artifactId>log4j-1.2-api</artifactId>
138+
</dependency>
139+
</dependencies>
140+
</project>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.example.neptune;
2+
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;
13+
14+
import java.net.URI;
15+
import java.time.Duration;
16+
17+
/**
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.
34+
*
35+
* 4. The `endpointOverride()` must use the **HTTPS Neptune endpoint** including the `:8182` port.
36+
*
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+
* ----------------------------------------------------------------------
43+
*/
44+
public class HelloNeptune {
45+
46+
private static final String NEPTUNE_ENDPOINT = "https://[Specify-Your-Endpoint]:8182";
47+
48+
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+
}
72+
}
73+
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')")
79+
.build();
80+
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...");
93+
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.");
105+
}
106+
}
107+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.neptunegraph.NeptuneGraphClient;
6+
import software.amazon.awssdk.services.neptunegraph.model.CreateGraphRequest;
7+
import software.amazon.awssdk.services.neptunegraph.model.CreateGraphResponse;
8+
import software.amazon.awssdk.services.neptunegraph.model.GraphStatus;
9+
import software.amazon.awssdk.services.neptunegraph.model.NeptuneGraphException;
10+
11+
public class CreateNeptuneGraphExample {
12+
13+
public static void main(String[] args) {
14+
// Set the desired region
15+
Region region = Region.US_EAST_1;
16+
17+
// Set the name for your new graph
18+
String graphName = "sample-analytics-graph";
19+
20+
// Create the NeptuneGraph client
21+
NeptuneGraphClient client = NeptuneGraphClient.builder()
22+
.region(region)
23+
.credentialsProvider(DefaultCredentialsProvider.create())
24+
.build();
25+
26+
try {
27+
// Create the graph request
28+
CreateGraphRequest request = CreateGraphRequest.builder()
29+
.graphName(graphName)
30+
.provisionedMemory(16)
31+
.build();
32+
33+
// Create the graph
34+
CreateGraphResponse response = client.createGraph(request);
35+
36+
// Extract the graph name and ARN
37+
String createdGraphName = response.name();
38+
String graphArn = response.arn();
39+
String graphEndpoint = response.endpoint();
40+
41+
System.out.println("Graph created successfully!");
42+
System.out.println("Graph Name: " + createdGraphName);
43+
System.out.println("Graph ARN: " + graphArn);
44+
System.out.println("Graph Endpoint: " +graphEndpoint );
45+
46+
} catch (NeptuneGraphException e) {
47+
System.err.println("Failed to create graph: " + e.awsErrorDetails().errorMessage());
48+
} finally {
49+
client.close();
50+
}
51+
}
52+
}
53+

0 commit comments

Comments
 (0)