【问题标题】:Use embedded neo4j with neo4j client将嵌入式 neo4j 与 neo4j 客户端一起使用
【发布时间】:2016-03-15 15:34:18
【问题描述】:

我可以使用 neo4jClient 连接我的 neo4j 服务器,并且一切正常。

对于单元测试场景,我想使用不同的本地服务器对我的 neo4j DAL 层执行单元测试。

所以我尝试了 neo4j 嵌入式版本。我可以创建节点并使用已弃用的

查询它们
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH)
ExecutionEngine engine = new ExecutionEngine(graphDb);

1)创建嵌入式neo4j实例的新方法是什么?

2)如何使用neo4jClient 查询嵌入式?尝试连接本地主机但没有成功(嵌入式版本是否有网络主机?)

【问题讨论】:

    标签: java neo4j neo4j-embedded


    【解决方案1】:

    创建嵌入式 neo4j 实例的新方法是什么?

    你实际上已经用你问题中的代码做到了!

    The documentation on the hello world app for embedded neo4j 显示此代码:

    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );

    所以你已经在那里了。

    如何使用 neo4jClient 查询嵌入式?尝试连接本地主机但没有成功(嵌入式版本是否有网络主机?)

    如果您所说的“neo4jclient”是指人们在浏览器中用来可视化图表的工具,那么这里是如何做到这一点的。

    当您创建嵌入式 neo4j 数据库时,DB_PATH 很重要。基本上,您最终只是在本地创建了一个使用该名称的目录。

    neo4j 浏览器应用程序可以指向任何图形路径。它不嵌入式运行,它与服务器一起运行,所以实际上你要做的是将服务器配置为指向你为嵌入式数据库创建的目录,然后它就会工作。

    this documentation,需要设置:

    org.neo4j.server.database.location=data/graph.db

    在您的嵌入式示例中,data/graph.dbDB_PATH 相同。

    【讨论】:

    • 在 2.3.2 版中标记为已弃用的类
    【解决方案2】:

    看看这个例子,它会对你有所帮助。

    应用程序.java

    package hello;
    
    import java.io.File;
    
    import org.neo4j.graphdb.GraphDatabaseService;
    import org.neo4j.graphdb.Transaction;
    import org.neo4j.graphdb.factory.GraphDatabaseFactory;
    import org.neo4j.kernel.impl.util.FileUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
    import org.springframework.data.neo4j.config.Neo4jConfiguration;
    import org.springframework.data.neo4j.core.GraphDatabase;
    
    @SpringBootApplication
    public class Application implements CommandLineRunner {
    
    	@Configuration
    	@EnableNeo4jRepositories(basePackages = "hello")
    	static class ApplicationConfig extends Neo4jConfiguration {
    
    		public ApplicationConfig() {
    			setBasePackage("hello");
    		}
    
    		@Bean
    		GraphDatabaseService graphDatabaseService() {
    			
    			return new GraphDatabaseFactory().newEmbeddedDatabase("C:/neo4j-community-2.1.7/data/graph.db");		
    		}
    	}
    
    	@Autowired PersonRepository personRepository;
    
    	@Autowired GraphDatabase graphDatabase;
    
    	public void run(String... args) throws Exception {
    
    		Person greg = new Person("Greg");
    		Person roy = new Person("Roy");
    		Person craig = new Person("Craig");
    		
    		Person abc=new Person("ABC");
    		Person def=new Person("DEF");
    		Person ghi=new Person("GHI");
    
    		/*System.out.println("Before linking up with Neo4j...");*/
    		for (Person person : new Person[] { greg, roy, craig,abc,def,ghi }) {
    		/*	System.out.println(person);*/
    		}
    
    		Transaction tx = graphDatabase.beginTx();
    		try {
    			personRepository.save(greg);
    			personRepository.save(roy);
    			personRepository.save(craig);
    			
    			personRepository.save(abc);
    			personRepository.save(def);
    			personRepository.save(ghi);
    			
    
    			greg = personRepository.findByName(greg.name);
    			greg.worksWith(roy);
    			greg.worksWith(craig);
    			personRepository.save(greg);
    
    			roy = personRepository.findByName(roy.name);
    			roy.worksWith(craig);
    			// We already know that roy works with greg
    			personRepository.save(roy);
    
    			// We already know craig works with roy and greg
    
    		//	System.out.println("Lookup each person by name...");
    			for (String name : new String[] { greg.name, roy.name, craig.name }) {
    				System.out.println("--->"+personRepository.findByName(name));
    			}
    
    		//	System.out.println("Looking up who works with Greg...");
    			for (Person person : personRepository.findByTeammatesName("Greg")) {
    				System.out.println("==>>"+person.name + " works with Greg.");
    			}
    		
    			tx.success();
    		} finally {
    			tx.close();
    		}
    	}
    
    	public static void main(String[] args) throws Exception {
    		FileUtils.deleteRecursively(new File("C:/neo4j-community-2.1.7/data/graph.db"));
    
    		SpringApplication.run(Application.class, args);
    	}
    }

    创建一个pojo文件,Person.java

    package hello;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import org.neo4j.graphdb.Direction;
    import org.springframework.data.neo4j.annotation.Fetch;
    import org.springframework.data.neo4j.annotation.GraphId;
    import org.springframework.data.neo4j.annotation.NodeEntity;
    import org.springframework.data.neo4j.annotation.RelatedTo;
    
    @NodeEntity
    public class Person {
    
        @GraphId Long id;
        public String name;
    
        public Person() {
        	
        }
        
        public Person(String name) { 
        	this.name = name; 
        }
    
        @RelatedTo(type="TEAMMATE", direction=Direction.BOTH)
        public @Fetch Set<Person> teammates;
    
        public void worksWith(Person person) {
            if (teammates == null) {
                teammates = new HashSet<Person>();
            }
            teammates.add(person);
        }
    
        public String toString() {
            String results = name + "'s teammates include\n";
            if (teammates != null) {
                for (Person person : teammates) {
                    results += "\t- " + person.name + "\n";
                }
            }
            return results;
        }
    
    }

    并创建 PersonRepository.java

    package hello;
    
    import org.springframework.data.repository.CrudRepository;
    
    public interface PersonRepository extends CrudRepository<Person, String> {
    
        Person findByName(String name);
    
        Iterable<Person> findByTeammatesName(String name);
    
    }

    【讨论】:

    • 它不适合我的用例,我有一个 neo4j DAL 类(自行实现),它使用 neo4jClient 类(使用 maven ava-neo4j-client)连接到 neo4j 服务器。现在我想要实现的是使用嵌入式初始化数据并使用我的 neo4j DAL 查询它来执行单元测试。
    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多