您的位置:首页 > 编程语言 > Java开发

【java连接Neo4j服务器】

2013-08-11 14:28 141 查看
我们来看一下java连接Neo4j服务器的具体实现:

package com.wzs.linux;

public class Relationship

{

public static final String OUT = "out";

public static final String IN = "in";

public static final String BOTH = "both";

private String type;

private String direction;

public String toJsonCollection()

{

StringBuilder sb = new StringBuilder();

sb.append("{ ");

sb.append(" \"type\" : \"" + type + "\"");

if (direction != null)

{

sb.append(", \"direction\" : \"" + direction + "\"");

}

sb.append(" }");

return sb.toString();

}

public Relationship(String type, String direction)

{

setType(type);

setDirection(direction);

}

public Relationship(String type)

{

this(type, null);

}

public void setType(String type)

{

this.type = type;

}

public void setDirection(String direction)

{

this.direction = direction;

}

}

[java]

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

}

更多精彩教程请关注:电脑桌面图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: