这个例子展示了如何使用MATLAB®的Neo4j®接口来分析网络中服务之间的依赖关系。假设有图形数据存储在代表网络的Neo4j®数据库中。这个数据库有7个节点和7个关系。每个节点只有一个唯一的属性键的名字
与价值观Service1
通过Service7
.每个关系都有类型DEPENDS_ON
.
要找到每个服务所依赖的服务数量,使用MATLAB®接口来实现Neo4j®和有向图
对象。有关Neo4j®的MATLAB®接口的详细信息,请参阅面向Neo4j数据库接口的图形数据库工作流.
本地机器托管带有端口号的Neo4j®数据库7474
,用户名neo4j
和密码matlab
.有关数据库中数据的可视化表示,请参见下图。
创建一个Neo4j®连接对象neo4jconn
使用URLhttp://localhost:7474/db/data
,用户名neo4j
和密码matlab
.
网址=“http://localhost: 7474 / db /数据”;用户名=“neo4j”; 密码=matlab的;neo4jconn=neo4j(url、用户名、密码);
检查消息
属性neo4jconn
.空白的消息
属性表示成功的连接。
neo4jconn。消息
ans=[]
检索所有类型的关系DEPENDS_ON
以及与每个关系相关联的所有节点。
network_graphdata = searchGraph (neo4jconn, {“DEPENDS_ON”})
网络图形数据=带字段的结构:节点:[7×3 table]关系:[7×5 table]
使用的表网络_graphdata.Nodes
,访问的名字
属性中显示的每个节点NodeData
表的变量。
分配表格网络_graphdata.Nodes
来不稳定
.
nodestable = network_graphdata。节点
不稳定=7×3表NodeLabels NodeData NodeObject __________ ____________ ___________________________________ 6“服务”[1×1 struct] [1 x1 database.neo4j.http。Neo4jNode] 0 'Service' [1×1 struct] [1x1 database.neo4j.http. txt . txt . txt . txt . txt . txt . txt . txt . txt . txt . txt。Neo4jNode] 4 'Service' [1×1 struct] [1x1 database.neo4j.http。Neo4jNode] 2 'Service' [1×1 struct] [1x1 database.neo4j.http. txt . txt . txt . txt . txt . txt . txt . txt . txt。Neo4jNode] 3 'Service' [1×1 struct] [1x1 database.neo4j.http. txt . txt . txt . txt . txt . txt . txt。Neo4jNode] 5 'Service' [1×1 struct] [1x1 database.neo4j.http. txt . txt . txt . txt . txt . txt . txt . txt。Neo4jNode] 1 'Service' [1×1 struct] [1x1 database.neo4j.http.Neo4jNode]
指定每一行的行名rownames
.
rownames = nodestable.Properties.RowNames
rownames =7×1单元阵列{'6'} {'0'} {'4'} {'2'} {'3'} {'5'} {'1'}
访问NodeData
变量的不稳定
为每一行。nodedata
包含一个结构数组。
nodedata=[nodestable.nodedata{rownames}]
野田=1×7带有字段的结构数组:的名字
检索的名字
字段,索引到数组中。节点名
包含节点名称的字符向量单元格数组。
节点名称={nodedata(:).name}
节点名=1×7单元阵列{'Service7'}{'Service6'}{'Service1'}{'Service3'}{'Service2'}{'Service4'}{'Service5'}
创建有向图
对象network_graph
使用neo4jStruct2Digraph
函数与图network_graphdata
.
network_graph = neo4jStruct2Digraph (network_graphdata,的节点名节点名)
网络图=具有以下属性的有向图:边:[7×3表]节点:[7×3表]
要查看图形的视觉表示,请创建一个显示图形的图形network_graph
.
绘图(网络图),“EdgeLabel”network_graph.Edges.RelationType)
在图中查找每个服务所依赖的服务数network_graph
。通过使用for循环遍历图中的节点来确定每个节点的依赖项计数。若要确定依赖项计数,请使用最近的
函数。
创建表dependency_count
它存储每个服务的从属服务的计数。按依赖项计数按降序对表中的行进行排序。
dependency_count =表;为i=1:height(network\u graph.Nodes)nodeid=network\u graph.Nodes.Name(i);nearest\u node=nearest(network\u graph,nodeid,Inf,“方向”,“外向”);最近的_长度=长度(最近的_节点);相关性_计数=[相关性_计数;...表(nodeid nearest_length,...“VariableNames”,{“节点”,“依赖项计数”})];结束dependency_count = sortrows (dependency_count, 2)
依赖性计数=7×2表节点依赖项\u计数\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
查找该服务的所有服务的价格
取决于图表network_graph
使用最近的
函数。
disp('服务6依赖于以下服务:');
Service6依赖于以下服务:
最近的(network_graph“价格”正,“方向”,“外向”)
ans =5×1单元阵列{' Service4}{‘Service5}{‘Service3}{”生成“Service2} {' Service1 '}
关闭(neo4jconn)
neo4j
|searchNode
|searchRelation
|最近的