加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_阳江站长网 (https://www.0662zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 政策 > 正文

PostgreSQL技巧分享:图(graph)的递归查询实例

发布时间:2020-10-16 10:12:12 所属栏目:政策 来源:做站长
导读:这篇文章主要给大家介绍了关于PostgreSQL图(graph)的递归查询的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用PostgreSQL具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧 背景 在树形递归查询这篇文章,我记录了使用CTE语法查

dap=# select * from demo.f_get_rel(7,0,3);
 direct | cur_depth | up_node | down_node | trace
——–+———–+———+———–+———–
  1 |   1 |  7 |   2 | {7,2}
  1 |   1 |  7 |   4 | {7,4}
  1 |   2 |  2 |   4 | {7,2,4}
  -1 |   1 |  3 |   7 | {3,7}
  -1 |   1 |  4 |   7 | {4,7}
  -1 |   1 |  5 |   7 | {5,7}
  -1 |   2 |  2 |   4 | {2,4,7}
  -1 |   2 |  6 |   5 | {6,5,7}
  -1 |   3 |  1 |   2 | {1,2,4,7}
  -1 |   3 |  5 |   2 | {5,2,4,7}
(10 rows)

图形显示结果

ECharts模板

在没有集成图形界面之前,使用ECharts的示例代码(地址),可以直观的查看关系图谱。对官方样表进行微调之后,代码如下

注意 代码中的 data 和 links 部分需要进行替换

option = {
 title: {
  text: ‘数据图谱’
 },
 tooltip: {},
 animationDurationUpdate: 1500,
 animationEasingUpdate: ‘quinticInOut’,
 series : [
  {
   type: ‘graph’,
   layout: ‘force’,
   force: {
     repulsion: 1000
    },
   focusNodeAdjacency: true,
   symbolSize: 30,
   roam: true,
   label: {
    normal: {
     show: true
    }
   },
   edgeSymbol: [‘circle’, ‘arrow’],
   edgeSymbolSize: [4, 10],
   edgeLabel: {
    normal: {
     textStyle: {
      fontSize: 20
     }
    }
   },
   data: [
    { name:”2″, draggable: true, symbolSize:20},
   ],
   links: [
    { source:”2″, target:”4″},
   ],

  }
 ]
};

造显示用数据

构造 data 部分

— 根据节点的关联点数量,设置图形大小
with rel as (select * from f_get_rel(7,0,2)),
 up_nodes as (select up_node, count(distinct down_node) as out_cnt from rel group by up_node),
 down_nodes as (select down_node, count(distinct up_node) as in_cnt from rel group by down_node),
 node_cnt as ( select up_node as node, out_cnt as cnt from up_nodes union all select * from down_nodes )
select ‘{ name:”‘ || n.node || ‘”, draggable: true, symbolSize:’ || sum(n.cnt) * 10 || ‘},’ as node
 from node_cnt n
group by n.node
order by 1;

构造 links 部分

select distinct r.up_node, r.down_node, ‘{ source:”‘|| r.up_node ||'”, target:”‘|| r.down_node ||'”},’ as links
 from f_get_rel(7,0,3) r
order by r.up_node ;

图形显示

把构造的data和links替换到ECharts代码里面

查询节点7的所有2度关联节点信息,结果显示如下

PostgreSQL技巧分享:图(graph)的递归查询实例

查询节点7的所有关联节点信息(不限层级数),结果显示如下

PostgreSQL技巧分享:图(graph)的递归查询实例

本文来自脚本之家,原文链接:https://www.jb51.net/article/176407.htm

PostgreSQL技巧分享:图(graph)的递归查询实例

申请创业报道,分享创业好点子。点击此处,共同探讨创业新机遇!

本文素材来自互联网

(编辑:应用网_阳江站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读