概述
全图K邻算法能识别图中每个节点的邻域。该算法广泛应用于关系发现、影响力预测、好友推荐等场景中。
全图K邻算法可以看作是K邻查询UQL命令的批量执行。
特殊说明
尽管全图K邻算法针对高并发性能进行了优化,但需要注意,在处理大型图(具有数千万个节点或边的图)或包含许多超级节点的图时,此算法仍会消耗大量计算资源。为了优化性能,要充分考虑图的特定特征和大小,建议避免执行过深的全图K邻计算。
在图G=(V, E)中,如果|E|/|V|=100,理论上查询一个节点的5跳邻居需要105(相当于100亿次计算)的计算复杂度,大约需要100毫秒。由此推断,在具有1千万节点的图中完成此类查询将需要100万秒(相当于约12天)。在处理这种规模的图形时,考虑计算需求和时间要求非常重要。
语法
- 命令:
algo(khop_all) - 参数:
名称 |
类型 |
规范 |
默认 |
可选 |
描述 |
|---|---|---|---|---|---|
| ids / uuids | []_id / []_uuid |
/ | / | 是 | K邻查询目标节点的ID/UUID,忽略则查询全部点 |
| k_start | int | ≥1 | 1 |
是 | K邻查询的起始深度,查询深度为[k_start, k_end] |
| k_end | int | ≥1 | 1 |
是 | K邻查询的终止深度,查询深度为[k_start, k_end] |
| direction | string | in, out |
/ | 是 | 查询路径中所有边的方向 |
| node_property | []@<schema>?.<property> |
数值类型,需LTE | / | 是 | 要进行聚合统计的所有点属性;此项要与aggregate_opt配合使用 |
| aggregate_opt | []string | max, min, mean, sum, var, dev |
/ | 是 | 对于指定点属性进行聚合统计的方法;此项要与node_property配合使用,每个方法对应一个属性max:最大值,min:最小值,mean:平均值,sum:求和,var:方差,dev:标准差 |
| src_include | int | 0, 1 |
0 |
是 | 1代表将目标节点包含在其查询和聚合结果中,0则不包含目标节点 |
| limit | int | ≥-1 | -1 |
是 | 返回的结果条数,-1返回所有结果 |
示例
示例是一个银行卡转账网络:

文件回写
配置项 |
回写内容 | 描述 |
|---|---|---|
| filename_ids | _id,_id |
第一个_id代表目标节点,第二个_id代表目标节点的邻居 |
| filename | _id,aggregate_result1,...,aggregate_resultN,count |
_id代表目标节点,aggregate_result1~aggregate_resultN是聚合结果,最后的count是目标节点的邻居数 |
algo(khop_all).params({
ids: ['card1', 'card7'],
k_start: 2,
k_end: 3,
direction: 'out',
node_property: ['@card.level', '@card.balance'],
aggregate_opt: ['max', 'mean']
}).write({
file:{
filename_ids: 'neighbors',
filename: 'aggregations'
}
})
结果:文件neighbors、aggregations
card1,card7
card1,card3
card1,card4
card7,card4
card1,4.000000,3174.103333,3.000000,
card7,2.000000,4768.800000,1.000000,
属性回写
| 配置项 | 回写内容 | 回写至 | 数据类型 |
|---|---|---|---|
| property | 邻居数 | 点属性 | double |
algo(khop_all).params({
k_start: 2,
k_end: 2
}).write({
db:{
property: 'khop2'
}
})
结果:每个节点的2步邻居数量回写至名为khop2的点属性下
直接返回
| 别名序号 | 类型 | 描述 |
列名 |
|---|---|---|---|
| 0 | []perNode | 点及其聚合统计结果、邻居数 | _uuid, value |
algo(khop_all).params({
ids: ['card1', 'card7'],
k_start: 2,
k_end: 3,
node_property: ['@card.level', '@card.balance'],
aggregate_opt: ['max', 'mean']
}) as r
return r
结果:r
| _uuid | value |
|---|---|
| 1 | 5.000000,6884.060000,6.000000, |
| 7 | 5.000000,7361.870000,5.000000, |
流式返回
| 别名序号 | 类型 | 描述 |
列名 |
|---|---|---|---|
| 0 | []perNode | 点及其聚合统计结果、邻居数 | _uuid, value |
algo(khop_all).params({
uuids: [2],
k_start: 2,
k_end: 2,
node_property: '@card.balance',
aggregate_opt: 'max'
}).stream() as results
return results
结果:results
| _uuid | value |
|---|---|
| 2 | 27123.800000,2.000000, |