问题
I know this is probably asked a hundred times and believe me, I read soooo much on the net already. But still, I am absolutely unapt to do this, so I'm in dire need of help here.
What I want to do, is to create a node with label and properties in the neo4j db using cypher and the transactional endpoint - with the properties from a json.
This is my code so far:
/**
*
* @description creates a node in the graph
* @param json object
* @param label
*
*/
private String createNodeObjectTransactional(JSONObject nodeObject, GraphNodeTypes label){
String nodeLocation=null;
try{
dbServerUrl = "http://localhost:7474"
transactionUrl = dbServerUrl + "/db/data" + "/transaction";
String finalUrl = transactionUrl;
String payload = "{\"statements\": [ {\"statement\": \"CREATE\" (p:"+ label.toString() +" "+ nodeObject.toString() + ") } ] }";
logger.trace("sending cypher {} to endpoint {}", payload, finalUrl);
WebResource resource = Client.create().resource( finalUrl );
ClientResponse response = resource
.accept( MediaType.APPLICATION_JSON )
.type( MediaType.APPLICATION_JSON )
.entity( payload )
.post( ClientResponse.class );
nodeLocation = response.getLocation().toString();
String responseString = response.getEntity(String.class);
logger.debug("POST to {} returned status code {}, returned data: {}",
finalUrl, response.getStatus(),
responseString);
// first check if the http code was ok
HttpStatusCodes httpStatusCodes = HttpStatusCodes.getHttpStatusCode(response.getStatus());
if (!httpStatusCodes.isOk()){
if (httpStatusCodes == HttpStatusCodes.FORBIDDEN){
logger.error(HttpErrorMessages.getHttpErrorText(httpStatusCodes.getErrorCode()));
} else {
logger.error("Error {} sending data to {}: {} ", response.getStatus(), finalUrl, HttpErrorMessages.getHttpErrorText(httpStatusCodes.getErrorCode()));
}
} else {
// now do the check on json details within the returned JSON object
JSONParser reponseParser = new JSONParser();
Object responseObj = reponseParser.parse(responseString);
JSONObject jsonResponseObj = responseObj instanceof JSONObject ?(JSONObject) responseObj : null;
if(jsonResponseObj == null)
throw new ParseException(0, "returned json object is null");
// this is the location to commit the transaction if node creation was successfull
String commit = (String) jsonResponseObj.get("commit").toString();
// this contains an error object (actually an array) in case the creation was NOT successfull
String error = (String) jsonResponseObj.get("errors").toString();
// doknow what that is
String result = (String) jsonResponseObj.get("results").toString();
final URI location = response.getLocation();
if (error.isEmpty()) {
logger.info("new node created at location {}", location);
logger.trace("returned result json is {}", result.toString());
logger.debug("committing transaction at location {}", commit);
resource = Client.create().resource( commit );
response = resource
.accept( MediaType.APPLICATION_JSON )
.type( MediaType.APPLICATION_JSON )
.post( ClientResponse.class );
logger.debug("COMMIT returned status code {}, returned data: {}",
response.getStatus(),
response.getEntity(String.class));
} else {
logger.error("ERROR :: {} - could not create node at location {}", error.substring(13), location);
logger.trace("returned error json is {}", error.toString());
}
}
response.close();
} catch(Exception e) {
logger.error("EXCEPTION :: failed to create node - {}", e.getMessage());
e.printStackTrace();
}
return nodeLocation;
}
and this is the server's response:
ERROR :: Unable to deserialize request: Unexpected character ('(' (code 40)): was expecting comma to separate OBJECT entries\n at [Source: org.eclipse.jetty.server.HttpConnection$Input@44382113{HttpChannelOverHttp@41038962{r=6,a=DISPATCHED,uri=\/db\/data\/transaction},HttpConnection@4f30a246{FILLING},g=HttpGenerator{s=START},p=HttpParser{s=END,275 of 275}}; line: 1, column: 42]","code":"Neo.ClientError.Request.InvalidFormat"}] - could not create node at location http://localhost:7474/db/data/transaction/121
What really bothers me about this, is that in the examples given on the neo4j site, they DO put in the ( character to encapsulate the label and json object, but the server does not like that character. As stated in the answer:
Unexpected character ('(' (code 40)): was expecting comma to separate OBJECT
I have no idea, what the server means by adding a comma at this location. It is not within the son of the object but rather precedes that.
Here comes the tracelog:
INFO Neo4JPersistence - creating a Twitter node object to store in the graph
DEBUG Neo4JPersistence - creating node transactional
TRACE Neo4JPersistence - sending cypher payload {"statements": [ {"statement": "CREATE" (p:POST {"id":"534621287264817153","subject":"daily....","teaser":"daily...","lang":"de","sn_id":"TW"}) } ] } to endpoint http://localhost:7474/db/data/transaction
DEBUG Neo4JPersistence - POST to http://localhost:7474/db/data/transaction returned status code 201, returned data: {"commit":"http://localhost:7474/db/data/transaction/121/commit","results":[],"transaction":{"expires":"Tue, 18 Nov 2014 08:19:55 +0000"},"errors":[{"code":"Neo.ClientError.Request.InvalidFormat","message":"Unable to deserialize request: Unexpected character ('(' (code 40)): was expecting comma to separate OBJECT entries\n at [Source: org.eclipse.jetty.server.HttpConnection$Input@44382113{HttpChannelOverHttp@41038962{r=6,a=DISPATCHED,uri=/db/data/transaction},HttpConnection@4f30a246{FILLING},g=HttpGenerator{s=START},p=HttpParser{s=END,275 of 275}}; line: 1, column: 42]"}]}
ERROR Neo4JPersistence - ERROR :: Unable to deserialize request: Unexpected character ('(' (code 40)): was expecting comma to separate OBJECT entries\n at [Source: org.eclipse.jetty.server.HttpConnection$Input@44382113{HttpChannelOverHttp@41038962{r=6,a=DISPATCHED,uri=\/db\/data\/transaction},HttpConnection@4f30a246{FILLING},g=HttpGenerator{s=START},p=HttpParser{s=END,275 of 275}}; line: 1, column: 42]","code":"Neo.ClientError.Request.InvalidFormat"}] - could not create node at location http://localhost:7474/db/data/transaction/121
TRACE Neo4JPersistence - returned error json is [{"message":"Unable to deserialize request: Unexpected character ('(' (code 40)): was expecting comma to separate OBJECT entries\n at [Source: org.eclipse.jetty.server.HttpConnection$Input@44382113{HttpChannelOverHttp@41038962{r=6,a=DISPATCHED,uri=\/db\/data\/transaction},HttpConnection@4f30a246{FILLING},g=HttpGenerator{s=START},p=HttpParser{s=END,275 of 275}}; line: 1, column: 42]","code":"Neo.ClientError.Request.InvalidFormat"}]
Please, please please,
can anyone help me?
Thanks in advance,
Chris
回答1:
As the error tells you, you are passing malformed json
... "CREATE" (p:POST ...
Json should be as described here
{
"statements" : [ {
"statement" : "CREATE (n {props}) RETURN n",
"parameters" : {
"props" : {
"name" : "My Node"
}
}
} ]
}
(See docs for details)
In your case:
String payload = "{\"statements\":
[ {\"statement\": \"CREATE (p:"+ label.toString() +" " {props}) \",
\"parameters\":" + nodeObject.toString() + " } ] }";
来源:https://stackoverflow.com/questions/26989613/create-a-node-in-neo4j-graph-db-with-transaction-endpoint