我可以使用neo4jClient连接我的neo4j服务器,一切正常。
对于单元测试场景,我想使用不同的本地服务器对我的neo4j DAL层执行单元测试。
所以我尝试了neo4j嵌入式版本。我可以使用不推荐的
创建节点并查询它们GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH)
ExecutionEngine engine = new ExecutionEngine(graphDb);
1)创建嵌入式neo4j实例的新方法是什么?
2)如何使用neo4jClient查询嵌入式?尝试连接本地主机但没有成功(嵌入式版本是否有网络主机?)
答案 0 :(得分:0)
创建嵌入式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.db
与嵌入式示例中的DB_PATH
相同。
答案 1 :(得分:0)
请查看此示例,它会对您有所帮助。
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);
}
}

创建一个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;
}
}
&#13;
并创建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);
}
&#13;