java连接neo4j_java连接Neo4j服务器

import java.net.URI;

import java.net.URISyntaxException;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;

import com.sun.jersey.api.client.ClientResponse;

import com.sun.jersey.api.client.WebResource;

public class CreateSimpleGraph

{

private static final String SERVER_ROOT_URI = "http://localhost:7474/db/data/";

public static void main( String[] args ) throws URISyntaxException

{

checkDatabaseIsRunning();

// START SNIPPET: nodesAndProps

URI firstNode = createNode();

addProperty( firstNode, "name", "Joe Strummer" );

URI secondNode = createNode();

addProperty( secondNode, "band", "The Clash" );

// END SNIPPET: nodesAndProps

// START SNIPPET: addRel

URI relationshipUri = addRelationship( firstNode, secondNode, "singer",

"{ \"from\" : \"1976\", \"until\" : \"1986\" }" );

// END SNIPPET: addRel

// START SNIPPET: addMetaToRel

addMetadataToProperty( relationshipUri, "stars", "5" );

// END SNIPPET: addMetaToRel

// START SNIPPET: queryForSingers

findSingersInBands( firstNode );

// END SNIPPET: queryForSingers

}

private static void findSingersInBands( URI startNode )

throws URISyntaxException

{

// START SNIPPET: traversalDesc

// TraversalDescription turns into JSON to send to the Server

TraversalDescription t = new TraversalDescription();

t.setOrder( TraversalDescription.DEPTH_FIRST );

t.setUniqueness( TraversalDescription.NODE );

t.setMaxDepth( 10 );

t.setReturnFilter( TraversalDescription.ALL );

t.setRelationships( new Relationship( "singer", Relationship.OUT ) );

// END SNIPPET: traversalDesc

// START SNIPPET: traverse

URI traverserUri = new URI( startNode.toString() + "/traverse/node" );

WebResource resource = Client.create()

.resource( traverserUri );

String jsonTraverserPayload = t.toJson();

ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )

.type( MediaType.APPLICATION_JSON )

.entity( jsonTraverserPayload )

.post( ClientResponse.class );

System.out.println( String.format(

"POST [%s] to [%s], status code [%d], returned data: "

+ System.getProperty( "line.separator" ) + "%s",

jsonTraverserPayload, traverserUri, response.getStatus(),

response.getEntity( String.class ) ) );

response.close();

// END SNIPPET: traverse

}

// 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 

{

sb.append( "\"" );

}

else

{

sb.append( "\", \"data\" : " );

for ( int i = 0; i 

{

sb.append( jsonAttributes[i] );

if ( i 

{ // 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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值