引子-neo4j

使用场景

  • 社交网络
    我需要某社区人与人之间的亲属、同事、朋友等各种关系,每个人的兴趣、爱好,并从事的各种不同活动。暂时这么多,需求可能会变的
  • 推荐引擎
    哪些人在哪里看了哪些电影,对哪些电影做了哪些评价,做一个电影社区推荐引擎
  • 交通运输
    运输工具、线路、站点以及班车、调度等各种数据,计算出两个站点的最短路径
  • 物流管理
    包裹分拣中心需要每个包裹配送的最短路径

嵌入式的Neo4j

   <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>3.0.6</version>
        </dependency>

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * 增删改查
 */
public class Knows {
    private static final File DB_PATH = new File("demo/neo4j-db");
    private static GraphDatabaseService graphDb;

    public static void main(String... args) throws IOException {
        Knows knows = new Knows();
        System.out.println("-------------create:");
        knows.create();
        System.out.println("-------------edit:");
        knows.edit();
        System.out.println("-------------remove:");
        knows.remove();
        knows.shutDown();
    }

    public void create() {
        try (Transaction tx = graphDb.beginTx()) {
            // 标签
            Label label = Label.label("Person");
            // 节点
            Node first = graphDb.createNode(label);
            first.setProperty("name", "first");
            Node second = graphDb.createNode(label);
            second.setProperty("name", "second");
            // 关系
            Relationship relationship = first.createRelationshipTo(second, RelTypes.KNOWS);
            System.out.println("create node name is " + first.getProperty("name"));
            System.out.println("create node name is " + second.getProperty("name"));
            System.out.println("create relationship type is " + relationship.getType());
            // 提交事务
            tx.success();
        }
    }

    public void edit() {
        try (Transaction tx = graphDb.beginTx()) {
            Label label = Label.label("Person");
            Node node = graphDb.findNode(label, "name", "second");
            System.out.println("query node name is " + node.getProperty("name"));
            node.setProperty("name", "My name");
            node.setProperty("sex", "男");
            System.out.println("edit node name is " + node.getProperty("name"));
            System.out.println("add property sex is " + node.getProperty("sex"));

            tx.success();
        }
    }

    /**
     * 如果存在关系,则必须先删除关系 , 再删除节点,或者同时删除;
     * 如果不 存在关系 ,则删除节点的操作将不能被成功执行。
     */
    public void remove() {
        try (Transaction tx = graphDb.beginTx()) {
            Label label = Label.label("Person");
            Node first = graphDb.findNode(label, "name", "first");
            Relationship relationship = first.getSingleRelationship(RelTypes.KNOWS, Direction.OUTGOING);

            System.out.println("delete node name is " + first.getProperty("name")
                    + ", relationship is KNOWS, end node name is "
                    + relationship.getEndNode().getProperty("name"));
            relationship.delete();
            relationship.getEndNode().delete();
            first.delete();

            tx.success();
        }
    }

    public void shutDown() {
        System.out.println();
        System.out.println("Shutting down database ...");
        graphDb.shutdown();
    }

    public Knows() throws IOException {
        // 递归删除
        FileUtils.deleteRecursively(DB_PATH);
        // 创建嵌入式数据库
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
        registerShutdownHook(graphDb);
    }

    private static void registerShutdownHook(final GraphDatabaseService graphDb) {
        Runtime.getRuntime().addShutdownHook(new Thread(graphDb::shutdown));
    }

    private static enum RelTypes implements RelationshipType {
        KNOWS
    }
}

参考github

posted @ 2022-03-28 09:22  fly_bk  阅读(30)  评论(0编辑  收藏  举报