Mercurial > hg > Members > nobuyasu > TestNeo4j
view src/begin/MakeNode.java @ 12:bf7863a55cd6 draft
add wikigraph.Neo4jTest.java
author | one |
---|---|
date | Thu, 23 Aug 2012 03:40:48 +0900 |
parents | d8d0855bcdfd |
children |
line wrap: on
line source
package begin; import java.net.URI; import javax.ws.rs.core.MediaType; import org.json.JSONException; import org.json.JSONObject; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientHandlerException; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.UniformInterfaceException; import com.sun.jersey.api.client.WebResource; public class MakeNode { final static String SERVER_ROOT_URI = "http://localhost:7474"; final static String nodeEntryPointUri = "http://localhost:7474/db/data/node"; final static String nodeIndexEntryPointUri = "http://localhost:7474/db/data/index/node"; public static URI createNode() { WebResource resource = Client.create().resource(nodeEntryPointUri); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class); System.out.println(String.format( "POST to [%s], status code [%d], location header [%s]", nodeEntryPointUri, response.getStatus(), response.getLocation() .toString())); return response.getLocation(); } public static ClientResponse getNode(String nodeUri) { WebResource resource = Client.create().resource(nodeUri); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); System.out.println(String.format("GET on [%s], status code [%d]",nodeUri, response.getStatus() )); return response; } public static void setProperty(String uri,String propertyName,String propertyValue) { String propertyUri = uri + "/properties/" + propertyName; WebResource resource = Client.create().resource(propertyUri); // http://localhost:7474/db/data/node/(node_id)/properties/(property_name) ClientResponse response = resource.accept(MediaType.APPLICATION_JSON) .type(MediaType.APPLICATION_JSON) .entity(toJsonStringLiteral(propertyValue)) .put(ClientResponse.class); System.out.println(String.format("PUT to [%s], status code [%d]",propertyUri, response.getStatus())); } public static void addProperty(URI nodeUri, String propertyName, String propertyValue) { setProperty(nodeUri.toString(), propertyName, propertyValue); } private static String toJsonStringLiteral(String str) { return "\"" + str + "\""; } public static void createRelationship(String uri1, String uri2, String typeValue) throws JSONException { String relationshipUri = uri1 + "/relationships"; WebResource resource = Client.create().resource(relationshipUri); JSONObject obj = new JSONObject(); obj.put("to",uri2); obj.put("type", typeValue); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON) .entity(obj.toString()) .post(ClientResponse.class); System.out.println(String.format("POST to [%s], status code [%d]",relationshipUri , response.getStatus())); } public static void deleteNode(String uri) { WebResource resource = Client.create().resource(uri); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).delete(ClientResponse.class); System.out.println(String.format( "DELETE to [%s], status code [%d]", uri, response.getStatus())); } public static URI createIndexNode(String indexName) throws JSONException { WebResource resource = Client.create().resource(nodeIndexEntryPointUri); JSONObject obj = new JSONObject(); obj.put("name",indexName); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON) .type(MediaType.APPLICATION_JSON) .entity(obj.toString()) .post(ClientResponse.class); System.out.println(String.format( "POST to [%s], status code [%d], location header [%s]", nodeIndexEntryPointUri, response.getStatus(), response.getLocation() .toString())); return response.getLocation(); } public static URI addNodeToIndex(String indexUri, String value, String uri, String key) throws JSONException { WebResource resource = Client.create().resource(indexUri); JSONObject obj = new JSONObject(); obj.put("value", value); obj.put("uri",uri); obj.put("key",key); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON) .type(MediaType.APPLICATION_JSON) .entity(obj.toString()) .post(ClientResponse.class); System.out.println(String.format( "POST to [%s], status code [%d], location header [%s]", nodeIndexEntryPointUri, response.getStatus(), response.getLocation() .toString())); return response.getLocation(); } public static void main(String[] args) throws ClientHandlerException, UniformInterfaceException, JSONException { final String uri1 = "http://localhost:7474/db/data/node/1"; final String uri2 = "http://localhost:7474/db/data/node/2"; final String indexUri = "http://localhost:7474/db/data/index/node/hogehoge"; addNodeToIndex(indexUri, "test_value", uri1, "test_key"); ClientResponse response = getNode(uri1); // System.out.println(response.getEntity(String.class)); JSONObject obj = new JSONObject(response.getEntity(String.class)); System.out.println(obj.toString()); /* getNode(uri2); createRelationship(uri1, uri2, "RELATED_TO"); */ /* URI firstNode = createNode(); addProperty(firstNode, "name", "Joe Strummer"); */ /* setProperty(uri1, "name", "Jason Kidd"); setProperty(uri2, "name", "Magic"); */ } public static void testCreateNodeWithProperty() { WebResource resource = Client.create().resource(nodeEntryPointUri); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON) .type(MediaType.APPLICATION_JSON).entity("{ \"foo\" : \"hogehoge\" }"/* Empty Node */) .post(ClientResponse.class); System.out.println(String.format( "POST to [%s], status code [%d], location header [%s]", nodeEntryPointUri, response.getStatus(), response.getLocation() .toString())); } }