Neo4j-Apoc

APOC

https://neo4j-contrib.github.io/neo4j-apoc-procedures/#_virtual_nodes_rels

提供的函数 存储过程应有尽有, 也可以自行实现添加

CALL apoc.help("dijkstra")

Apoc配置:

apoc.trigger.enabled=false/true  :  Enable triggers

apoc.ttl.enabled=false/true:  Enable time to live background task

apoc.jdbc.<key>.uri=jdbc-url-with-credentials :  配置数据库连接串

apoc.es.<key>.uri=es-url-with-credentials:  ES连接

apoc.mongodb.<key>.uri=mongodb-url-with-credentials: mongodb连接

apoc.couchbase.<key>.uri=couchbase-url-with-credentials: couchbase连接

apoc.jobs.scheduled.num_threads=number-of-threads: APOC调度器的线程池

apoc.jobs.pool.num_threads=number-of-threads: 执行器的线程池

有用的函数方法:

解析域名: WITH 'http://www.example.com/all-the-things' AS url RETURN apoc.data.domain(url) // will return 'www.example.com'

日期函数: 

    apoc.date.parse('2015/03/25 03-15-59',['s'],['yyyy/MM/dd HH/mm/ss'])

    apoc.date.add(12345, 'ms', -365, 'd') 


格式转换:

    return apoc.number.format(12345.67) as value

    return apoc.number.format(12345, '#,##0.00;(#,##0.00)', 'it') as value

数学运算:

    RETURN apoc.number.exact.add(stringA,stringB)

    RETURN apoc.number.exact.sub(stringA,stringB)

    ETURN apoc.number.exact.mul(stringA,stringB,[prec],[roundingModel]

    RETURN apoc.number.exact.div(stringA,stringB,[prec],[roundingModel])

    RETURN apoc.number.exact.toInteger(string,[prec],[roundingMode])

    RETURN apoc.number.exact.toFloat(string,[prec],[roundingMode])

    RETURN apoc.number.exact.toExact(number)

比较节点不同:

    apoc.diff.nodes([leftNode],[rightNode])

图算法:

路径扩展(选择走哪些边, 哪些节点, 几层, 什么时候结束等等):

    CALL apoc.path.expand(startNode <id>|Node, relationshipFilter, labelFilter, minLevel, maxLevel )

    MATCH (user:User) WHERE user.id = 460
    CALL apoc.path.expandConfig(user,{relationshipFilter:"RATED",minLevel:3,maxLevel:3,bfs:false,uniqueness:"NONE"}) YIELD path
    RETURN count(*);


    apoc.path.subgraphAll(startNode <id>Node/list, {maxLevel, relationshipFilter, labelFilter, bfs:true, filterStartNode:true, limit:-1}) yield nodes, relationships


    MATCH (user:User) WHERE user.id = 460
    CALL apoc.path.subgraphNodes(user, {}) YIELD node
    RETURN node;

Closeness Centrality:

    CALL apoc.algo.closeness(['TYPE'],nodes,'INCOMING') YIELD node, score

Betweenness Centrality:

    CALL apoc.algo.betweenness(['TYPE'],nodes,'BOTH') YIELD node, score


pageRank:

    CALL apoc.algo.pageRank(nodes) YIELD node, score

    // only compute over relationships of types TYPE_1 or TYPE_2
    
    CALL apoc.algo.pageRankWithConfig(nodes,{types:'TYPE_1|TYPE_2'}) YIELD node, score

    // peroform 10 page rank iterations, computing only over relationships of type TYPE_1

    CALL apoc.algo.pageRankWithConfig(nodes,{iterations:10,types:'TYPE_1'}) YIELD node, score

dijkstra:

    apoc.algo.dijkstra(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 'distance') YIELD path, weight

    apoc.algo.dijkstraWithDefaultWeight(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 'distance', 10) YIELD path, weight

    example:

        MATCH (from:Loc{name:'A'}), (to:Loc{name:'D'})
        CALL apoc.algo.dijkstra(from, to, 'ROAD', 'd') yield path as path, weight as weight
        RETURN path, weight

community: 标签传播的社区发现算法

    apoc.algo.community(times,labels,partitionKey,type,direction,weightKey,batchSize)

    example:  遍历25轮, 

        CALL apoc.algo.community(25,null,'partition','X','OUTGOING','weight',10000)


aStar: A*遍历算法

    apoc.algo.aStar(startNode, endNode, 'KNOWS|<WORKS_WITH|IS_MANAGER_OF>', 'distance','lat','lon') YIELD path, weight

cliques: 聚类社区算法:

    apoc.algo.cliques(minSize) YIELD clique

    apoc.algo.cliquesWithNode(startNode, minSize) YIELD clique

各种距离算法:

    apoc.algo.cosineSimilarity([vector1], [vector2])   cosin相似度

    apoc.algo.euclideanDistance([vector1], [vector2])  欧几里得距离

    apoc.algo.euclideanSimilarity([vector1], [vector2])  欧几里得相似度

Virtual Nodes/Rels: 虚拟节点, 关系 类似视图的概念

MATCH (a)-[r]->(b)
WITH head(labels(a)) AS l, head(labels(b)) AS l2, type(r) AS rel_type, count(*) as count
CALL apoc.create.vNode([l],{name:l}) yield node as a
CALL apoc.create.vNode([l2],{name:l2}) yield node as b
CALL apoc.create.vRelationship(a,rel_type,{count:count},b) yield rel
RETURN *;

CALL apoc.create.vPattern({_labels:['Person'],name:'Mary'},'KNOWS',{since:2012},{_labels:['Person'],name:'Michael'})

CALL apoc.create.vPattern({_labels:['Person', 'Woman'],name:'Mary'},'KNOWS',{since:2012},{_labels:['Person', 'Man'],name:'Michael'})

CALL apoc.create.vPatternFull(['British','Person'],{name:'James', age:28},'KNOWS',{since:2009},['Swedish','Person'],{name:'Daniel', age:30})

Cypher Exectuion: 批量执行脚本

CALL apoc.cypher.runFiles([files or urls],{config}) yield row, result   

runs each statement in the files, all semicolon separated


CALL apoc.cypher.runFile(file or url,{config}) yield row, result
runs each statement in the file, all semicolon separated - currently no schema operations

Manual Index: 手工索引, 默认不会自动更新

synchronously同步更新 只有创建索引时指定autoUpdate:true, 才会真正生效, 更新图时性能上会有影响

    apoc.autoIndex.enabled=true  

asynchronously异步更新

    apoc.autoIndex.async=true  

    默认的异步更新参数:50000 operations or 5000 milliseconds 触发

    apoc.autoIndex.queue_capacity=100000
    apoc.autoIndex.async_rollover_opscount=50000
    apoc.autoIndex.async_rollover_millis=5000
    apoc.autoIndex.tx_handler_stopwatch=false


添加多个节点属性缩影 配合search用, 重复执行会先删除再创建: apoc.index.addAllNodes('index-name',{label1:['prop1',…​],…​}, {options})  

options的选择(map形式给入):

    type: fulltext/exact  全文索引/精确索引

    to_lower_case: false/true

    analyzer: classname   用哪种classname of lucene analyzer 

    similarity: classname   相似度计算的方式 classname for lucene similarity 

    autoUpdate:  true/false  是否自动更新


添加一个节点的属性索引(可以不存在这个属性字段) apoc.index.addNode(node,['prop1',…​])

对于给定的标签,添加节点索引(索引名称的Label和node可以不一致): apoc.index.addNodeByLabel('Label',node,['prop1',…​])

给索引命名 默认是Label作为名称:  apoc.index.addNodeByName('name',node,['prop1',…​])

apoc.index.addNodeMap(node,{key:value})

apoc.index.addNodeMapByName(index, node,{key:value})

关系索引: apoc.index.addRelationship(rel,['prop1',…​])

apoc.index.addRelationshipByName('name',rel,['prop1',…​])

apoc.index.addRelationshipMap(rel,{key:value})

apoc.index.addRelationshipMapByName(index, rel,{key:value})

删除节点索引 apoc.index.removeNodeByName('name',node) remove node from an index for the given name


索引模糊查询(计算编辑距离)  apoc.index.search('index-name', 'query') YIELD node, weight

apoc.index.nodes('Label','prop:value*') YIELD node, weight

apoc.index.relationships('TYPE','prop:value*') YIELD rel, weight

没理解: apoc.index.between(node1,'TYPE',node2,'prop:value*') YIELD rel, weight

示例:
    match (p:Person) call apoc.index.addNode(p,["name","age"]) RETURN count(*); // 129s for 1M People

    call apoc.index.nodes('Person','name:name100*') YIELD node, weight return * limit 2

Index Management:

CALL apoc.index.remove('Thing') 

展示: CALL apoc.index.list() 

CALL apoc.index.forNodes('name',{config}) YIELD type,name,config

CALL apoc.index.forRelationships('name',{config}) YIELD type,name,config

Triggers : 触发器

apoc.trigger.enabled=true

Locking: 锁

call apoc.lock.nodes([nodes])

call apoc.lock.rels([relationships])

call apoc.lock.all([nodes],[relationships])

Meta Graph: 展示节点, 关系标签的概览图

CALL apoc.meta.graphSample()

CALL apoc.meta.graph

有选择的展示一部分结果: CALL apoc.meta.subGraph({labels:[labels],rels:[rel-types],excludes:[label,rel-type,…​]})

表格形式展示数据: CALL apoc.meta.data

Map形式展示数据: CALL apoc.meta.schema


快速查看图中各种存在的节点,边: CALL apoc.meta.stats yield labelCount, relTypeCount, propertyKeyCount, nodeCount, relCount, labels, relTypes, stats

Sehema: 查看各种索引, 约束

apoc.schema.assert({indexLabel:[indexKeys],…​},{constraintLabel:[constraintKeys],…​}, dropExisting : true) yield label, key, unique, action

apoc.schema.nodes() yield name, label, properties, status, type

apoc.schema.relationships() yield name, type, properties, status

apoc.schema.node.indexExists(labelName, properties)

apoc.schema.node.constraintExists(labelName, properties)

apoc.schema.relationship.constraintExists(type, properties)

Streaming Data to Gephi: 导出数据到Gephi

apoc.gephi.add(url-or-key, workspace, data, weightproperty, ['exportproperty'])

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容