Using Python to connect to a Neptune DB instance - Amazon Neptune

Using Python to connect to a Neptune DB instance

If you can, always use the latest version of the Apache TinkerPop Python Gremlin client, gremlinpython, that your engine version supports. Newer versions contain numerous bug fixes that improve the stability, performance and usability of the client. The gremlinpython version to use will typically align with the TinkerPop versions described in the table for the Java Gremlin client.

Note

The gremlinpython 3.5.x versions are compatible with TinkerPop 3.4.x versions as long as you only use 3.4.x features in the Gremlin queries you write.

The following section walks you through the running of a Python 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:

  • Download and install Python 3.6 or later from the Python.org website.

  • Verify that you have pip installed. If you don't have pip or you're not sure, see Do I need to install pip? in the pip documentation.

  • If your Python installation does not already have it, download futures as follows: pip install futures

To connect to Neptune using Python
  1. Enter the following to install the gremlinpython package:

    pip install --user gremlinpython
  2. Create a file named gremlinexample.py, and then open it in a text editor.

  3. Copy the following into the gremlinexample.py 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 Connecting to Amazon Neptune Endpoints section.

    from __future__ import print_function # Python 2/3 compatibility from gremlin_python import statics from gremlin_python.structure.graph import Graph from gremlin_python.process.graph_traversal import __ from gremlin_python.process.strategies import * from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection graph = Graph() remoteConn = DriverRemoteConnection('wss://your-neptune-endpoint:8182/gremlin','g') g = graph.traversal().withRemote(remoteConn) print(g.V().limit(2).toList()) remoteConn.close()
  4. Enter the following command to run the sample:

    python gremlinexample.py

    The Gremlin query at the end of this example returns the vertices (g.V().limit(2)) in a list. This list is then printed with the standard Python print function.

    Note

    The final part of the Gremlin query, toList(), 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()

    The preceding example returns the first two vertices in the graph by using the g.V().limit(2).toList() traversal. To query for something else, replace it with another Gremlin traversal with one of the appropriate ending methods.