【发布时间】:2019-05-29 16:24:16
【问题描述】:
我正在与 NEO4J 和 Java 合作,为一个应用程序创建一个原型,该应用程序集成了一个图形数据库,该数据库包含患者信息(包含虚假的、虚构的数据)。
我创建了一个简单的二类程序,并创建了节点(尚未分配关系)。但是,我希望能够查看我创建的节点,以确保我的应用程序正常工作,并能够在 NEO4J 浏览器/社区服务器中查看结果。
如何让节点在视觉上显示?我知道我可以通过查询它们来测试它们是被创建的,但我也想知道如何直观地显示它们。
我尝试进入 Neo4j.conf 文件,并将活动数据库从默认的 "graph.db" 更改为 "Users/andrew/eclipse- workspace/patients-database/target/patient-db",因为在我创建的Java类中,我使用这行代码来设置我的数据库:
private static final File Patient_DB = new File("target/patient-db");
但是,每当我在 localhost:7474 上打开 NEO4J 浏览器时,运行我的代码后都看不到任何节点。
下面,我将代码粘贴到我的 PatientGraph 类中(另一个类只是一个创建患者及其属性的 Patient 类)
package com.andrewhe.neo4j.Patients_Database;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.traversal.Evaluators;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.graphdb.traversal.Traverser;
import org.neo4j.io.fs.FileUtils;
public class PatientGraph {
private static final File Patient_DB = new File("target/patient-db");
private ArrayList<Patient> patients = new ArrayList<Patient>();
private long patientZero;
private GraphDatabaseService graphDb;
public ArrayList<Patient> getPatients() { return patients; }
public void manualPatientSetUp() throws IOException {
Patient homeNode = new Patient("");
Patient jan = new Patient("Jan");
patients.add(jan);
Patient kim = new Patient("Kim");
patients.add(kim);
Patient ahmad = new Patient("Ahmad");
patients.add(ahmad);
Patient andrew = new Patient("Andrew");
patients.add(andrew);
}
public void createPatientNodes() throws IOException {
FileUtils.deleteRecursively(Patient_DB);
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(Patient_DB);
registerShutdownHook();
try (Transaction tx = graphDb.beginTx()) {
for (Patient patient : patients) {
Node patientNode = graphDb.createNode();
System.out.println("Node created");
setProperties(patientNode, patient);
}
tx.success();
}
}
//Method to create and set properties for node instead of using 5 set properties each time.
public void setProperties(Node node, Patient patient) {
node.setProperty("name", patient.getName());
node.setProperty("weight", patient.getWeight());
node.setProperty("pat_id", new String(patient.getPatientID()));
node.setProperty("age", patient.getAge());
//Don't worry about diagnoses yet;
//To get it to work, just turn the diagnoses ArrayList into a String separated by commas.
}
public void setUp() throws IOException {
//reads in patients using a file
}
public void shutdown()
{
graphDb.shutdown();
}
private void registerShutdownHook()
{
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running example before it's completed)
Runtime.getRuntime().addShutdownHook(new Thread(() -> graphDb.shutdown()));
}
public static void main(String[] args) throws IOException {
PatientGraph pg = new PatientGraph();
pg.manualPatientSetUp();
pg.createPatientNodes();
for (int i = 0; i < pg.getPatients().size(); i++) {
pg.getPatients().get(i).printAllData();
}
pg.shutdown();
}
}
【问题讨论】:
标签: java database neo4j graph-databases