Neo4jRule:将算法插件(例如randomWalk)包括到Neo4j测试服务器中

时间:2019-05-10 09:36:07

标签: neo4j graph-databases

我正在尝试测试由Neo4j给出的用于测试过程的代码所完成的一些过程。但是,我的过程基于随机游走算法的结果,我必须通过“ algo.randomWalk.stream()”进行调用。

为此,我要实例化Neo4j测试服务器。但是,它无法识别algo.randomWalk.stream(),因为我认为它的插件中没有算法包。

这是我正在处理的代码


package example;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Rule;
import org.junit.Test;
import org.neo4j.driver.v1.*;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import static org.neo4j.driver.v1.Values.parameters;


import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.neo4j.harness.junit.Neo4jRule;


import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;
import static org.neo4j.driver.v1.Values.parameters;


public class ScoringTest {
    // This rule starts a Neo4j instance for us
    @Rule
    public Neo4jRule neo4j = new Neo4jRule()

            // This is the Procedure we want to test
            .withProcedure( Scoring.class );

    //org.neo4j.server.thirdparty_jaxrs_classes=org.neo4j.examples.server.unmanaged=/examples/unmanaged
    @Test
    public void shouldAllowReturningTheLastValue() throws Throwable
    {
        // This is in a try-block, to make sure we close the driver after the test
        try( Driver driver = GraphDatabase
                .driver( neo4j.boltURI() , Config.build().withEncryptionLevel( Config.EncryptionLevel.NONE ).toConfig() ) )
        {
            System.out.println(neo4j.boltURI().toString());
            // Given
            neo4j.withExtension(neo4j.boltURI().toString(), "..\\graph-algorithms-algo-3.5.4.0.jar");
            Session session = driver.session();
            String PATH = "..\\data\\data.json";
            File JSON_SOURCE = new File(PATH);

            List<HashMap<String,Object>> mss = new ObjectMapper().readValue(JSON_SOURCE, List.class);
            session.run("UNWIND {bulk} as row " + 
                    "CREATE (n:Users) " + 
                    "SET n += row.properties", parameters("bulk", mss ));
            for(int k = 0; k<9; k++) {
                PATH = "..\\data\\"+k+".json";
                mss = new ObjectMapper().readValue(JSON_SOURCE, List.class);
                JSON_SOURCE = new File(PATH);
                session.run("UNWIND {bulk} as row " + 
                        "MATCH (from:Users), (to:Clips) " + 
                        "WHERE ID(from) = toInt(row.from) AND ID(to) = toInt(row.to._key) " + 
                        "CREATE (from)-[rel:hasClipped]->(to) " + 
                        "SET rel += row.properties ", parameters("bulk", mss ));
            }

            // When
            Value result = session.run( "MATCH (n:Clips) WHERE ID(n) = 1038 " + 
                    "CALL algo.randomWalk.stream(ID(n), 2, 1) " + 
                    "YIELD nodeIds " + 
                    "UNWIND nodeIds as nodeId " + 
                    "MATCH (l:Clips)-[r:hasClipped]-(q:Users) " + 
                    "WHERE (ID(l) = nodeId) AND (ID(q) in nodeIds) " + 
                    "WITH collect({relation:r,Clip:l,User:q}) as res " + 
                    "RETURN res").single().get("res");
            System.out.println(result);
            // Then
            assertThat( result, equalTo( 0L ) );
        }
    }

}


我得到的确切错误是:org.neo4j.driver.v1.exceptions.ClientException:没有为此数据库实例注册名称为algo.randomWalk.stream的过程。请确保您正确拼写了过程名称,并且该过程已正确部署。

感谢您的时间和未来的答复, Syndorik

1 个答案:

答案 0 :(得分:0)

因此,我找到了解决此问题的方法。 Neo4jRule对象有一个选项,可让您更改插件目录的路径。

例如,我刚刚将此配置添加到Neo4jRule中,然后可以调用graphalgo库:

public Neo4jRule neo4j = new Neo4jRule()

            // This is the Procedure we want to test
            .withProcedure( Scoring.class )
            .withConfig(GraphDatabaseSettings.plugin_dir, "PATH_TO_PLUGIN_DIR")
            .withConfig(GraphDatabaseSettings.procedure_unrestricted, "algo.*" );