• Stars
    star
    181
  • Rank 212,110 (Top 5 %)
  • Language
    Python
  • Created about 5 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Client API of Nebula Graph in Python

nebula-python

This repository holds the official Python API for NebulaGraph.

Before you start

Before you start, please read this section to choose the right branch for you. The compatibility between the API and NebulaGraph service can be found in How to choose nebula-python. The current master branch is compatible with NebulaGraph 3.x.

The directory structure

|--nebula-python
    |
    |-- nebula3                               // client code
    |   |-- fbthrift                          // the fbthrift lib code
    |   |-- common           
    |   |-- data           
    |   |-- graph           
    |   |-- meta           
    |   |-- net                               // the net code for graph client
    |   |-- storage           
    |   |-- Config.py                         // the pool config
    |   |__ Exception.py                      // the define exception
    |           
    |-- examples
    |   |-- GraphClientMultiThreadExample.py  // the multi thread example
    |   |-- GraphClientSimpleExample.py       // the simple example
    |   |__ ScanVertexEdgeExample.py                   
    |
    |-- tests                                 // the test code
    |                      
    |-- setup.py                              // used to install or package
    |                      
    |__ README.md                             // the introduction of nebula3-python

How to get nebula3-python

Option one: install with pip

# for v3.x
pip install nebula3-python==$version
# for v2.x
pip install nebula2-python==$version

Option two: install from the source code

  • Clone from GitHub
git clone https://github.com/vesoft-inc/nebula-python.git
cd nebula-python
  • Install
pip install .

Quick example to use graph-client to connect graphd

from nebula3.gclient.net import ConnectionPool
from nebula3.Config import Config

# define a config
config = Config()
config.max_connection_pool_size = 10
# init connection pool
connection_pool = ConnectionPool()
# if the given servers are ok, return true, else return false
ok = connection_pool.init([('127.0.0.1', 9669)], config)

# option 1 control the connection release yourself
# get session from the pool
session = connection_pool.get_session('root', 'nebula')

# select space
session.execute('USE nba')

# show tags
result = session.execute('SHOW TAGS')
print(result)

# release session
session.release()

# option 2 with session_context, session will be released automatically
with connection_pool.session_context('root', 'nebula') as session:
    session.execute('USE nba')
    result = session.execute('SHOW TAGS')
    print(result)

# close the pool
connection_pool.close()

Example of using session pool

There are some limitations while using the session pool:

1. There MUST be an existing space in the DB before initializing the session pool.
2. Each session pool is corresponding to a single USER and a single Space. This is to ensure that the user's access control is consistent. i.g. The same user may have different access privileges in different spaces. If you need to run queries in different spaces, you may have multiple session pools.
3. Every time when sessinPool.execute() is called, the session will execute the query in the space set in the session pool config.
4. Commands that alter passwords or drop users should NOT be executed via session pool.

see /example/SessinPoolExample.py

Quick example to fetch result to dataframe

from nebula3.gclient.net import ConnectionPool
from nebula3.Config import Config
import pandas as pd
from typing import Dict
from nebula3.data.ResultSet import ResultSet

def result_to_df(result: ResultSet) -> pd.DataFrame:
    """
    build list for each column, and transform to dataframe
    """
    assert result.is_succeeded()
    columns = result.keys()
    d: Dict[str, list] = {}
    for col_num in range(result.col_size()):
        col_name = columns[col_num]
        col_list = result.column_values(col_name)
        d[col_name] = [x.cast() for x in col_list]
    return pd.DataFrame.from_dict(d, orient='columns')

# define a config
config = Config()

# init connection pool
connection_pool = ConnectionPool()

# if the given servers are ok, return true, else return false
ok = connection_pool.init([('127.0.0.1', 9669)], config)

# option 2 with session_context, session will be released automatically
with connection_pool.session_context('root', 'nebula') as session:
    session.execute('USE <your graph space>')
    result = session.execute('<your query>')
    df = result_to_df(result)
    print(df)

# close the pool
connection_pool.close()

Quick example to use storage-client to scan vertex and edge

You should make sure the scan client can connect to the address of storage which see from SHOW HOSTS

from nebula3.mclient import MetaCache, HostAddr
from nebula3.sclient.GraphStorageClient import GraphStorageClient

# the metad servers's address
meta_cache = MetaCache([('172.28.1.1', 9559),
                        ('172.28.1.2', 9559),
                        ('172.28.1.3', 9559)],
                       50000)

# option 1 metad usually discover the storage address automatically
graph_storage_client = GraphStorageClient(meta_cache)

# option 2 manually specify the storage address
storage_addrs = [HostAddr(host='172.28.1.4', port=9779),
                 HostAddr(host='172.28.1.5', port=9779),
                 HostAddr(host='172.28.1.6', port=9779)]
graph_storage_client = GraphStorageClient(meta_cache, storage_addrs)

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)

How to choose nebula-python

Nebula-Python Version NebulaGraph Version
1.0 1.x
2.0.0 2.0.0/2.0.1
2.5.0 2.5.0
2.6.0 2.6.0/2.6.1
3.0.0 3.0.0
3.1.0 3.1.0
3.3.0 3.3.0
3.4.0 3.4.0
master master

How to contribute to nebula-python

Fork this repo, then clone it locally (be sure to replace the {username} in the repo URL below with your GitHub username):

git clone https://github.com/{username}/nebula-python.git
cd nebula-python

Install the package in the editable mode, then install all the dev dependencies:

pip install -e .
pip install -r requirements/dev.txt

Make sure the Nebula server in running, then run the tests with pytest:

pytest

Using the default formatter with black.

Please run make fmt to format python code before submitting.

See How to contribute for the general process of contributing to Nebula projects.

More Repositories

1

nebula

A distributed, fast open-source graph database featuring horizontal scalability and high availability
C++
10,313
star
2

nebula-graph

A distributed, fast open-source graph database featuring horizontal scalability and high availability. This is an archived repo for v2.5 only, from 2.6.0 +, NebulaGraph switched back to https://github.com/vesoft-inc/nebula
C++
844
star
3

github-statistics

A react static app for displaying github repo statistics like Star History, Fork History and more.
JavaScript
311
star
4

nebula-studio

NebulaGraph Web GUI Tools
TypeScript
188
star
5

nebula-java

Client API and data importer of Nebula Graph in Java
Java
163
star
6

nebula-go

Nebula client in Golang
Go
127
star
7

nebula-docker-compose

Docker compose for Nebula Graph
Smarty
101
star
8

nebula-importer

Nebula Graph Importer with Go
Go
88
star
9

nebulagraph-veditor

A Highly customizable JavaScript Diagramming Lib with SVG, HTML, Canvas support.
TypeScript
80
star
10

nebula-operator

Operation utilities for Nebula Graph
Go
77
star
11

nebula-algorithm

Nebula-Algorithm is a Spark Application based on GraphX, which enables state of art Graph Algorithms to run on top of NebulaGraph and write back results to NebulaGraph.
Scala
67
star
12

nebula-storage

A distributed consistent graph storage. This is an archived repo for v2.5 only, from 2.6.0 +, NebulaGraph switched back to https://github.com/vesoft-inc/nebula
C++
59
star
13

nebula-console

Command line interface for the Nebula Graph service
Go
54
star
14

nebula-community

A place where community members and learn from and collaborate with each other.
53
star
15

nebula-docs-cn

Repository for the Chinese documentations
HTML
52
star
16

nebula-docs

Documentations for the Nebula Graph project
HTML
51
star
17

nebula-flink-connector

Flink Connector for Nebula Graph
Java
47
star
18

nebula-spark-connector

Scala
39
star
19

nebula-rust

Nebula Graph Client API in Rust
Rust
34
star
20

nebula-common

Common code for all Nebula projects. This is an archived repo for v2.5 only, from 2.6.0 +, NebulaGraph switched back to https://github.com/vesoft-inc/nebula
C++
31
star
21

nebula-dashboard

Nebula Graph Service Monitor Tool
TypeScript
28
star
22

nebula-exchange

NebulaGraph Exchange is an Apache Spark application to parse data from different sources to NebulaGraph in a distributed environment. It supports both batch and streaming data in various formats and sources including other Graph Databases, RDBMS, Data warehouses, NoSQL, Message Bus, File systems, etc.
Scala
28
star
23

nebula-http-gateway

Gateway to provide HTTP endpoints for the Nebula Graph service.
Go
25
star
24

nebula-spark-utils

Spark related libraries and tools
Scala
23
star
25

nebula-third-party

Project to build third-party dependencies of Nebula Graph
CMake
18
star
26

nebula-cpp

Nebula Graph Client API in C++
C++
17
star
27

nebula-dev-docker

Nebula docker image for development
Shell
15
star
28

nebula-stats-exporter

Data exporter of Nebula Graph
Go
15
star
29

nebula-clients

API of Nebula Graph in different programming languages.
Java
15
star
30

nebula-br

Backup and restore utility for Nebula Graph
Go
14
star
31

k6-plugin

Go
9
star
32

nebula-chaos

Chaos framework for the Storage Service
C++
9
star
33

nebula-ansible

Jinja
6
star
34

nebula-agent

Go
6
star
35

nebula-gears

Gears for Nebula Graph
Shell
3
star
36

auto-sync-repos

sync repos automatically
Python
2
star
37

.github

Smarty
1
star
38

go-pkg

Go
1
star
39

auto-merge-pr

JavaScript
1
star