【问题标题】:Retrieving nodes from index with transaction in neo4jrestclient在 neo4jrestclient 中使用事务从索引中检索节点
【发布时间】:2012-07-10 06:07:13
【问题描述】:

我想对已经保存到 neo4j 的一堆节点进行批处理操作,最终在它们之间创建关系。我有类似的东西

with gdb.transaction():
    for s_id, d_id in nodelist:
        sn = nidx['nid'][s_id].single
        dn = nidx['nid'][d_id].single

nidx 是我创建的索引('nid', s_id 是键/值对)。但是,看起来nidx['nid'][s_id] 是一个 TransactionOperationProxy 对象,尽管我打算让它成为一个节点。有什么方法可以将其转换为节点,或者至少使用它来创建sndn 之间的关系(类似于 sn.Follows(dn))?

谢谢。

【问题讨论】:

  • 它看起来像一个错误。让我看看。
  • 我添加了一个 new test 来调试它,但我认为 Neo4 REST API 不支持。解释是您正在请求索引的第一个节点,并且在对它们进行任何操作之前,您需要完成获取节点 URL 的事务,以便使用 REST API 在两者之间创建关系。
  • 为什么要使用索引按ID查找而不是直接按ID获取?
  • @espeed s_idd_id 不是“真正的”neo4j id,我正在从另一个列表中读取它们。如果有一种方法可以将节点保存到具有给定 ID 的数据库中,那会更容易。

标签: python transactions neo4j


【解决方案1】:

您可以使用批处理加载程序执行此操作,但使用 Gremlin 脚本会更容易。

如果您的 nodelist 是节点 ID 对的列表,这里是 Gremlin 脚本来批量加载边缘(未经测试)...

// gremlin.groovy

def batch_load(nodelist, label) {
  g.setMaxBufferSize(0)
  g.startTransaction()
  try {
    for (entry in nodelist) {
      s_id = entry[0]
      d_id = entry[1]
      // if s_id and d_id are actual node IDs, you don't need to use an index...
      sn = g.idx('someindex').get('nid',s_id)[0]
      dn = g.idx('someindex').get('nid',d_id)[0]
      g.addEdge(sn,dn,label)
    }
    g.stopTransaction(TransactionalGraph.Conclusion.SUCCESS)
    return true
  } catch (e) {
    g.stopTransaction(TransactionalGraph.Conclusion.FAILURE)  
    return e
  }
}

下面是在 Bulbs 中执行它的方式——你需要为 neo4jrestclient 修改它...

>>> from bulbs.neoj4server import Graph
>>> g = Graph()
>>> g.scripts.update('/path/to/gremlin.groovy')
>>> script = g.scripts.get('batch_load')
>>> params = dict(nodelist=your_node_list, label="follows")
>>> g.gremlin.execute(script, params)

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 2015-08-20
    • 1970-01-01
    • 2023-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多