cypher笔记

2018-07-25  本文已影响0人  吃吃吃吃吃

class DriverLifecycleExample:

def __init__(self, uri, user, password):

self._driver = GraphDatabase.driver(uri, auth=(user, password))

def close(self):

              self._driver.close()

在导入时,merge 子句里面 只有在 已经存在 各个标签、属性、属性值 与当前 merge 后面描述的节点 完全相同的 情况下才不会重复创建多余节点。所以你要先整理数据源,保证csv中的每条数据相同的节点 要完全相同

如果已经生成了多余节点,要合并 相同的节点,可以使用存储过程apoc.refactor.mergeNodes,试试,比较好用

MATCH (n:Tag)

WITH n.name AS name, COLLECT(n) AS nodelist, COUNT(*) AS count

WHERE count > 1

CALL apoc.refactor.mergeNodes(nodelist) YIELD node

RETURN node


https://stackoverflow.com/questions/42800137/neo4j-cypher-merge-duplicate-nodes

合并nodes,似乎要确保完全一样,关系也可以合并:

APOC Procedures has some graph refactoring procedures that can help. I think apoc.refactor.mergeNodes() ought to do the trick.

Be aware that in addition to transferring all relationships from the other nodes onto the first node of the list, it will also apply any labels and properties from the other nodes onto the first node. If that's not something you want to do, then you may have to collect incoming and outgoing relationships from the other nodes and use apoc.refactor.to() and apoc.refactor.from() instead.

Here's the query for merging nodes:

MATCH (n:Tag)

WITH n.name AS name, COLLECT(n) AS nodelist, COUNT(*) AS count

WHERE count > 1

CALL apoc.refactor.mergeNodes(nodelist) YIELD node

RETURN node


没有标签的节点

MATCH (n) WHERE NOT labels(n) RETURN n


neo4j使用cypher查询路径避免出现环路

https://blog.csdn.net/wry2008wry/article/details/80899860 


查找没有任何关系的节点

MATCH (s) WHERE NOT ()-[]-(s) RETURN s


查找tom的关系节点

MATCH (n:Person{name:"tom"})-->() return n

不必用where n.name=...


四条及以内关系的任何点

MATCH (n)-[*1..4]-() RETURN ...


查找a和b最短关系路径

MATCH p=shortestPath(

(x:xxx{xxx:"xxx"})-[*]-(x:xxx{xxx:"xxx"})

)


mysql到处csv文件

select ....

into outfile 'xxx.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';


所有演员各自参演电影的数量

MATCH (a:Person)-[:ACTED_IN]->(m:Movie)

WITH a,count(m) AS nrOfMovies

RETURN a{.name, nrOfMovies}


字段-数据元有多条关系的:

match p=(n:物理表)-->(a:物理表字段)-[r]->(b:数据元) with n,a,count(r) as rels, collect(b) as de where rels>1 return n,a,rels,de

注意 with里没有b,想想为什么


foreach用法

FOREACH (r IN relationships(path) | SET r.marked=true)

FOREACH (value IN coll | CREATE (:Person {name:value}))


CALL db.labels() YIELD label

shortestPath((n1:Person)-[*..6]-(n2:Person)) Find a single shortest path.

allShortestPaths((n1:Person)-[*..6]->(n2:Person))Find all shortest paths.

range($firstNum, $lastNum, $step) AS list

返回list的函数还有:labels(), nodes(), relationships(), filter(), extract()


extract(x IN nodes(path) | x.prop)

Extract properties from the nodes in a path.


节点 由接口 org.neo4j.graphdb.Node 表示

关系是 org.neo4j.graphdb.Relationship

关系类型是 org.neo4j.graphdb.RelationshipType

对属性操作的方法在org.neo4j.graphdb.PropertyContainer  【Node、Relationship接口都继承了】,常用 getProperty和setProperty


MATCH (a:业务流程)-[r]->(b:底层业务)

WITH a, b, collect(r) as rels

CALL apoc.refactor.mergeRelationships(rels,{properties:"combine"})

YIELD rel RETURN rel


可视化

https://neo4j.com/graph-visualization-neo4j/

上一篇下一篇

猜你喜欢

热点阅读