为什么Neo4jTemplate涵盖了节点数而不是其内容?

时间:2014-11-08 12:23:06

标签: java csv neo4j spring-data-neo4j

这是我的pom.xml:

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j-rest</artifactId>
            <version>3.2.1.RELEASE</version>
        </dependency>

和config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/data/neo4j
                            http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd">

     <!-- REST Connection to Neo4j server -->    
    <bean id="graphDatabaseService"
          class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
        <constructor-arg index="0" value="http://localhost:7474/db/data" />

    </bean>

    <!-- graphDatabaseService- Neo4j configuration (creates Neo4jTemplate) -->
    <neo4j:config
            storeDirectory="db/neo4j/data/graph.db"
            base-package="x.y.z.mediator.domain.model"
            graphDatabaseService="graphDatabaseService"/>

</beans>

我想要neo4jtemplate形式的neo4j独立服务器:

@Repository
public class EmployeeDAO_Neo4j implements EmployeeDAO {

    @Inject
    private Neo4jTemplate neo4jTemplate;


    @Override
    public List<Map<String, Object>> findAll(String query) {

        //Object is the node address
        Result<Map<String, Object>> result = neo4jTemplate.query("MATCH (emp:`EmpBase`) RETURN emp;", null);
        //Below line results with: {emp=http://localhost:7474/db/data/node/1}
        System.out.println("Results:"+ result.as(List.class).get(1).toString());


        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;



        for(EmployeeBase u : result.to(EmployeeBase.class)) {
            map = new HashMap<String, Object>( );
           map.put(u.getE_id().toString(), u.toString());

            list.add(map);
        }

        System.out.println(result.to(EmployeeBase.class).as(List.class).get(1));

        return list;
    }
}

返回的列表是4个元素,但它在上面的代码的每个System.out.println中都说:{e_id=1, e_bossId=null, e_name='null'}所以它没有填充,因为测试数据是:

$ cat /tmp/empbase.csv 
e_id,e_bossid,e_name
11,11,Smith
12,11,Johnson
13,11,Roberts
14,13,Doe

包含结果的行只会显示:{emp=http://localhost:7474/db/data/node/1}

Neo4j服务器中的数据来自csv import。 empfull.csv:

CREATE empbase;

// Import data and schema for empbase; the '_EmpBase' is required by SpringData-neo4j
USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM "file:/tmp/empbase.csv" AS row
CREATE (:EmpBase:_EmpBase {  neo_eb_id:      row.e_id,
                    neo_eb_bossID:  row.e_bossid,
                    neo_eb_name:    row.e_name});

//Create index
CREATE INDEX ON :EmpBase:(neo_eb_id);

// Create relationships
LOAD CSV WITH HEADERS FROM "file:/tmp/empbase.csv" AS row
MATCH (employee:EmpBase:_EmpBase    {neo_eb_id: row.e_id})
MATCH (manager:EmpBase:_EmpBase     {neo_eb_id: row.e_bossid})
MERGE (employee)-[:REPORTS_TO]->(manager);

这是SDN的错误还是这个配置错了?

PS。 永远不会生成xml配置中的db/neo4j/data/graph.db。我用mvn clear package运行这个项目 PS2: 这是EmployeeBase.java

@NodeEntity
public class EmployeeBase {

    @GraphId
    private Long e_id;

    private Integer e_bossId;
    private String  e_name;

    public Long getE_id() {
        return e_id;
    }

    public Integer getE_bossId() {
        return e_bossId;
    }

    public String getE_name() {
        return e_name;
    }

    @Override
    public String toString() {
        return "{" +
                "e_id=" + e_id +
                ", e_bossId=" + e_bossId +
                ", e_name='" + e_name + '\'' +
                '}';
    }

}

1 个答案:

答案 0 :(得分:1)

由于您在导入中使用了错误的属性名称,因此使用neo_eb而不是e_为它们添加了前缀。你也有一些错别字,例如在e_bossId

CREATE empbase;

// Import data and schema for empbase; the '_EmpBase' is required by SpringData-neo4j
USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM "file:/tmp/empbase.csv" AS row
CREATE (:EmpBase:_EmpBase {  e_id:      row.e_id,
                    e_bossId:  row.e_bossid,
                    e_name:    row.e_name});

//Create index
CREATE INDEX ON :EmpBase:(e_id);

// Create relationships
LOAD CSV WITH HEADERS FROM "file:/tmp/empbase.csv" AS row
MATCH (employee:EmpBase:_EmpBase    {e_id: row.e_id})
MATCH (manager:EmpBase:_EmpBase     {e_id: row.e_bossid})
MERGE (employee)-[:REPORTS_TO]->(manager);