Nebula Python是一款Python语言的客户端,可以连接、管理Nebula Graph图数据库。
$ pip install nebula2-python==2.5.0
$ git clone --branch v2.5.0 https://github.com/vesoft-inc/nebula-python.git
$ cd nebula-python
$ pip install -r requirements.txt
$ sudo python3 setup.py install
from nebula2.gclient.net import ConnectionPool from nebula2.Config import Config # 定义配置 config = Config() config.max_connection_pool_size = 10 # 初始化连接池 connection_pool = ConnectionPool() # 如果给定的服务器正常,则返回true,否则返回false。 ok = connection_pool.init([('192.168.16.96', 9669)], config) # 方法1:控制连接自行释放。 # 从连接池中获取会话 session = connection_pool.get_session('root', '123456') # 选择图空间 session.execute('USE Graph500) # 执行查看TAG命令 result = session.execute('SHOW TAGS') print(result) # 释放会话 session.release() # 方法2:使用session_context,会话将被自动释放。 with connection_pool.session_context('root', '123456’) as session: session.execute('USE Graph500;') result = session.execute('SHOW TAGS;') print(result) # 关闭连接池 connection_pool.close()
from nebula2.mclient import MetaCache, HostAddr from nebula2.sclient.GraphStorageClient import GraphStorageClient # metad服务器地址 meta_cache = MetaCache([('192.168.14.13', 9559), ('192.168.14.14', 9559), ('192.168.16.96', 9559)], 50000) # 方法一 metad 通常会自动发现存储地址 graph_storage_client = GraphStorageClient(meta_cache) # 方法二 手动指定存储地址 storage_addrs = [HostAddr(host='192.168.14.13', port=9779), HostAddr(host='192.168.14.13', port=9779), HostAddr(host='192.168.16.96', 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)