当前位置:编程学习 > JAVA >>

java连接Neo4j服务器

[java]  
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  
    }  
  
    // START SNIPPET: insideAddMetaToProp  
    private static void addMetadataToProperty( URI relationshipUri,  
            String name, String v
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,