Python

Updated at:

This topic describes how to connect to and manage a Graph Database (GDB) instance in a Python environment.

Prerequisites

  • Ensure that your Graph Database (GDB) instance and your Elastic Compute Service (ECS) virtual machine are in the same Virtual Private Cloud (VPC).

  • Before you connect to a GDB instance using Python, prepare the following environment:

  • If you use Python 2.7, install the following module:

    pip install futures --user

Connect to a GDB instance using Python

  1. Run the following command to install the gremlinpython package:

    pip install gremlinpython --user
  2. Create a file named test.py. Add the following content to the file and replace the placeholder configuration information:

    • Replace ${your-gdb-endpoint} with the domain name of your Graph Database (GDB) instance.

    • Replace ${username} with the username of your GDB instance.

    • Replace ${password} with the password of your GDB instance.

    from __future__ import print_function  # Python 2/3 compatibility
    from gremlin_python.driver import client
    client = client.Client('ws://${your-gdb-endpoint}:8182/gremlin', 'g', username="${username}", password="${password}")
    callback = client.submit("g.V().limit(1)").all().result()
    for result in callback:
        print(result)
    client.close()
    Note

    The internal network port for Graph Database (GDB) is 8182. If you connect using a public IP address, change the port to 3734.

  3. Run the following command to execute the sample code.

    python test.py

    The following response is returned:

    [v[3a63cc90-d957-4324-9ffc-16a8e4c1c1f4]]

    The preceding response shows that the g.V().limit(1) command traverses and returns one vertex from the GDB instance. To query other data, replace the command with the appropriate Gremlin Domain-Specific Language (DSL) query.

  4. (Optional) Handle common exceptions.

    • Cannot run the event loop while another loop is running

      This fault may occur when you use gremlin_python in an environment such as Jupyter because of a nested event loop. To resolve this issue, add the following code.

      # Install the package
      !pip install nest_asyncio
      
      # Import and use the package in the code file
      import nest_asyncio
      nest_asyncio.apply()
    • The gremlin driver reports NotImplementError on Windows

      In Python 3.8 and later, the default event loop implementation for Windows in the asyncio library was changed. The gremlin driver uses tornado for network communication, which depends on asyncio. This can cause a NotImplementError exception. To resolve this issue, add the following code to change the event loop policy.

      import sys
      import asyncio
      
      if sys.platform == 'win32':
          asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

The following are other DSL query examples:

  1. Delete vertices and edges with a specified label.

    g.E().hasLabel('gdb_sample_knows').drop()
    g.E().hasLabel('gdb_sample_created').drop()
    g.V().hasLabel('gdb_sample_person').drop()
    g.V().hasLabel('gdb_sample_software').drop()
  2. Add vertices and set their IDs and properties.

    g.addV('gdb_sample_person').property(id, 'gdb_sample_marko').property('age', 28).property('name', 'marko')
    g.addV('gdb_sample_person').property(id, 'gdb_sample_vadas').property('age', 27).property('name', 'vadas')
    g.addV('gdb_sample_person').property(id, 'gdb_sample_josh').property('age', 32).property('name', 'josh')
    g.addV('gdb_sample_person').property(id, 'gdb_sample_peter').property('age', 35).property('name', 'peter')
    g.addV('gdb_sample_software').property(id, 'gdb_sample_lop').property('lang', 'java').property('name', 'lop')
    g.addV('gdb_sample_software').property(id, 'gdb_sample_ripple').property('lang', 'java').property('name', 'ripple')
  3. Modify or add the age property.

    g.V('gdb_sample_marko').property('age', 29)
  4. Create relationships and set the weight property.

    g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_vadas')).property('weight', 0.5f)
    g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_josh')).property('weight', 1.0f)
    g.addE('gdb_sample_created').from(V('gdb_sample_marko')).to(V('gdb_sample_lop')).property('weight', 0.4f)
    g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_lop')).property('weight', 0.4f)
    g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_ripple')).property('weight', 1.0f)
    g.addE('gdb_sample_created').from(V('gdb_sample_peter')).to(V('gdb_sample_lop')).property('weight', 0.2f)
  5. Query the number of all vertices or vertices with a specified label.

    g.V().count()
    g.V().hasLabel('gdb_sample_person').count()
  6. Query for vertices that represent people older than 29 and sort the results by name in descending order.

    g.V().hasLabel('gdb_sample_person').has('age', gt(29))
    g.V().hasLabel('gdb_sample_person').order().by('name', decr)
  7. Run an associated query to retrieve the people that Marko knows and the software they created.

    g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person')
    g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person').outE('gdb_sample_created').inV().hasLabel('gdb_sample_software')
  8. Delete relationships and vertices.

    g.V('gdb_sample_marko').outE('gdb_sample_knows').where(inV().has(id, 'gdb_sample_josh')).drop()
    g.V('gdb_sample_marko').drop()

Related resources

For more information about the Gremlin Python API, see Gremlin-Python in the Apache TinkerPop3 documentation.