简体   繁体   中英

Use embedded neo4j with neo4j client

I can connect my neo4j server using the neo4jClient and every thing works fine.

For unit testing scenarios I want to use a different local server to perform unit testing to my neo4j DAL layer.

so I tried the neo4j embedded version. I can create nodes and query them using the deprecated

GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH)
ExecutionEngine engine = new ExecutionEngine(graphDb);

1)What is the new way to create embedded neo4j instance?

2)How can I query the embedded using the neo4jClient? tried connecting with local host but with no success(is the embedded version has web host?)

What is the new way to create embedded neo4j instance?

You actually already did it with the code in your question!

The documentation on the hello world app for embedded neo4j shows this code:

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

So you're already there.

How can I query the embedded using the neo4jClient? tried connecting with local host but with no success(is the embedded version has web host?)

If by the "neo4jclient" you mean the tool that people use in their browsers to visualize graphs, here's how to do that.

When you create an embedded neo4j database, the DB_PATH is important. Basically you just end up creating a directory by that name locally.

The neo4j browser application can be pointed at any graph path. It doesn't run embedded, it runs along with the server, so practically speaking what you'll do is configure the server to point to that directory you created for the embedded DB, and then it'll work.

See this documentation , you need to set:

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

Where data/graph.db is the same as DB_PATH in your embedded example.

Check with this example, it will help you.

Application.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); } } 

create a pojo file, 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; } } 

and create 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); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM