我无法从文档中看到如何将批量数据与属性合并。此处的示例使用 R,但解决方案将是 Cypher。我需要指定id
为唯一字段,以便在更改属性或添加新属性数据时不会重复该节点。
x = data.frame(id = 1:2, name = c('Kate','Charles'),
age = c(27,34), grp = c('A','B'), stringsAsFactors = F)
merge_df_with_graph = function(x, graph, query){
t = newTransaction(graph)
for (i in 1:nrow(x)) {
appendCypher(t, query, id = x$id[i], name = x$name[i],
age = x$age[i], grp = x$grp[i])
}
commit(t)
}
merge_df_with_graph(x, graph, "MERGE (n:User {id:{id}, name:{name}});")
n = getNodes(graph, 'MATCH (n) RETURN (n);')
length(n)
#> [1] 2
# when an attribute changes
x$name[2] = 'Charlie'
merge_df_with_graph(x, graph, "MERGE (n:User {id:{id}, name:{name}});")
n = getNodes(graph, 'MATCH (n) RETURN (n);')
length(n)
#> [1] 3
# when new attributes data is added
merge_df_with_graph(x, graph, "MERGE (n:User {id:{id}, name:{name}, age:{age}});")
n = getNodes(graph, 'MATCH (n) RETURN (n);')
length(n)
#> [1] 5
我是 neo4j 的新手,文档中的示例没有指定如何定义唯一键以外的属性。感谢您的帮助。