class DriverLifecycleExample:
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
? ? ? ? ? ? ? self._driver.close()
在導(dǎo)入時(shí),merge 子句里面 只有在 已經(jīng)存在 各個(gè)標(biāo)簽、屬性、屬性值 與當(dāng)前 merge 后面描述的節(jié)點(diǎn) 完全相同的 情況下才不會(huì)重復(fù)創(chuàng)建多余節(jié)點(diǎn)。所以你要先整理數(shù)據(jù)源,保證csv中的每條數(shù)據(jù)相同的節(jié)點(diǎn) 要完全相同
如果已經(jīng)生成了多余節(jié)點(diǎn),要合并 相同的節(jié)點(diǎn),可以使用存儲(chǔ)過程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,似乎要確保完全一樣,關(guān)系也可以合并:
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
沒有標(biāo)簽的節(jié)點(diǎn)
MATCH (n) WHERE NOT labels(n) RETURN n
neo4j使用cypher查詢路徑避免出現(xiàn)環(huán)路
https://blog.csdn.net/wry2008wry/article/details/80899860?
查找沒有任何關(guān)系的節(jié)點(diǎn)
MATCH (s) WHERE NOT ()-[]-(s) RETURN s
查找tom的關(guān)系節(jié)點(diǎn)
MATCH (n:Person{name:"tom"})-->() return n
不必用where n.name=...
四條及以內(nèi)關(guān)系的任何點(diǎn)
MATCH (n)-[*1..4]-() RETURN ...
查找a和b最短關(guān)系路徑
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';
所有演員各自參演電影的數(shù)量
MATCH (a:Person)-[:ACTED_IN]->(m:Movie)
WITH a,count(m) AS nrOfMovies
RETURN a{.name, nrOfMovies}
字段-數(shù)據(jù)元有多條關(guān)系的:
match p=(n:物理表)-->(a:物理表字段)-[r]->(b:數(shù)據(jù)元) 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的函數(shù)還有:labels(), nodes(), relationships(), filter(), extract()
extract(x IN nodes(path) | x.prop)
Extract properties from the nodes in a path.
節(jié)點(diǎn) 由接口 org.neo4j.graphdb.Node 表示
關(guān)系是 org.neo4j.graphdb.Relationship
關(guān)系類型是 org.neo4j.graphdb.RelationshipType
對(duì)屬性操作的方法在org.neo4j.graphdb.PropertyContainer? 【Node、Relationship接口都繼承了】,常用 getProperty和setProperty
MATCH (a:業(yè)務(wù)流程)-[r]->(b:底層業(yè)務(wù))
WITH a, b, collect(r) as rels
CALL apoc.refactor.mergeRelationships(rels,{properties:"combine"})
YIELD rel RETURN rel
可視化
https://neo4j.com/graph-visualization-neo4j/