【问题标题】:neo4j : batch import relationsneo4j:批量导入关系
【发布时间】:2013-09-11 17:19:44
【问题描述】:

我无法在图表中导入关系。

假设我有数百个已创建的唯一索引用户。然后我想创建大约 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,但是我根本不明白在创建关系时我应该如何使用索引。

任何帮助将不胜感激,在此先感谢

【问题讨论】:

  • 您找到解决方案了吗?
  • 我实际上使用了一些解决方法,我会发布一个答案来解释它。

标签: ruby neo4j neography


【解决方案1】:

由于这个问题已被赞成,我发布了我从那时起找到的解决方法:

正如问题中提到的,可以批处理 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.

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

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

其中id1id2ids 给出。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多