跳转至

Nebula PythonGraph

Graph是一款Python语言的客户端,可以连接、管理NebulaGraph图数据库。

前提条件Graph

已安装Python,版本为3.5及以上。

版本对照表Graph

NebulaGraph版本 Nebula Python版本
2.5.0 2.5.0
2.0.1 2.0.0
2.0.0 2.0.0
2.0.0-rc1 2.0.0rc1

安装Nebula PythonGraph

pip安装Graph

$ pip install nebula2-python==<version>

克隆源码安装Graph

  1. 克隆Nebula Python源码到机器。

    • (推荐)如果需要安装指定版本的Nebula Python,请使用选项--branch指定分支。例如安装v2.5.0发布版本,请执行如下命令:

      $ git clone --branch v2.5.0 https://github.com/vesoft-inc/nebula-python.git
      
    • 如果需要安装日常开发版本,请执行如下命令下载master分支的源码:

      $ git clone https://github.com/vesoft-inc/nebula-python.git
      
  2. 进入目录nebula-python。

    $ cd nebula-python
    
  3. 执行如下命令安装依赖。

    $ pip install -r requirements.txt
    

    Note

    如果想在开发模式下进行单元测试,请安装requirements-dev.txt的依赖。

  4. 执行如下命令安装。

    $ sudo python3 setup.py install
    

核心代码Graph

详细示例请参见Graph。

连接Graph服务Graph

# 定义配置
config = Config()
config.max_connection_pool_size = 10
# 初始化连接池
connection_pool = ConnectionPool()
# 如果给定的服务器正常,则返回true,否则返回false。
ok = connection_pool.init([('192.168.xx.1', 9669)], config)

# 方法1:控制连接自行释放。
# 从连接池中获取会话
session = connection_pool.get_session('root', 'nebula')

# 选择图空间
session.execute('USE basketballplayer')

# 执行查看TAG命令
result = session.execute('SHOW TAGS')
print(result)

# 释放会话
session.release()

# 方法2:使用session_context,会话将被自动释放。
with connection_pool.session_context('root', 'nebula') as session:
    session.execute('USE basketballplayer;')
    result = session.execute('SHOW TAGS;')
    print(result)

# 关闭连接池
connection_pool.close()

连接Storage服务Graph

# 设置所有Meta服务地址
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)

最后更新: August 30, 2021
Back to top