Here’s the table of contents:
自动化生成图模式CYPHER查询语句
通过JSON定义的图格式数据,抽取图模式并拼接为CYPHER语句。根据CYPHER执行代价自动优化CYPHER查询。
函数使用方式
// {limit小于1表示不限制匹配子图个数}
RETURN olab.schema.auto.cypher({json},{limit}) AS autoCypher
autoCypher返回的格式
- 节点【图模式只包含节点】
RETURN n
- 图对象【path集合】【图模式包含节点与关系并且不包含孤立节点】
RETURN {graph:[path1,path2]} AS graph LIMIT 1
过滤器
涉及到对指标数据的过滤,可以根据数据模型选择不同的过滤器
属性过滤器
抽象定义对节点和关系的一级属性【暂不支持二级属性的过滤】进行过滤;
{var}
表示对当前过滤对象的抽象定义;过滤条件之间支持使用AND连接。"properties_filter": [ { "hcreatetime": "{var}.hcreatetime='20201116032333'" }, { "count": "{var}.count>=0 AND {var}.count<=10" } ]
Elasticsearch过滤器
主要实现对节点和关系的二级属性过滤查询;被过滤对象和过滤条件之间使用
entity_unique_code:{var}
对接,entity_unique_code
字段是默认的ONgDB和Elasticsearch数据对接的字段;依赖custom.es.result.bool
函数执行ES过滤。"es_filter": [ { "es_url": "10.20.13.130:9200", "index_name": "index_name_1", "query": "{size:1,query:{term:{entity_unique_code:{var}}}}}" } ]
使用场景
使用场景-无代码化分析获取图数据:对节点和关系的二级属性执行过滤;对节点和关系的指标执行过滤;挖掘满足多重指标限制的图模式;时序子图的过滤。
依赖
ongdb集群需要安装auto-cypher-para\install\es-function.cql所示的函数
-- 函数安装方式 -- 使用场景:对节点和关系的指标执行过滤;挖掘满足多重指标限制的图模式;时序子图的过滤 CALL apoc.custom.asFunction( 'es.result.bool', 'CALL apoc.es.query($esuUrl,$indexName,\'\',null,$queryDsl) YIELD value WITH value.hits.total.value AS count CALL apoc.case([count>0,\'RETURN TRUE AS countBool\'],\'RETURN FALSE AS countBool\') YIELD value RETURN value.countBool AS bool', 'BOOLEAN', [['esuUrl','STRING'],['indexName','STRING'],['queryDsl','MAP']], false, '通过判断ES查询结果返回FALSE或者TRUE【结果集大于0返回TRUE】' ); -- 访问方式:RETURN custom.es.result.bool('10.20.13.130:9200','dl_default_indicator_def',{size:1,query:{term:{product_code:"PF0020020104"}}}) AS boolean
根据auto-cypher分析的案例schema
生成图模式扩展的样例结构
- 无环路
CREATE (org1:公司) SET org1.name='公司' WITH org1 CREATE (org2:公司) SET org2.name='公司' WITH org1,org2 CREATE (org3:公司) SET org3.name='公司' WITH org1,org2,org3 CREATE (loc:位置) SET loc.name='位置' WITH org1,org2,org3,loc CREATE (pro:产品) SET pro.name='产品' WITH org1,org2,org3,loc,pro CREATE (idu1:行业) SET idu1.name='行业' WITH org1,org2,org3,loc,pro,idu1 CREATE (idu2:行业) SET idu2.name='行业' WITH org1,org2,org3,loc,pro,idu1,idu2 CREATE p1=(org1)-[:持股]->(org2) WITH org1,org2,org3,loc,pro,idu1,idu2,p1 CREATE p2=(org1)-[:担保]->(org3) WITH org1,loc,pro,idu1,idu2,p1,p2 CREATE p3=(org1)-[:属于]->(loc) WITH org1,pro,idu1,idu2,p1,p2,p3 CREATE p4=(org1)-[:生产]->(pro) WITH org1,idu1,idu2,p1,p2,p3,p4 CREATE p5=(org1)-[:属于]->(idu1) WITH org1,idu1,idu2,p1,p2,p3,p4,p5 CREATE p6=(idu1)-[:上下游]->(idu2) WITH p1,p2,p3,p4,p5,p6 RETURN {graph:[p1,p2,p3,p4,p5,p6]} AS graph
- 单环路
CREATE (n1:公司) SET n1.name='公司' WITH n1 CREATE (n2:公司) SET n2.name='公司' WITH n1,n2 CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3 CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4 CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1 CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2 CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3 CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4 CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5 CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6 RETURN {graph:[p1,p2,p3,p4,p5,p6]} AS graph
- 多个环路
CREATE (n1:公司) SET n1.name='公司' WITH n1 CREATE (n2:公司) SET n2.name='公司' WITH n1,n2 CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3 CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4 CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1 CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2 CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3 CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4 CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5 CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6 CREATE p7=(n2)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7 CREATE p8=(n1)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7,p8 RETURN {graph:[p1,p2,p3,p4,p5,p6,p7,p8]} AS graph
使用案例
入参【根据图模式扩展的样例结构生成】
graph-data:auto-cypher-para\graph-pro-es.json
查询
// {limit}=-1不限制数据量;{json}输入graph-data,-入参【根据图模式扩展的样例结构生成】
RETURN olab.schema.auto.cypher({json},-1) AS autoCypher
olab.schema.auto.cypher执行结果
- 无环路
CREATE (org1:公司) SET org1.name='公司' WITH org1 CREATE (org2:公司) SET org2.name='公司' WITH org1,org2 CREATE (org3:公司) SET org3.name='公司' WITH org1,org2,org3 CREATE (loc:位置) SET loc.name='位置' WITH org1,org2,org3,loc CREATE (pro:产品) SET pro.name='产品' WITH org1,org2,org3,loc,pro CREATE (idu1:行业) SET idu1.name='行业' WITH org1,org2,org3,loc,pro,idu1 CREATE (idu2:行业) SET idu2.name='行业' WITH org1,org2,org3,loc,pro,idu1,idu2 CREATE p1=(org1)-[:持股]->(org2) WITH org1,org2,org3,loc,pro,idu1,idu2,p1 CREATE p2=(org1)-[:担保]->(org3) WITH org1,loc,pro,idu1,idu2,p1,p2 CREATE p3=(org1)-[:属于]->(loc) WITH org1,pro,idu1,idu2,p1,p2,p3 CREATE p4=(org1)-[:生产]->(pro) WITH org1,idu1,idu2,p1,p2,p3,p4 CREATE p5=(org1)-[:属于]->(idu1) WITH org1,idu1,idu2,p1,p2,p3,p4,p5 CREATE p6=(idu1)-[:上下游]->(idu2) WITH p1,p2,p3,p4,p5,p6 WITH olab.convert.json([p1,p2,p3,p4,p5,p6]) AS json RETURN olab.schema.auto.cypher(json,100) AS cypher
CREATE (org1:公司) SET org1.name='公司' WITH org1 CREATE (org2:公司) SET org2.name='公司' WITH org1,org2 CREATE (org3:公司) SET org3.name='公司' WITH org1,org2,org3 CREATE (loc:位置) SET loc.name='位置' WITH org1,org2,org3,loc CREATE (pro:产品) SET pro.name='产品' WITH org1,org2,org3,loc,pro CREATE (idu1:行业) SET idu1.name='行业' WITH org1,org2,org3,loc,pro,idu1 CREATE (idu2:行业) SET idu2.name='行业' WITH org1,org2,org3,loc,pro,idu1,idu2 CREATE p1=(org1)-[:持股]->(org2) WITH org1,org2,org3,loc,pro,idu1,idu2,p1 CREATE p2=(org1)-[:担保]->(org3) WITH org1,loc,pro,idu1,idu2,p1,p2 CREATE p3=(org1)-[:属于]->(loc) WITH org1,pro,idu1,idu2,p1,p2,p3 CREATE p4=(org1)-[:生产]->(pro) WITH org1,idu1,idu2,p1,p2,p3,p4 CREATE p5=(org1)-[:属于]->(idu1) WITH org1,idu1,idu2,p1,p2,p3,p4,p5 CREATE p6=(idu1)-[:上下游]->(idu2) WITH p1,p2,p3,p4,p5,p6 WITH olab.convert.json([p1,p2,p3,p4,p5,p6]) AS json WITH olab.schema.auto.cypher(json,100) AS cypher CALL apoc.cypher.run(cypher,{}) YIELD value RETURN value.graph AS graph
- 单环路
CREATE (n1:公司) SET n1.name='公司' WITH n1 CREATE (n2:公司) SET n2.name='公司' WITH n1,n2 CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3 CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4 CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1 CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2 CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3 CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4 CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5 CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6 WITH olab.convert.json([p1,p2,p3,p4,p5,p6]) AS json RETURN olab.schema.auto.cypher(json,100) AS cypher
CREATE (n1:公司) SET n1.name='公司' WITH n1 CREATE (n2:公司) SET n2.name='公司' WITH n1,n2 CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3 CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4 CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1 CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2 CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3 CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4 CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5 CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6 WITH olab.convert.json([p1,p2,p3,p4,p5,p6]) AS json WITH olab.schema.auto.cypher(json,100) AS cypher CALL apoc.cypher.run(cypher,{}) YIELD value RETURN value.graph AS graph
- 多环路
CREATE (n1:公司) SET n1.name='公司' WITH n1 CREATE (n2:公司) SET n2.name='公司' WITH n1,n2 CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3 CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4 CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1 CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2 CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3 CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4 CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5 CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6 CREATE p7=(n2)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7 CREATE p8=(n1)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7,p8 WITH olab.convert.json([p1,p2,p3,p4,p5,p6,p7,p8]) AS json RETURN olab.schema.auto.cypher(json,100) AS cypher
CREATE (n1:公司) SET n1.name='公司' WITH n1 CREATE (n2:公司) SET n2.name='公司' WITH n1,n2 CREATE (n3:公司) SET n3.name='公司' WITH n1,n2,n3 CREATE (n4:行业) SET n4.name='行业' WITH n1,n2,n3,n4 CREATE p1=(n1)-[:持股]->(n2) WITH n1,n2,n3,n4,p1 CREATE p2=(n1)-[:担保]->(n2) WITH n1,n2,n3,n4,p1,p2 CREATE p3=(n1)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3 CREATE p4=(n1)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4 CREATE p5=(n2)-[:持股]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5 CREATE p6=(n3)-[:属于]->(n4) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6 CREATE p7=(n2)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7 CREATE p8=(n1)-[:担保]->(n3) WITH n1,n2,n3,n4,p1,p2,p3,p4,p5,p6,p7,p8 WITH olab.convert.json([p1,p2,p3,p4,p5,p6,p7,p8]) AS json WITH olab.schema.auto.cypher(json,100) AS cypher CALL apoc.cypher.run(cypher,{}) YIELD value RETURN value.graph AS graph
参考链接
PREVIOUSCYPHER自动生成接口设计
NEXTGradle安装