Neo4j学习之路05——Java嵌入开发

Java开发人员可以在java代码中调用Neo4j的API,并且将操作嵌入在Java代码中。

一、基本操作:

创建maven工程并添加neo4j的依赖关系

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

创建图数据库服务实例(GraphDatabaseService),这个实例可以在多个线程间共享,但不能创建指向同一个数据库的多个实例。

    // 全局只创建一个GraphDatabaseService实例对象
    private GraphDatabaseService graphDb;

加载数据库数据文件使用文件路径加载的方式,首先创建一个File对象

 private static final File databaseDiretory = new File("your database path");

打开数据库:

 使用GraohDatabase的工厂方法创建数据库对象,打开数据库

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

关闭数据库:

graphDb.shutdown();

声明节点:

private Node firstNode, secondNode;

声明关系:

private Relationship relationship;

neo4j的java嵌入式开发中,节点的属性与标签都是通过枚举的方法来声明的

    // 创建关系枚举
    private static enum RelType implements RelationshipType{
        KNOWS
    }

    // 创建标签枚举
    private enum MyLabels implements Label{
        Person
    }

CRUD:

在用java程序对数据库进行操作时,需要先关闭neo4j服务,否则运行会报冲突错误。

对节点的操作需要封装成一个事务来进行。

创建节点:

    // 创建数据
    private void createDb(){
        System.out.println("打开数据库。。。");
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(databaseDiretory);

        // 创建事务
        try(Transaction tx = graphDb.beginTx()){
            firstNode = graphDb.createNode();
            // 设置属性
            firstNode.setProperty("message", "Hello");
            // 添加标签
            firstNode.addLabel(MyLabels.Person);

            secondNode = graphDb.createNode();
            secondNode.setProperty("message", "World");

            relationship = firstNode.createRelationshipTo(secondNode, RelType.KNOWS);
            relationship.setProperty("message", "brave Neo4j");

            // 读取关系实例
            System.out.println(firstNode.getProperty("message"));
            System.out.println(relationship.getProperty("message"));
            System.out.println(secondNode.getProperty("message"));


            // 提交事务
            tx.success();
        }
    }

删除节点:

    // 删除数据
    private void removeData(){
        try(Transaction tx = graphDb.beginTx()){
            firstNode.getSingleRelationship(RelType.KNOWS, Direction.OUTGOING).delete();
            firstNode.delete();
            secondNode.delete();
            System.out.println("数据信息已删除");
            tx.success();
        }
    }

查询节点:

    // 查询数据
    private void searchNode(){
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(databaseDiretory);

        try(Transaction tx = graphDb.beginTx()){
            // 查找所有带Person标签的节点存放在迭代器中
            ResourceIterator<Node> persions = graphDb.findNodes(MyLabels.Person);

            // 查询带有Person标签message属性为hello,的节点集合
//            Map<String, Object> propertyValues = new HashMap<String, Object>();
//            propertyValues.put("message", "hello,");
//            ResourceIterator<Node> persions = graphDb.findNodes(MyLabels.Person, propertyValues);

            persions.forEachRemaining((entityTypeGraphNode) -> {
                // 输出带有Person标签节点的message属性信息
                System.out.println("Node id:" + entityTypeGraphNode.getId() + ",property:" + entityTypeGraphNode.getProperties("message"));

                // 得到带有Person标签节点的所有关系存放在迭代器中
                Iterable<Relationship> typeObjectsRelationships = entityTypeGraphNode.getRelationships();
                typeObjectsRelationships.forEach(
                        (relationship) -> {
                            // 得到该关系的尾节点
                            Node entityGraphNode = relationship.getEndNode();
                            System.out.println("Realation id:" + entityGraphNode.getId() + ",property:" + entityGraphNode.getProperty("message"));
                        }
                );
            });

            tx.success();
        }

    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值