【问题标题】:How can I view the visual graph representation of the nodes created from a Neo4J Java Application?如何查看从 Neo4J Java 应用程序创建的节点的可视化图形表示?
【发布时间】: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


    【解决方案1】:

    您没有提供有关如何查询节点的足够信息。在实际粘贴整个课程内容之前,您甚至没有简要说明您的课程是做什么的。这些类之间有什么关系?期望有人从代码中为您解码,要求太多了。

    您可以使用 Neo4J Browser(内置)或 Neo4J Bloom(商业工具)来可视化图形节点及其互连(关系)。

    要查询 Neo4j 数据库,您可以使用 Cypher,这是一种图形查询语言,将模式表示为 Ascii Art。

    下面的文章给出了查询和可视化图形节点的详细动手过程。

    https://medium.com/neo4j/hands-on-graph-data-visualization-bd1f055a492d

    【讨论】:

    • 您好,我的问题不是关于查询节点。我特别声明我只是希望能够以图形方式查看从 Neo4J 浏览器中的 Java 应用程序创建的节点。我真的不相信 Patient 课程对于回答我的问题是必要的;我相信的相关信息是我创建仅限于 PatientGraph 类的节点的地方。但是,谢谢你的链接。我会读一读。
    • 您确定数据库包含数据条目或记录吗?请检查一次。您可以删除架构目录,以便在启动时重建架构/标签索引。之前请做好备份。并再次尝试浏览器。
    猜你喜欢
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2013-03-25
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    相关资源
    最近更新 更多