Nebula Python简介
Nebula Python是一款Python语言的客户端,可以连接、管理Nebula Graph图数据库。
Nebula Python安装
方法一:pip安装
$ pip install nebula2-python==2.5.0
方法二:源码安装
Step 1. 克隆Nebula Python源码
$ git clone --branch v2.5.0 https://github.com/vesoft-inc/nebula-python.git
Step 2. 进入目录nebula-python
$ cd nebula-python
Step 3. 安装依赖包
$ pip install -r requirements.txt
Step 4. 执行如下命令安装
$ sudo python3 setup.py install
连接Graph服务
from nebula2.gclient.net import ConnectionPool
from nebula2.Config import Config
config = Config()
config.max_connection_pool_size = 10
connection_pool = ConnectionPool()
ok = connection_pool.init([('192.168.16.96', 9669)], config)
session = connection_pool.get_session('root', '123456')
session.execute('USE Graph500)
result = session.execute('SHOW TAGS')
print(result)
session.release()
with connection_pool.session_context('root', '123456’) as session:
session.execute('USE Graph500;')
result = session.execute('SHOW TAGS;')
print(result)
connection_pool.close()
连接Storage服务
from nebula2.mclient import MetaCache, HostAddr
from nebula2.sclient.GraphStorageClient import GraphStorageClient
meta_cache = MetaCache([('192.168.14.13', 9559),
('192.168.14.14', 9559),
('192.168.16.96', 9559)],
50000)
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)
|