Amazon Neptune
User Guide (API Version 2017-11-29)

Using Node.js to Connect to a Neptune DB Instance

The following section walks you through the running of a Node.js sample that connects to an Amazon Neptune DB instance and performs a Gremlin traversal.

You must follow these instructions from an Amazon EC2 instance in the same virtual private cloud (VPC) as your Neptune DB instance.

Before you begin, do the following:

  • Verify that Node.js version 8.11 or higher is installed. If it is not, download and install Node.js from the Nodejs.org website.

To connect to Neptune using Node.js

  1. Enter the following to install the gremlin-javascript package:

    npm install gremlin
  2. Create a file named gremlinexample.js and open it in a text editor.

  3. Copy the following into the gremlinexample.js file. Replace your-neptune-endpoint with the address of your Neptune DB instance.

    For information about finding the address of your Neptune DB instance, see the Amazon Neptune Endpoints section.

    const gremlin = require('gremlin'); const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection; const Graph = gremlin.structure.Graph; dc = new DriverRemoteConnection('wss://your-neptune-endpoint:8182/gremlin'); const graph = new Graph(); const g = graph.traversal().withRemote(dc); g.V().limit(1).count().next(). then(data => { console.log(data); dc.close(); }).catch(error => { console.log('ERROR', error); dc.close(); });
  4. Enter the following command to run the sample:

    node gremlinexample.js

The preceding example returns the count of a single vertex in the graph by using the g.V().limit(1).count().next() traversal. To query for something else, replace it with another Gremlin traversal with one of the appropriate ending methods.

Note

The final part of the Gremlin query, next(), is required to submit the traversal to the server for evaluation. If you don't include that method or another equivalent method, the query is not submitted to the Neptune DB instance.

The following methods submit the query to the Neptune DB instance:

  • toList()

  • toSet()

  • next()

  • nextTraverser()

  • iterate()

Important

This is a standalone Node.js example. If you are using this code in an AWS Lambda function, ensure that you indicate success or failure by using the callback parameter or the context parameter. Failure to do so can result in timeouts.

For more information, see Using the Callback Parameter in the AWS Lambda documentation.

The following shows the previous example with the changes that are necessary for it to run as an AWS Lambda function. This example requires that you create a deployment package that includes the gremlin-javascript package. For more information about creating a Lambda deployment package, see Creating a Deployment Package (Node.js).

const gremlin = require('gremlin'); const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection; const Graph = gremlin.structure.Graph; exports.handler = (event, context, callback) => { dc = new DriverRemoteConnection('wss://your-neptune-endpoint:8182/gremlin'); const graph = new Graph(); const g = graph.traversal().withRemote(dc); g.V().limit(1).count().next(). then(data => { console.log(data); dc.close(); context.succeed(results); }).catch(error => { console.log('ERROR', error); dc.close(); context.fail('Traversal failed: ' + err); }); }

For more information about Amazon Neptune, see Next Steps.