Cypher LOAD CSV - 如何创建属性排序的节点的链接列表?

时间:2017-02-03 22:37:50

标签: csv neo4j cypher

我是Neo4j的新手,正在寻找一些指导: - )

基本上我想从下面的csv创建下面的图表。根据其属性 sequence 的顺序在Points之间创建NEXT关系。如果 sequence 是连续的,我希望能够忽略。有什么想法吗?

(S1:形状) - [:POINTS] - GT;(P1:点)

(S1:形状) - [:POINTS] - GT;(P2:点)

(S1:形状) - [:POINTS] - GT;(P3:点)

(P1) - [:NEXT] - GT;(P2)

(P2)[:NEXT] - >(P3)

等等

shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence,shape_dist_traveled
"1-700-y11-1.1.I","53.42646060879","-6.23930113514121","1","0"
"1-700-y11-1.1.I","53.4268571616632","-6.24059395687542","2","96.6074531286277"
"1-700-y11-1.1.I","53.4269700485041","-6.24093540883784","3","122.549696670773"
"1-700-y11-1.1.I","53.4270439028769","-6.24106779537932","4","134.591291249566"
"1-700-y11-1.1.I","53.4268623569266","-6.24155684094256","5","172.866609667575"
"1-700-y11-1.1.I","53.4268380666968","-6.2417384245122","6","185.235926544428"
"1-700-y11-1.1.I","53.4268874080753","-6.24203735638874","7","205.851454672516"
"1-700-y11-1.1.I","53.427394066848","-6.24287421729846","8","285.060040065768"
"1-700-y11-1.1.I","53.4275257974236","-6.24327509689195","9","315.473852717259"
"1-700-y11-1.2.O","53.277024711771","-6.20739084216546","1","0"
"1-700-y11-1.2.O","53.2777605784999","-6.20671521402849","2","93.4772699644143"
"1-700-y11-1.2.O","53.2780318605927","-6.2068238246152","3","124.525619356934"
"1-700-y11-1.2.O","53.2786209984572","-6.20894363498438","4","280.387737910482"
"1-700-y11-1.2.O","53.2791038678913","-6.21057305710353","5","401.635418300665"
"1-700-y11-1.2.O","53.2790975844245","-6.21075327761739","6","413.677012879457"
"1-700-y11-1.2.O","53.2792296384738","-6.21116766400758","7","444.981964564454"
"1-700-y11-1.2.O","53.2799500357098","-6.21065767664905","8","532.073870043666"
"1-700-y11-1.2.O","53.2800290799386","-6.2105343995296","9","544.115464622458"
"1-700-y11-1.2.O","53.2815594673093","-6.20949562301196","10","727.987702875002"

这是我无法完成的第三部分。创建NEXT关系!

//1. Create Shape
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM 
'file:///D:\\shapes.txt' AS csv
With distinct csv.shape_id as ids
Foreach (x in ids | merge (s:Shape {id: x} ));

//2. Create Point, and Shape to Point relationship
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM 
'file:///D:\\shapes.txt' AS csv
MATCH (s:Shape {id: csv.shape_id})
with s, csv
MERGE (s)-[:POINTS]->(p:Point {id: csv.shape_id, 
lat : csv.shape_pt_lat, lon : csv.shape_pt_lat, 
sequence : toInt(csv.shape_pt_sequence), dist_travelled : csv.shape_dist_traveled});

//3.Create Point to Point relationship
USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM 
'file:///D:\\shapes.txt' AS csv
???

1 个答案:

答案 0 :(得分:1)

您希望为此安装APOC Procedures。它既有批处理方式,也有快速链接集合中所有节点的方法。

由于你已经拥有了数据库中形状的所有形状,你不需要再做一次加载csv,只需使用你已经获得的数据。

我们将apoc.periodic.iterate()批处理每个形状,apoc.nodes.link()按关系链接形状中的所有有序点。

CALL apoc.periodic.iterate(
  "MATCH (s:Shape) RETURN s",
  "WITH {s} as shape 
   MATCH (shape)-[:POINTS]->(point:Point)
   WITH shape, point
   ORDER by point.sequence ASC
   WITH shape, COLLECT(point) as points
   CALL apoc.nodes.link(points,'NEXT')", 
  {batchSize:1000, parallel:true}) YIELD batches, total
  RETURN batches, total

修改

在apoc.periodic.iterate()中使用过程调用时看起来可能存在错误,其中没有发生变异操作(在查询的第二部分中包含SET操作以在某些节点上设置属性后尝试此操作,没有添加属性。)

不确定这是在过程调用中执行过程调用的一般情况,还是特定于apoc.periodic.iterate(),或者只有iterate()和link()才会发生这种情况。

如果我能了解更多有关原因的信息,我会提交错误信息。与此同时,如果您不需要批处理,可以放弃apoc.periodic.iterate():

   MATCH (shape:Shape)-[:POINTS]->(point:Point)
   WITH shape, point
   ORDER by point.sequence ASC
   WITH shape, COLLECT(point) as points
   CALL apoc.nodes.link(points,'NEXT')