当前位置 主页 > 网站技术 > 代码类 >

    PostgreSQL图(graph)的递归查询实例(2)

    栏目:代码类 时间:2019-12-15 15:06

    测试一下,查询节点7的所有3度关联节点信息,如下

    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度关联节点信息,结果显示如下

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

    总结

    以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对IIS7站长之家的支持。