Neo4j 3.3.1-自定义APOC程序失败

时间:2018-01-02 08:49:12

标签: java neo4j neo4j-apoc

我正在尝试使用APOC在Neo4j 3.3.1版本中编写自己的程序。作为练习,我盯着返回所有节点ID。由于我是java环境的新手,因此要成功运行它。

下面是我的java代码,用于返回节点ID' s。 包示例;

import java.io.File;
import java.util.stream.Stream;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Procedure;

public class AllNodes {

    public class Output{
    public String out;
    }

    @Context 
    private GraphDatabaseService db;

    @Procedure(value = "example.allnodes")
    public Stream<Output> allnodes() {
        db = new GraphDatabaseFactory().newEmbeddedDatabase( new File("/Users/ashwanthkumardaggula/Library/Application Support/Neo4j Desktop/Application/neo4jDatabases/database-ce072093-73a7-464e-baa8-d953f5b5f53c/installation-3.3.1/data/databases/graph.db") );
        Result re = db.execute("MATCH (n) RETURN n");
//      Stream<Output> res = re.resultAsString();
        return  (Stream<Output>) re;
    }

}

错误:

org.neo4j.server.ServerStartupException: Starting Neo4j failed: Component 'org.neo4j.server.database.LifecycleManagingDatabase@455824ad' was successfully initialized, but failed to start. Please see the attached cause exception "Field `db` on `AllNodes` must be non-final and public.".
    at org.neo4j.server.exception.ServerStartupErrors.translateToServerStartupError(ServerStartupErrors.java:68)
    at org.neo4j.server.AbstractNeoServer.start(AbstractNeoServer.java:219)
    at org.neo4j.server.ServerBootstrapper.start(ServerBootstrapper.java:111)
    at org.neo4j.server.ServerBootstrapper.start(ServerBootstrapper.java:79)
    at org.neo4j.server.enterprise.CommercialEntryPoint.main(CommercialEntryPoint.java:22)
Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.server.database.LifecycleManagingDatabase@455824ad' was successfully initialized, but failed to start. Please see the attached cause exception "Field `db` on `AllNodes` must be non-final and public.".
    at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:466)
    at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:107)
    at org.neo4j.server.AbstractNeoServer.start(AbstractNeoServer.java:211)
    ... 3 more
Caused by: java.lang.RuntimeException: Error starting org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory, /Users/ashwanthkumardaggula/Library/Application Support/Neo4j Desktop/Application/neo4jDatabases/database-ce072093-73a7-464e-baa8-d953f5b5f53c/current/data/databases/graph.db
    at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.initFacade(GraphDatabaseFacadeFactory.java:211)
    at org.neo4j.kernel.enterprise.EnterpriseGraphDatabase.<init>(EnterpriseGraphDatabase.java:36)
    at org.neo4j.server.enterprise.EnterpriseNeoServer.lambda$static$1(EnterpriseNeoServer.java:75)
    at org.neo4j.server.database.LifecycleManagingDatabase.start(LifecycleManagingDatabase.java:88)
    at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:445)
    ... 5 more
Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.impl.proc.Procedures@4038cd3a' was successfully initialized, but failed to start. Please see the attached cause exception "Field `db` on `AllNodes` must be non-final and public.".
    at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:466)
    at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:107)
    at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.initFacade(GraphDatabaseFacadeFactory.java:207)
    ... 9 more
Caused by: org.neo4j.kernel.api.exceptions.ProcedureException: Field `db` on `AllNodes` must be non-final and public.
    at org.neo4j.kernel.impl.proc.FieldInjections.assertValidForInjection(FieldInjections.java:157)
    at org.neo4j.kernel.impl.proc.FieldInjections.setters(FieldInjections.java:110)
    at org.neo4j.kernel.impl.proc.ReflectiveProcedureCompiler.compileProcedure(ReflectiveProcedureCompiler.java:263)
    at org.neo4j.kernel.impl.proc.ReflectiveProcedureCompiler.compileProcedure(ReflectiveProcedureCompiler.java:213)
    at org.neo4j.kernel.impl.proc.ProcedureJarLoader.loadProcedures(ProcedureJarLoader.java:94)
    at org.neo4j.kernel.impl.proc.ProcedureJarLoader.loadProceduresFromDir(ProcedureJarLoader.java:82)
    at org.neo4j.kernel.impl.proc.Procedures.start(Procedures.java:275)
    at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:445)
    ... 11 more
2018-01-02 08:38:46.882+0000 INFO  Neo4j Server shutdown initiated by request

如何成功运行我的代码?

1 个答案:

答案 0 :(得分:0)

错误非常自我解释,您的过程类中的db字段应该是公开的。

更改

@Context 
private GraphDatabaseService db;

@Context 
public GraphDatabaseService db;