知识图谱:【图数据库Nebula(八)】——Nebula Python
作者:互联网
文章目录
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()
# 如果给定的服务器正常,则返回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()
连接Storage服务
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)
标签:Python,图谱,Nebula,192.168,Step,session,result 来源: https://blog.csdn.net/u013010473/article/details/120936457