neo4j:批量导入关系

时间:2013-07-04 09:02:01

标签: ruby neo4j neography

我在图表中导入关系时遇到了麻烦。

假设我已经创建了几百个唯一的索引用户。然后我想创建大约120k节点,每个节点通过关系链接到某个用户。

不幸的是,我无法找到批量导入的方法。我正在尝试使用 neography ruby​​ gem执行此操作,但由于我对这个环境非常新,我不介意在需要时使用其他方法。

我尝试了什么:

@neo.batch(
  [:get_node_index, 'user', 'user_id', '1'], #attempt to get the node from index
  [:create_node, {"foo => 'bar'}],
  [:create_relationship, "has" , "{0}", "{1}"] 
) # => fails

@neo.batch(
  [:create_unique_node, "user", "user_id", "1"], #attempt to create or get the node
  [:create_node, {"foo" => "bar"}],
  [:create_relationship, "has", "{0}", "{1}"]
) # => fails. 

请注意,仅可以批量处理一些create_unique_node命令。

我可以运行脚本的唯一方法是使用

@neo.batch(
  [:create_node, {"user_id" => 1}], #works, but duplicates the node
  [:create_node, {"foo" => "bar"}],
  [:create_relationship, "has", "{0}", "{1}"]
) # => success

然而,这将复制我的所有用户节点,这绝对不是我想要实现的。 看来我的问题类似于this one,但我根本不知道在创建关系时我应该如何使用索引。

任何帮助都会非常感谢,提前谢谢

1 个答案:

答案 0 :(得分:0)

由于这个问题已被提出,我发布了自那时以来我找到的解决方法:

正如问题中提到的,可以批量create_unique_node来创建节点。然后batch命令返回一个指针列表,您可以在其中获取每个节点的neo4j ID。我不确定是否记得我不得不从某些散列结构中提取id,但我相信你会明白这一点。

基本上,我首先创建了一个批处理并将结果存储在一个数组中:

ids = @neo.batch(
    # list of `create_nodes` commands
) #=> returns a list of neo4j ids that you can use further.

为了链接节点,我使用了第二个批处理命令。您可以简单地使用节点的(绝对)neo4j id,而不是使用失败的{id}引用,所以这看起来像

[:create_relationship, "something", id1, id2]

其中id1id2ids提供。

这基本上是一个解决方案,我使用绝对ID而不是相对的....