java连接Neo4j服务器

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

// START SNIPPET: insideAddMetaToProp private static void addMetadataToProperty( URI relationshipUri, String name, String value ) throws URISyntaxException { URI propertyUri = new URI( relationshipUri.toString() + “/properties” ); String entity = toJsonNameValuePairCollection( name, value ); WebResource resource = Client.create() .resource( propertyUri ); ClientResponse response = resource.accept( MediaType.APPLICATION_JSON ) .type( MediaType.APPLICATION_JSON ) .entity( entity ) .put( ClientResponse.class ); System.out.println( String.format( “PUT [%s] to [%s], status code [%d]”, entity, propertyUri, response.getStatus() ) ); response.close(); } // END SNIPPET: insideAddMetaToProp private static String toJsonNameValuePairCollection( String name, String value ) { return String.format( “{ \”%s\” : \”%s\” }”, name, value ); } private static URI createNode() { // START SNIPPET: createNode final String nodeEntryPointUri = SERVER_ROOT_URI + “node”; // http://localhost:7474/db/data/node WebResource resource = Client.create() .resource( nodeEntryPointUri ); // POST {} to the node entry point URI ClientResponse response = resource.accept( MediaType.APPLICATION_JSON ) .type( MediaType.APPLICATION_JSON ) .entity( “{}” ) .post( ClientResponse.class ); final URI location = response.getLocation(); System.out.println( String.format( “POST to [%s], status code [%d], location header [%s]”, nodeEntryPointUri, response.getStatus(), location.toString() ) ); response.close(); return location; // END SNIPPET: createNode } // START SNIPPET: insideAddRel private static URI addRelationship( URI startNode, URI endNode, String relationshipType, String jsonAttributes ) throws URISyntaxException { URI fromUri = new URI( startNode.toString() + “/relationships” ); String relationshipJson = generateJsonRelationship( endNode, relationshipType, jsonAttributes ); WebResource resource = Client.create() .resource( fromUri ); // POST JSON to the relationships URI ClientResponse response = resource.accept( MediaType.APPLICATION_JSON ) .type( MediaType.APPLICATION_JSON ) .entity( relationshipJson ) .post( ClientResponse.class ); final URI location = response.getLocation(); System.out.println( String.format( “POST to [%s], status code [%d], location header [%s]”, fromUri, response.getStatus(), location.toString() ) ); response.close(); return location; } // END SNIPPET: insideAddRel private static String generateJsonRelationship( URI endNode, String relationshipType, String… jsonAttributes ) { StringBuilder sb = new StringBuilder(); sb.append( “{ \”to\” : \”” ); sb.append( endNode.toString() ); sb.append( “\”, ” ); sb.append( “\”type\” : \”” ); sb.append( relationshipType ); if ( jsonAttributes == null || jsonAttributes.length < 1 ) { sb.append( “\”” ); } else { sb.append( “\”, \”data\” : ” ); for ( int i = 0; i < jsonAttributes.length; i++ ) { sb.append( jsonAttributes[i] ); if ( i < jsonAttributes.length – 1 ) { // Miss off the final comma sb.append( “, ” ); } } } sb.append( ” }” ); return sb.toString(); } private static void addProperty( URI nodeUri, String propertyName, String propertyValue ) { // START SNIPPET: addProp String propertyUri = nodeUri.toString() + “/properties/” + propertyName; // http://localhost:7474/db/data/node/{node_id}/properties/{property_name} WebResource resource = Client.create() .resource( propertyUri ); ClientResponse response = resource.accept( MediaType.APPLICATION_JSON ) .type( MediaType.APPLICATION_JSON ) .entity( “\”” + propertyValue + “\”” ) .put( ClientResponse.class ); System.out.println( String.format( “PUT to [%s], status code [%d]”, propertyUri, response.getStatus() ) ); response.close(); // END SNIPPET: addProp } private static void checkDatabaseIsRunning() { // START SNIPPET: checkServer WebResource resource = Client.create() .resource( SERVER_ROOT_URI ); ClientResponse response = resource.get( ClientResponse.class ); System.out.println( String.format( “GET on [%s], status code [%d]”, SERVER_ROOT_URI, response.getStatus() ) ); response.close(); // END SNIPPET: checkServer } } [java] import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class TraversalDescription { public static final String DEPTH_FIRST = “depth first”; public static final String NODE = “node”; public static final String ALL = “all”; private String uniqueness = NODE; private int maxDepth = 1; private String returnFilter = ALL; private String order = DEPTH_FIRST; private List<Relationship> relationships = new ArrayList<Relationship>(); public void setOrder( String order ) { this.order = order; } public void setUniqueness( String uniqueness ) { this.uniqueness = uniqueness; } public void setMaxDepth( int maxDepth ) { this.maxDepth = maxDepth; } public void setReturnFilter( String returnFilter ) { this.returnFilter = returnFilter; } public void setRelationships( Relationship… relationships ) { this.relationships = Arrays.asList( relationships ); } public String toJson() { StringBuilder sb = new StringBuilder(); sb.append( “{ ” ); sb.append( ” \”order\” : \”” + order + “\”” ); sb.append( “, ” ); sb.append( ” \”uniqueness\” : \”” + uniqueness + “\”” ); sb.append( “, ” ); if ( relationships.size() > 0 ) { sb.append( “\”relationships\” : [” ); for ( int i = 0; i < relationships.size(); i++ ) { sb.append( relationships.get( i ) .toJsonCollection() ); if ( i < relationships.size() – 1 ) { // Miss off the final comma sb.append( “, ” ); } } sb.append( “], ” ); } www.2cto.com sb.append( “\”return filter\” : { ” ); sb.append( “\”language\” : \”builtin\”, ” ); sb.append( “\”name\” : \”” ); sb.append( returnFilter ); sb.append( “\” }, ” ); sb.append( “\”max depth\” : ” ); sb.append( maxDepth ); sb.append( ” }” ); return sb.toString(); } }

[1][2]

教育人的诗句或名言警句,激励人在逆境中脱颖而出的话

java连接Neo4j服务器

相关文章:

你感兴趣的文章:

标签云: