当前位置:网站首页>实例讲解将Graph Explorer搬上JupyterLab
实例讲解将Graph Explorer搬上JupyterLab
2022-07-01 23:56:00 【华为云开发者联盟】
摘要:基于Graph Explorer在Jupyter上进行图探索,可以大大降低编码成本,丰富JupyterLab的数据表现力。
本文分享自华为云社区《将Graph Explorer搬上JupyterLab:使用GES4Jupyter连接GES并进行图探索》,作者: 蜉蝣与海 。
GES4Jupyter是一款可以在JupyterLab中连接访问GES并可视化的工具。工具中封装了部分GES业务面接口,并提供对返回数据的可视化能力。基于该工具在Jupyter上进行图探索,可以大大降低编码成本,丰富JupyterLab的数据表现力。
一、使用前准备
1. 华为云账号
在使用华为云服务之前您需要注册华为云帐号。通过此帐号,只需为使用的服务付费,即可使用所有华为云服务。
注册华为云账号步骤请点击:《华为云注册介绍》
注册成功后即可自动登录华为云,您需要完成“实名认证”才可以正常使用服务。
2. OBS对象存储服务
OBS即对象存储服务(Object Storage Service),GES将OBS作为数据源导入数据。
数据若想导入图引擎服务GES,需要先上传至OBS。详情参考:华为云图引擎服务 GES 实战——创图
3. GES图引擎服务
使用GES4Jupyter前,需要在图引擎服务控制台创建一个GES图实例,并且导入数据。本例中使用的数据源是新冠患者轨迹追溯数据集v2,可以从AI Gallery中下载。详情参考:华为图引擎文档-快速入门和华为云图引擎服务 GES 实战——创图
4. 获取调用GES业务面API必备的参数
调用GES API需要输入token鉴权信息,认证鉴权能力依赖华为云统一身份认证服务IAM。获取Token需要用户名密码、图所在区域等信息。详情查看:华为图引擎文档-业务面API认证鉴权和调用 GES 服务业务面 API 相关参数的获取
二、使用GES4Jupyter连接GES服务
从华为云首页进入ModelArts控制台,点击CodeLab新建一个Jupyter Notebook,并等待资源初始化完成。
新建一个Notebook,使用下列代码获取GES4Jupyter程序和资源文件。
import moxing as moxmox.file.copy('obs://obs-aigallery-zc/GES/ges4jupyter/beta/ges4jupyter.py', 'ges4jupyter.py')mox.file.copy('obs://obs-aigallery-zc/GES/ges4jupyter/beta/ges4jupyter.html', 'ges4jupyter.html')
在Notebook中输入代码后,将该段文本配置为代码,然后点击左侧的运行按钮,并等待运行完成。
点击左上角的“+”号新建代码片段,输入下列代码段并运行,完成GES4Jupyter的初始化。
from ges4jupyter import GESConfig, GES4Jupyter, read_csv_configeip = ''project_id = ''graph_name = ''iam_url = ''user_name = ''password = ''domain_name = ''project_name = ''port = 80eip, project_id, graph_name, iam_url, user_name, password, domain_name, project_name, port = read_csv_config('cn_north_4_graph.csv')config = GESConfig(eip, project_id, graph_name, iam_url = iam_url, user_name = user_name, password = password, domain_name = domain_name, project_name = project_name, port = port)ges_util = GES4Jupyter(config, True);
上面代码中涉及的字段含义基本分为两大类,简单介绍下:
- 用于构造API的参数:eip,projectId,graph_name, port这四个参数都参与构造了业务面请求的url。eip与graph_name两个参数在创图时容易获得,port参数默认为80,开启安全模式时为443,关于projectId,可参考图引擎官网文档-获取项目id,从图控制台根据图所属的区域来获取项目id。
- 用于在请求API前获取token的参数:iam_url, user_name, password, domain_name,project_name,GES4Jupyter会使用这四个参数获取token,进而在使用api时进行鉴权,相关参数获取请参考:华为图引擎文档-业务面API认证鉴权和调用 GES 服务业务面 API 相关参数的获取
除了手动输入这部分参数,也可以将参数构造为一个csv文件上传至CodeLab平台,而后执行图示read_csv_config方法,一键获取所有参数。
如图为一个区域为北京四的图的示例文件,关键信息(如ip、projectId、图名、账户名、密码、iam子账号名)通过马赛克隐藏。
对于新创的图,使用下列代码段可以创建点边索引,方便使用cypher:
print('开始创建点索引:')job_id = ges_util.build_vertex_index()job_result = ges_util.get_job(job_id)if 'errorCode' not in job_result: for i in range(100): if job_result['status'] == 'success': break else: time.sleep(1) job_result = ges_util.get_job(job_id)print('点索引创建完成')print('开始创建边索引:')job_id = ges_util.build_edge_index()job_result = ges_util.get_job(job_id)if 'errorCode' not in job_result: for i in range(100): if job_result['status'] == 'success': break else: time.sleep(1) job_result = ges_util.get_job(job_id)print('边索引创建完成')
如果图比较大,且没有基于label过滤方面的诉求,也可以关闭cypher的索引开关。
ges_util.cypher_query("call dbms.parameter('needNodeIndex', false)");ges_util.cypher_query("call dbms.parameter('needEdgeIndex', false)");
执行summary方法可以看到点边分布:
三、使用GES4Jupyter调用业务面接口并进行可视化
GES4Jupyter支持调用cypher语句,并可视化cypher的结果。在初始化完成GES4Jupyter后,使用下列代码可以执行并可视化cypher查询:
cypher_result = ges_util.cypher_query("match (n)-[r]->(m) return n,r,m limit 10",formats=['row','graph']);ges_util.format_cypher_result(cypher_result)
在Notebook中输入该段代码,点击运行,可以看到Notebook中效果:
同时,工具还提供了其他选项卡,不仅可以看到可视化Graph结构,还可以看到表格数据、以及原始的json数据。
GES4Jupyter也提供了能力对gremlin语言返回的点边数据进行可视化。
gremlin_result = ges_util.gremlin_query("g.V().outE().bothV().path().limit(2)");ges_util.format_gremlin_result(gremlin_result)

同时,对于ges的path-query接口,当返回数据为tree格式时,GES4Jupyter也能提供较好的支持。
result = ges_util.path_query({ "repeat": [ { "operator": "bothV", "vertex_filter": { "property_filter": { "leftvalue": { "id": "" }, "predicate": "NOTIN", "rightvalue": { "value": ["北京"] } } } } ], "until": [ { "vertex_filter": { "property_filter": { "leftvalue": { "id": "" }, "predicate": "=", "rightvalue": { "value": ["额济纳旗"] } } } } ], "times": 5, "queryType": "Tree", "vertices": ["北京病例2"] })ges_util.format_path_query(result)

四、图引擎官网有更多案例可以使用GES4Jupyter上手体验
本文的数据集取自华为云图引擎官网“新冠患者轨迹追溯”数据集,notebook代码取自“新冠患者轨迹追溯”案例,在图引擎官方网站上,还有其他动手实践案例,配套ModelArts的CodeLab,可以实现“开箱即用”,提供丰富的场景和大家一起认识图、了解图、使用图。
参考项目:https://github.com/merqurio/neo4jupyter
边栏推荐
- Windows 7 install MySQL error: 1067
- SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
- Jielizhi Bluetooth headset quality control and production skills [chapter]
- PostgreSQL notes (10) dynamically execute syntax parsing process
- Is it safe to choose mobile phone for stock trading account opening in Beijing?
- Iota in golang
- Difficult to get up syndrome (bit by bit greed)
- 电商RPA机器人,助力品牌电商抢立流量高点
- I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
- 门级建模—课后习题
猜你喜欢
门级建模—课后习题
S32Kxxx bootloader之UDS bootloader
PyTorch学习记录
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
如何提升数据质量
【.Net Core】程序相关各种全局文件
Write some suggestions to current and future doctoral students to sort out and share
The essence of software architecture
下载在线视频 m3u8使用教程
Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object
随机推荐
【QT】對於Qt MSVC 2017無法編譯的問題解决
Selectively inhibiting learning bias for active sampling
.env.xxx 文件,加了常量,卻undefined
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
在代码中使用SqlCommand对象
Operate database transactions with jpatractionmanager
Notblank and notempty
【QT】测试Qt是否能连接上数据库
excel如何打开100万行以上的csv文件
LDR6035智能蓝牙音响可对手机设备持续充放电方案
golang中的iota
Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
TS初次使用、ts类型
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Is it safe to choose mobile phone for stock trading account opening in Beijing?
Use the htaccess file to prohibit the script execution permission in the directory
Digital transformation has a long way to go, so how to take the key first step
13 MySQL constraint
ADO. Net SqlDataAdapter object
How to solve the image pop-up problem when pycharm calls Matplotlib to draw