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