Skip to content

Nebula Python

Nebula Python is a Python client for connecting to and managing the Nebula Graph database.

Prerequisites

You have installed Python 3.5 or later versions.

Compatibility with Nebula Graph

Nebula Graph version Nebula Python version
2.5.1 2.5.0
2.0.1 2.0.0
2.0.0 2.0.0
2.0.0-rc1 2.0.0rc1

Install Nebula Python

Install Nebula Python with pip

$ pip install nebula2-python==<version>

Install Nebula Python from the source code

  1. Clone the Nebula Python source code to the host.

    • (Recommended) To install a specific version of Nebula Python, use the Git option --branch to specify the branch. For example, to install v2.5.0, run the following command:

      $ git clone --branch v2.5.0 https://github.com/vesoft-inc/nebula-python.git
      
    • To install the daily development version, run the following command to download the source code from the master branch:

      $ git clone https://github.com/vesoft-inc/nebula-python.git
      
  2. Change the working directory to nebula-python.

    $ cd nebula-python
    
  3. Run the following command to install dependencies.

    $ pip install -r requirements.txt
    

    Note

    To run unit tests in the development mode, install dependencies of requirements-dev.txt.

  4. Run the following command to install Nebula Python.

    $ sudo python3 setup.py install
    

Core of the example code

This section shows the core of the example code. For all the code, see Example.

Connect to the Graph Service

# Customize configurations.
config = Config()
config.max_connection_pool_size = 10
# Initialize the connection pool.
connection_pool = ConnectionPool()
# Returns true if the server is healthy, false otherwise.
ok = connection_pool.init([('192.168.xx.1', 9669)], config)

# Method 1: Manually specify when to release the session.
# Get the session from the connection pool.
session = connection_pool.get_session('root', 'nebula')

# Select a graph space.
session.execute('USE basketballplayer')

# Run the SHOW TAGS statement.
result = session.execute('SHOW TAGS')
print(result)

# Release the session.
session.release()

# Method 2: Use session_context to automatically release the session.
with connection_pool.session_context('root', 'nebula') as session:
    session.execute('USE basketballplayer;')
    result = session.execute('SHOW TAGS;')
    print(result)

# Close the connection pool.
connection_pool.close()

Connect to the Storage Server

# Set the IP addresses of all Meta servers.
meta_cache = MetaCache([('192.168.xx.1', 9559),
                        ('192.168.xx.2', 9559),
                        ('192.168.xx.3', 9559)],
                       50000)
graph_storage_client = GraphStorageClient(meta_cache)

resp = graph_storage_client.scan_vertex(
        space_name='ScanSpace',
        tag_name='person')
while resp.has_next():
    result = resp.next()
    for vertex_data in result:
        print(vertex_data)

resp = graph_storage_client.scan_edge(
    space_name='ScanSpace',
    edge_name='friend')
while resp.has_next():
    result = resp.next()
    for edge_data in result:
        print(edge_data)

Last update: September 3, 2021
Back to top