日期:2014-05-16  浏览次数:20609 次

java连接Neo4j服务器

一:Neo4j服务器安装(参考:http://docs.neo4j.org.cn/server-installation.html)

1.下载Neo4j数据,我下载的版本是: neo4j-enterprise-1.8.1-windows

2.解压 neo4j-enterprise-1.8.1-windows 

3.到Neo4j的bin目录下neo4j-enterprise-1.8.1-windows\neo4j-enterprise-1.8.1\bin

4.运行 neo4j start 命令

5.打开 http://localhost:7474  看到图形化界面则安装成功!


二:测试代码(参考:http://www.neo4j.org.cn/2012/07/30/server-java-rest-client-example/)

测试代码总共有三个类:

CreateSimpleGraph.java  下载地址:https://github.com/neo4j/community/blob/1.8.M06/server-examples/src/main/java/org/neo4j/examples/server/CreateSimpleGraph.java
Relationship.java  下载地址:https://github.com/neo4j/community/blob/1.8.M06/server-examples/src/main/java/org/neo4j/examples/server/Relationship.java
TraversalDescription.java  下载地址:https://github.com/neo4j/community/blob/1.8.M06/server-examples/src/main/java/org/neo4j/examples/server/TraversalDescription.java


三:程序正常运行用到的jar包

1.neo4j-enterprise-1.8.1-windows\neo4j-enterprise-1.8.1\lib下所有jar包

2.自己下载的jar包

com.sun.jersey.jersey-core-1.4.0.jar

javax.ws.rs.jar

jersey-client-1.9.jar


四:程序代码

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;
    }
}

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 SNIP