on
Italy
- Get link
- X
- Other Apps
POST http://localhost:7474/db/data/node ==> 201 Created ==> { ==> "outgoing_relationships" : "http://localhost:7474/db/data/node/26/relationships/out", ==> "data" : { ==> }, ==> "traverse" : "http://localhost:7474/db/data/node/26/traverse/{returnType}", ==> "all_typed_relationships" : "http://localhost:7474/db/data/node/26/relationships/all/{-list|&|types}", ==> "property" : "http://localhost:7474/db/data/node/26/properties/{key}", ==> "self" : "http://localhost:7474/db/data/node/26", ==> "properties" : "http://localhost:7474/db/data/node/26/properties", ==> "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/26/relationships/out/{-list|&|types}", ==> "incoming_relationships" : "http://localhost:7474/db/data/node/26/relationships/in", ==> "extensions" : { ==> }, ==> "create_relationship" : "http://localhost:7474/db/data/node/26/relationships", ==> "paged_traverse" : "http://localhost:7474/db/data/node/26/paged/traverse/{returnType}{?pageSize,leaseTime}", ==> "all_relationships" : "http://localhost:7474/db/data/node/26/relationships/all", ==> "incoming_typed_relationships" : "http://localhost:7474/db/data/node/26/relationships/in/{-list|&|types}" ==> }The above command creates an empty node, with no properties, except for a reference id to itself. In the above example the node id is 26. The REST API uses the HTTP protocol; the "201 Created" message you see is a HTTP status code.
POST http://localhost:7474/db/data/node {"name":"Archie"} ==> 201 Created ==> { ==> "outgoing_relationships" : "http://localhost:7474/db/data/node/27/relationships/out", ==> "data" : { ==> "name" : "Archie" ==> }, ==> "traverse" : "http://localhost:7474/db/data/node/27/traverse/{returnType}", ==> "all_typed_relationships" : "http://localhost:7474/db/data/node/27/relationships/all/{-list|&|types}", ==> "property" : "http://localhost:7474/db/data/node/27/properties/{key}", ==> "self" : "http://localhost:7474/db/data/node/27", ==> "properties" : "http://localhost:7474/db/data/node/27/properties", ==> "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/27/relationships/out/{-list|&|types}", ==> "incoming_relationships" : "http://localhost:7474/db/data/node/27/relationships/in", ==> "extensions" : { ==> }, ==> "create_relationship" : "http://localhost:7474/db/data/node/27/relationships", ==> "paged_traverse" : "http://localhost:7474/db/data/node/27/paged/traverse/{returnType}{?pageSize,leaseTime}", ==> "all_relationships" : "http://localhost:7474/db/data/node/27/relationships/all", ==> "incoming_typed_relationships" : "http://localhost:7474/db/data/node/27/relationships/in/{-list|&|types}" ==> }Make sure the JSON object adheres to the JSON specification, else you will encounter an invalid JSON data error message. For example
{name:"Archie"}
is invalid because the property should also be quoted according to the JSON specs. Also according to the JSON specs, you should use the double quote character (") for quoting strings, and not the single quote (').GET http://localhost:7474/db/data/node/27 ==> 200 OK ==> { ==> "outgoing_relationships" : "http://localhost:7474/db/data/node/27/relationships/out", ==> "data" : { ==> "name" : "Archie" ==> }, ==> "traverse" : "http://localhost:7474/db/data/node/27/traverse/{returnType}", ==> "all_typed_relationships" : "http://localhost:7474/db/data/node/27/relationships/all/{-list|&|types}", ==> "property" : "http://localhost:7474/db/data/node/27/properties/{key}", ==> "self" : "http://localhost:7474/db/data/node/27", ==> "properties" : "http://localhost:7474/db/data/node/27/properties", ==> "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/27/relationships/out/{-list|&|types}", ==> "incoming_relationships" : "http://localhost:7474/db/data/node/27/relationships/in", ==> "extensions" : { ==> }, ==> "create_relationship" : "http://localhost:7474/db/data/node/27/relationships", ==> "paged_traverse" : "http://localhost:7474/db/data/node/27/paged/traverse/{returnType}{?pageSize,leaseTime}", ==> "all_relationships" : "http://localhost:7474/db/data/node/27/relationships/all", ==> "incoming_typed_relationships" : "http://localhost:7474/db/data/node/27/relationships/in/{-list|&|types}" ==> }Trying to get a none existent node will result in HTTP 404 error:
GET http://localhost:7474/db/data/node/10303371 ==> 404 Not Found ==> { ==> "message" : "Cannot find node with id [10303371] in database.", ==> "exception" : "org.neo4j.server.rest.web.NodeNotFoundException: Cannot find node with id [10303371] in database.", ==> "stacktrace" : [ "org.neo4j.server.rest.web.DatabaseActions.node(DatabaseActions.java:112)", "org.neo4j.server.rest.web.DatabaseActions.getNode(DatabaseActions.java:223)", "org.neo4j.server.rest.web.RestfulGraphDatabase.getNode(RestfulGraphDatabase.java:202)", "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)", "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)", "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)", "java.lang.reflect.Method.invoke(Method.java:597)", "com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)", "com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)", "com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)", "com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)", "com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)", "com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)", "com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)", "com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)", "com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)", "com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)", "com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)", "com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)", "com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)", "com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)", "com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)", "javax.servlet.http.HttpServlet.service(HttpServlet.java:820)", "org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)", "org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)", "org.neo4j.server.web.LimitRequestTimeFilter.doFilter(LimitRequestTimeFilter.java:64)", "org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)", "org.neo4j.server.statistic.StatisticFilter.doFilter(StatisticFilter.java:62)", "org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)", "org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)", "org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)", "org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)", "org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)", "org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)", "org.mortbay.jetty.Server.handle(Server.java:326)", "org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)", "org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)", "org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)", "org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)", "org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)", "org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)", "org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)" ] ==> }To delete a node
DELETE http://localhost:7474/db/data/node/26 ==> 204 No ContentNote: nodes with relationships cannot be deleted. To demonstrate that, let's create another node:
POST http://localhost:7474/db/data/node {"name":"Veronica"}and create a relationship:
POST http://localhost:7474/db/data/node/27/relationships {"to" : "http://localhost:7474/db/data/node/28", "type" : "LOVES"} ==> 201 Created ==> { ==> "start" : "http://localhost:7474/db/data/node/27", ==> "data" : { ==> }, ==> "self" : "http://localhost:7474/db/data/relationship/1", ==> "property" : "http://localhost:7474/db/data/relationship/1/properties/{key}", ==> "properties" : "http://localhost:7474/db/data/relationship/1/properties", ==> "type" : "LOVES", ==> "extensions" : { ==> }, ==> "end" : "http://localhost:7474/db/data/node/28" ==> }Note that each relationship we create has its own id too. The relationship id is required for referring, updating and deleting the relationship. Now that we have created an outgoing relationship between nodes 27 and 28, let's try deleting node 27:
DELETE http://localhost:7474/db/data/node/27 ==> 409 Conflict ==> { ==> "message" : "The node with id 27 cannot be deleted. Check that the node is orphaned before deletion.", ==> "exception" : "org.neo4j.server.rest.web.OperationFailureException: The node with id 27 cannot be deleted. Check that the node is orphaned before deletion.", ==> "stacktrace" : [ "org.neo4j.server.rest.web.DatabaseActions.deleteNode(DatabaseActions.java:244)", "org.neo4j.server.rest.web.RestfulGraphDatabase.deleteNode(RestfulGraphDatabase.java:216)", "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)", "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)", "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)", "java.lang.reflect.Method.invoke(Method.java:597)", "com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)", "com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)", "com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)", "com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)", "com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)", "com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)", "com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)", "com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)", "com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)", "com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)", "com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)", "com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)", "com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)", "com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)", "com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)", "javax.servlet.http.HttpServlet.service(HttpServlet.java:820)", "org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)", "org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)", "org.neo4j.server.web.LimitRequestTimeFilter.doFilter(LimitRequestTimeFilter.java:64)", "org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)", "org.neo4j.server.statistic.StatisticFilter.doFilter(StatisticFilter.java:62)", "org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)", "org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)", "org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)", "org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)", "org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)", "org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)", "org.mortbay.jetty.Server.handle(Server.java:326)", "org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)", "org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)", "org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)", "org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)", "org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)", "org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)", "org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)" ] ==> }There it is. It failed to delete. We need to delete all the relationships on node 27 before we can delete it. Here is how we delete the relationship we created:
DELETE http://localhost:7474/db/data/relationship/1 ==> 204 No ContentWith the relationship deleted, let's try deleting the node once more:
DELETE http://localhost:7474/db/data/node/27 ==> 204 No ContentThis time it's gone. Deleted.
POST http://localhost:7474/db/data/node {"name":"Archie"} ==> 201 Created ==> { ==> "outgoing_relationships" : "http://localhost:7474/db/data/node/29/relationships/out", ==> "data" : { ==> "name" : "Archie" ==> }, ==> "traverse" : "http://localhost:7474/db/data/node/29/traverse/{returnType}", ==> "all_typed_relationships" : "http://localhost:7474/db/data/node/29/relationships/all/{-list|&|types}", ==> "property" : "http://localhost:7474/db/data/node/29/properties/{key}", ==> "self" : "http://localhost:7474/db/data/node/29", ==> "properties" : "http://localhost:7474/db/data/node/29/properties", ==> "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/29/relationships/out/{-list|&|types}", ==> "incoming_relationships" : "http://localhost:7474/db/data/node/29/relationships/in", ==> "extensions" : { ==> }, ==> "create_relationship" : "http://localhost:7474/db/data/node/29/relationships", ==> "paged_traverse" : "http://localhost:7474/db/data/node/29/paged/traverse/{returnType}{?pageSize,leaseTime}", ==> "all_relationships" : "http://localhost:7474/db/data/node/29/relationships/all", ==> "incoming_typed_relationships" : "http://localhost:7474/db/data/node/29/relationships/in/{-list|&|types}" ==> }From here on, node id 29 is Archie.
PUT http://localhost:7474/db/data/node/29/properties/age 17 ==> 204 No ContentDon't be alarmed by "204 No Content", it means the operation was successful, but the server did not return any content. Anyway, let's confirm that the property is actually created.
GET http://localhost:7474/db/data/node/29/properties ==> 200 OK ==> { ==> "age" : 17, ==> "name" : "Archie" ==> }There it is, node 29 now has a new property called age with the value of 17.
PUT http://localhost:7474/db/data/node/28/properties {"name":"Veronica", "age":17, "hobbies":["shopping", "boys"]}Not that a property value cannot be
null
or a JSON object. These two commands will fail:PUT http://localhost:7474/db/data/node/28/properties {"name":"Veronica", "age":17, "hobbies":null}
PUT http://localhost:7474/db/data/node/28/properties {"name":"Veronica", "age":17, "hobbies":{"primary":"shopping", "secondary":"boys"}}But these will work:
PUT http://localhost:7474/db/data/node/28/properties {"name":"Veronica", "age":17, "hobbies":""}"" is an empty string, but it is not
null
.PUT http://localhost:7474/db/data/node/28/properties {"name":"Veronica", "age":17, "hobbies":"{\"primary\":\"shopping\", \"secondary\":\"boys\"}"}This time the hobbies JSON objects has been stringified, making it a valid Neo4j property value.
PUT http://localhost:7474/db/data/node/29/properties/name "Archibald" ==> 204 No ContentConfirm the edit:
GET http://localhost:7474/db/data/node/29/properties/ ==> 200 OK ==> { ==> "age" : 17, ==> "name" : "Archibald" ==> }Note how we quoted "Archibald". Any string parameter needs to be quoted, numbers need not be.
PUT http://localhost:7474/db/data/node/29/properties {"name":"Archie"}
GET http://localhost:7474/db/data/node/29/properties/ ==> 200 OK ==> { ==> "name" : "Archie" ==> }We have lost the age property! This method assumes that the accompanying JSON data is the new set of properties for the node. Since we excluded the age property, we lost it. For updating individual properties, use the first method.
DELETE http://localhost:7474/db/data/node/28/properties/hobbiesTo delete the whole set of properties for a node:
DELETE http://localhost:7474/db/data/node/28/propertiesEven though the examples were for nodes, the commands and parameters apply to working with relationship properties too. For example to add a property to a relationship:
PUT http://localhost:7474/db/data/relationship/10/properties/year 1941
GET http://localhost:7474/db/data/relationship/types ==> 200 OK ==> ["KNOWS","RELATED_TO","HATES","Hates","LOVES"]Note that relationship types are case-insentitive, and persists forever, meaning once you create a relationship type, you can't delete it. That sounds strange, but that's the way it is as of this writing.
PUT http://localhost:7474/db/data/node/1/properties {"name":"Archie"} PUT http://localhost:7474/db/data/node/2/properties {"name":"Betty"} PUT http://localhost:7474/db/data/node/3/properties {"name":"Veronica"} PUT http://localhost:7474/db/data/node/4/properties {"name":"Jughead"} PUT http://localhost:7474/db/data/node/5/properties {"name":"Reggie"} PUT http://localhost:7474/db/data/node/6/properties {"name":"Ethel"} PUT http://localhost:7474/db/data/node/7/properties {"name":"Food"}To create a relationship
POST http://localhost:7474/db/data/node/1/relationships {"to" : "http://localhost:7474/db/data/node/2", "type" : "LOVES"} ==> 201 Created ==> { ==> "start" : "http://localhost:7474/db/data/node/1", ==> "data" : { ==> }, ==> "self" : "http://localhost:7474/db/data/relationship/3", ==> "property" : "http://localhost:7474/db/data/relationship/3/properties/{key}", ==> "properties" : "http://localhost:7474/db/data/relationship/3/properties", ==> "type" : "LOVES", ==> "extensions" : { ==> }, ==> "end" : "http://localhost:7474/db/data/node/2" ==> }An id of 3 is assigned to the relationship we just created.
POST http://localhost:7474/db/data/node/1/relationships {"to" : "http://localhost:7474/db/data/node/3", "type" : "LOVES"} POST http://localhost:7474/db/data/node/2/relationships {"to" : "http://localhost:7474/db/data/node/1", "type" : "LOVES"} POST http://localhost:7474/db/data/node/3/relationships {"to" : "http://localhost:7474/db/data/node/1", "type" : "LOVES"} POST http://localhost:7474/db/data/node/3/relationships {"to" : "http://localhost:7474/db/data/node/5", "type" : "LOVES"} POST http://localhost:7474/db/data/node/4/relationships {"to" : "http://localhost:7474/db/data/node/7", "type" : "LOVES"} POST http://localhost:7474/db/data/node/5/relationships {"to" : "http://localhost:7474/db/data/node/3", "type" : "LOVES"} POST http://localhost:7474/db/data/node/6/relationships {"to" : "http://localhost:7474/db/data/node/4", "type" : "LOVES"} POST http://localhost:7474/db/data/node/1/relationships {"to" : "http://localhost:7474/db/data/node/4", "type" : "FRIENDS"} POST http://localhost:7474/db/data/node/4/relationships {"to" : "http://localhost:7474/db/data/node/1", "type" : "FRIENDS"} POST http://localhost:7474/db/data/node/2/relationships {"to" : "http://localhost:7474/db/data/node/3", "type" : "FRIENDS"} POST http://localhost:7474/db/data/node/3/relationships {"to" : "http://localhost:7474/db/data/node/2", "type" : "FRIENDS"}Note that relationships are not "unique". By that I mean, if you were to run this command again:
POST http://localhost:7474/db/data/node/1/relationships {"to" : "http://localhost:7474/db/data/node/2", "type" : "LOVES"}
GET http://localhost:7474/db/data/relationship/3 ==> 200 OK ==> { ==> "start" : "http://localhost:7474/db/data/node/1", ==> "data" : { ==> "intensity" : "low", ==> "since" : "day one" ==> }, ==> "self" : "http://localhost:7474/db/data/relationship/3", ==> "property" : "http://localhost:7474/db/data/relationship/3/properties/{key}", ==> "properties" : "http://localhost:7474/db/data/relationship/3/properties", ==> "type" : "LOVES", ==> "extensions" : { ==> }, ==> "end" : "http://localhost:7474/db/data/node/2" ==> }To add properties to a relationship
PUT http://localhost:7474/db/data/relationship/3/properties/intensity "low"If you want to add multiple properties:
PUT http://localhost:7474/db/data/relationship/3/properties {"intensity":"low", "since":"day one"}You can test the results of the two commands above this way:
GET http://localhost:7474/db/data/relationship/3To create a relationship with properties
POST http://localhost:7474/db/data/node/1/relationships {"to" : "http://localhost:7474/db/data/node/5", "type" : "RIVALS", "data":{"intensity":"hight", "since":"day one"}}Creating, reading, updating, and deleting relationship properties is similar to node properties, refer to the operations on node properties in the last examples.
DELETE http://localhost:7474/db/data/relationship/3To see all the relationships on a node
GET http://localhost:7474/db/data/node/1/relationships/allTo see all the incoming relationships to a node
GET http://localhost:7474/db/data/node/3/relationships/inTo see all the outgoing relationships from a node
GET http://localhost:7474/db/data/node/4/relationships/outTo see typed relationships
GET http://localhost:7474/db/data/node/1/relationships/all/LOVES
GET http://localhost:7474/db/data/node/4/relationships/out/LOVES
GET http://localhost:7474/db/data/node/4/relationships/in/LOVES
GET http://localhost:7474/db/data/node/4/relationships/out/LOVES&FRIENDS
Comments
Post a Comment