Once you’ve created a graph and set up the prerequisites for connecting to that graph, you can proceed with accessing your graph to load and query data. This section contains an explanation of ways you can communicate with your graph along with some example queries. For details on how to load data, see the loading data section in the Neptune Analytics user guide.
Using a notebook
You can access to your Neptune Analytics graph through a Neptune workbench, which provides visualization tools on top of Neptune Analytics which can help with interpreting query results. For more information on how to set up and use a graph notebook, see the notebooks section in the Neptune Analytics user guide.
Example
The cell magic below submits an openCypher query that returns a single node.
%%oc MATCH (n) RETURN n LIMIT 1
Using the AWS SDK
With the AWS SDK, you can access your graph using a programming language of your choice, which provides
clean integration between Neptune Analytics and your applications. With the Neptune Analytics SDK (service name Neptune graph),
you can perform data operations like querying and summarization in addition to control plane operations
such as graph creation, deletion, and modification. For a list of the supported programming languages and
directions for setting up the SDK in each language, see the
AWS developer tools
Direct links to the API reference documentation for the Neptune Analytics service in each SDK language can be found below:
Examples
The following examples outline how to interact with an Amazon Neptune graph database using different programming languages and tools. It covers the steps to set up an SDK client, execute an OpenCypher query, and print the results, for Python as well as other languages. Additionally, it demonstrates how to use the AWS Command Line Interface (CLI) and the AWSCURL tool to submit queries directly to the Neptune graph endpoint.
The code sample below uses the Python SDK to submit a query that returns a single node and prints the result.
-
Follow the installation instructions
to install Boto3. If you are using a SageMaker AI hosted Jupyter notebook, Boto3 will be pre-installed, but you may need to update it. -
Configure your Boto3 credentials by following the configuring credentials
guide. -
Create a file named
queryExample.py
. -
In that file, paste the following code. It will set up a Neptune graph client, execute an openCypher query request, and print the result. Replace the graph identifier and query string as needed.
import boto3 // Set up the Neptune Graph client. client = boto3.client('neptune-graph') // Execute a query. response = client.execute_query( graphIdentifier='g-0123456789', queryString='MATCH (n) RETURN n LIMIT 1', language='OPEN_CYPHER' ) // Print the response. print(response['payload'].read().decode('utf-8'))
-
Run the sample code by entering
python queryExample.py
.
Using the AWS CLI
You can connect to your graph using the AWS command-line interface. Specify the neptune-graph service name
to use Neptune Analytics APIs. For information on AWS CLI installation and usage, see the
AWS CLI documentation
The example command below uses the AWS CLI to submit a query that returns a single node.
aws neptune-graph execute-query \
--graph-identifier g-0123456789 \
--region us-east-2 \
--query-string "MATCH (n) RETURN n LIMIT 1" \
--language open_cypher \
out.txt
Using AWSCURL
You can connect to your graph using the awscurl
command-line tool. This allows you to directly
make requests using HTTPS against a graph endpoint. You can find the correct endpoint to use in the AWS
console (under the “Connectivity & Security” section of a Neptune Analytics graph page) and in the response of any
GetGraph
API request. To set up credentials, refer to
the AWS CLI user guide.
For awscurl
installation and setup instructions, see
AWSCURL
The following command uses awscurl
to submit a query that returns a single node.
awscurl -X POST "https://g-0123456789.us-east-2.neptune-graph.amazonaws.com/queries" \
-H "Content-Type: application/x-www-form-urlencoded" \
--region us-east-2 \
--service neptune-graph \
-d "query=MATCH (n) RETURN n LIMIT 1"