py2neo在WriteBatch中使用get或create [英] py2neo using get or create in WriteBatch

查看:124
本文介绍了py2neo在WriteBatch中使用get或create的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Neo4j和py2neo.我正在尝试使用py2neo中的批处理功能来加载neo4j数据库的批量数据.

在基本级别上,我想使用py2neo中的WriteBatch创建两个节点(如果它们已经存在,则获取它们)并使用默认权重在它们之间创建一个关系(如果关系已经存在,则增加权重). /p>

文档仅说明如何创建两个新节点并在它们之间形成关系.我正在研究以下方面的东西:

from py2neo import neo4j, cypher
graphdb = neo4j.GraphDatabaseService()
topic_index = graphdb.get_or_create_index(neo4j.Node, "node_index")
batch = neo4j.WriteBatch(graphdb)
batch.get_or_create_indexed_node('node_index', 'name', 'Alice', {'name': 'Alice'})
batch.get_or_create_indexed_node('node_index', 'name', 'Bob', {'name': 'Bob'})
batch.get_or_create_indexed_relationship('rel_index', 'type', 'KNOWS', 0, 'KNOWS', 1, {})
results = batch.submit()

但是,此操作失败并显示以下错误:

SystemError:{u'stacktrace':[u'org.neo4j.server.rest.batch.NonStreamingBatchOperations.invoke(NonStreamingBatchOperations.java:63)',u'org.neo4j.server.rest.batch.BatchOperations. performRequest(BatchOperations.java:178)',u'org.neo4j.server.rest.batch.BatchOperations.parseAndPerform(BatchOperations.java:149)',u'org.neo4j.server.rest.batch.NonStreamingBatchOperations.performBatchJobs( NonStreamingBatchOperations.java:48)',u'org.neo4j.server.rest.web.BatchOperationService.batchProcess(BatchOperationService.java:117)',u'org.neo4j.server.rest.web.BatchOperationService.performBatchOperations(BatchOperationService. java:71)',u'java.lang.reflect.Method.invoke(Method.java:616)'],u'message':u'{\ n"message":对于输入字符串:\" {0 } \",\ n"exception":"BadInputException",\ n"stacktrace":["org.neo4j.server.rest.web.RestfulGraphDatabase.extractNodeId(RestfulGraphDatabase.java:162)","org.neo4j. server.rest.web.RestfulGraphDatabase.extractNodeIdOrNull(RestfulGraphDatabase.java:151), "org.neo4j.server.rest.web.RestfulGraphDatabase.addToRelationshipIndex(RestfulGraphDatabase.java:813)","java.lang.reflect.Method.invoke(Method.java:616)","org.neo4j.server.web .Jetty6WebServer.invokeDirectly(Jetty6WebServer.java:273)," org.neo4j.server.rest.batch.NonStreamingBatchOperations.invoke(NonStreamingBatchOperations.java:55)," org.neo4j.server.rest.batch.BatchOperations.performRequest (BatchOperations.java:178)"、"org.neo4j.server.rest.batch.BatchOperations.parseAndPerform(BatchOperations.java:149)"、"org.neo4j.server.rest.batch.NonStreamingBatchOperations.performBatchJobs(NonStreamingBatchOperations.java :48)," org.neo4j.server.rest.web.BatchOperationService.batchProcess(BatchOperationService.java:117)," org.neo4j.server.rest.web.BatchOperationService.performBatchOperations(BatchOperationService.java:71)","java.lang.reflect.Method.invoke(Method.java:616)"]​​ \ n}',u'exception':u'BatchOperationFailedException'}

基于错误的输入异常",我很确定这是start_node和end_node参数的问题.基本上,我想参考前面的get_or_create节点和关系.您如何在批处理中引用这些内容?

更新:经过大量实验,我缩小了重现此错误的可能方法-如果get_or_create中的任一节点已存在于图中,则批处理操作将失败,并在对应于索引的索引上出现错误的输入错误现有节点.我还更新了代码以准确显示我的运行.第一次成功运行此代码(两个节点均未在图中显示).再次运行失败.

py2neo版本:1.5

neo4j版本:1.8.2

解决方案

使用您的确切代码,我无法重现py2neo 1.5和Neo4j 2.0.0-M03的问题.您能说明您正在使用哪个版本的py2neo和Neo4j吗?

I am just getting started with Neo4j and py2neo. I am experimenting with the batch feature available in py2neo for bulk data loading of a neo4j database.

At a basic level, I want to create two nodes (or get them if they already exist) and create a relationship between them with a default weight (or increment the weight if the relationship already exists) using WriteBatch in py2neo.

The documentation explains only how to create two new nodes and form a relationship between them. I am looking into something along the lines of:

from py2neo import neo4j, cypher
graphdb = neo4j.GraphDatabaseService()
topic_index = graphdb.get_or_create_index(neo4j.Node, "node_index")
batch = neo4j.WriteBatch(graphdb)
batch.get_or_create_indexed_node('node_index', 'name', 'Alice', {'name': 'Alice'})
batch.get_or_create_indexed_node('node_index', 'name', 'Bob', {'name': 'Bob'})
batch.get_or_create_indexed_relationship('rel_index', 'type', 'KNOWS', 0, 'KNOWS', 1, {})
results = batch.submit()

However, this fails with the error:

SystemError: {u'stacktrace': [u'org.neo4j.server.rest.batch.NonStreamingBatchOperations.invoke(NonStreamingBatchOperations.java:63)', u'org.neo4j.server.rest.batch.BatchOperations.performRequest(BatchOperations.java:178)', u'org.neo4j.server.rest.batch.BatchOperations.parseAndPerform(BatchOperations.java:149)', u'org.neo4j.server.rest.batch.NonStreamingBatchOperations.performBatchJobs(NonStreamingBatchOperations.java:48)', u'org.neo4j.server.rest.web.BatchOperationService.batchProcess(BatchOperationService.java:117)', u'org.neo4j.server.rest.web.BatchOperationService.performBatchOperations(BatchOperationService.java:71)', u'java.lang.reflect.Method.invoke(Method.java:616)'], u'message': u'{\n "message" : "For input string: \"{0}\"",\n "exception" : "BadInputException",\n "stacktrace" : [ "org.neo4j.server.rest.web.RestfulGraphDatabase.extractNodeId(RestfulGraphDatabase.java:162)", "org.neo4j.server.rest.web.RestfulGraphDatabase.extractNodeIdOrNull(RestfulGraphDatabase.java:151)", "org.neo4j.server.rest.web.RestfulGraphDatabase.addToRelationshipIndex(RestfulGraphDatabase.java:813)", "java.lang.reflect.Method.invoke(Method.java:616)", "org.neo4j.server.web.Jetty6WebServer.invokeDirectly(Jetty6WebServer.java:273)", "org.neo4j.server.rest.batch.NonStreamingBatchOperations.invoke(NonStreamingBatchOperations.java:55)", "org.neo4j.server.rest.batch.BatchOperations.performRequest(BatchOperations.java:178)", "org.neo4j.server.rest.batch.BatchOperations.parseAndPerform(BatchOperations.java:149)", "org.neo4j.server.rest.batch.NonStreamingBatchOperations.performBatchJobs(NonStreamingBatchOperations.java:48)", "org.neo4j.server.rest.web.BatchOperationService.batchProcess(BatchOperationService.java:117)", "org.neo4j.server.rest.web.BatchOperationService.performBatchOperations(BatchOperationService.java:71)", "java.lang.reflect.Method.invoke(Method.java:616)" ]\n}', u'exception': u'BatchOperationFailedException'}

Based on the 'Bad Input Exception', I am pretty sure that is a problem with the start_node and end_node arguments. Basically, I want to refer to the previous get_or_create nodes and relationships. How do you refer to these in the batch?

UPDATE: after much experimentation, I have narrowed in on a possible method to reproduce this error - if either of the nodes in the get_or_create already exist in the graph, the batch operation fails with a bad input error on the index corresponding to the existing node. I have also updated the code to show exactly what I ran. Running this code for the first time succeeds (with both nodes NOT present in the graph). Running it again fails.

py2neo version: 1.5

neo4j version: 1.8.2

解决方案

Using your exact code, I'm unable to reproduce the problem with py2neo 1.5 and Neo4j 2.0.0-M03. Could you clarify which versions of py2neo and Neo4j you are using?

这篇关于py2neo在WriteBatch中使用get或create的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆