Neo4j Load CSV only when unique

泄露秘密 提交于 2019-12-11 09:49:01

问题


If i have csv as follows:

"route_id","service_id","trip_id","trip_headsign"
4973_3, 2, 14519396, "Bf. Tostedt"
4973_3, 2, 14519395, "Bf. Tostedt"
4973_3, 1, 14519379, "Sittensen, Schule"
4973_3, 1, 14519391, "Sittensen, Bahnhofstraße"
4973_3, 2, 14519394, "Bf. Tostedt"
4973_3, 1, 14519390, "Bf. Tostedt"
4973_3, 3, 14519381, "Bf. Tostedt"
4973_3, 4, 14519392, "Bf. Tostedt"

and I want to load only when trip_headsign is unique, so in above case I would only create Trip 3 times. How do i do it?

I am so far as follows:

load csv with headers from  
 'file:///hamburg/trips.txt' as csv   
 create (t:Trip {id: csv.trip_id, service_id: csv.service_id, headsign: csv.trip_headsign}); 

But I get this error:

Node(76020) already exists with label `Trip` and property `headsign` = 'Bf. Tostedt'

I have intentionally keps headsign as unique constraint because I only want everyone of it to exist only once.


回答1:


I figured out now. Thanks

load csv with headers from  
'file:///hamburg/trips.txt' as csv   
merge (t:Trip {headsign: csv.trip_headsign})
on match set t.id =  csv.trip_id
on match set t.service_id = csv.service_id


来源:https://stackoverflow.com/questions/48543121/neo4j-load-csv-only-when-unique

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!