LoginSignup
3
4

More than 5 years have passed since last update.

JavaでNeo4jを組み込んでノードと関係をCreate / Deleteする

Last updated at Posted at 2015-08-18

環境の構築や、簡単な試行は以前書いた記事へ。
Java組み込みでNeo4jを使う

基本的な手順としては
1.コード書く
2.サーバ立ち上がってない状態で実行
3.サーバ立ち上げてブラウザで確認
を繰り返してうまく動いてるかやってみてます。

ノード・関係のCreateは上記のページでやっちゃってるので、それを再記します。

Create.java
package com.example;

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.DynamicLabel; 

/**
 * ノードと関係のCreate
 */
public class Create {

    public enum MyRelationshipTypes implements RelationshipType {
        KNOWS
    }

    public static void main(String[] args) {
        GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("data/graphdb");

        Transaction tx = graphDb.beginTx();
        try {

            //2つのノードと1つの関係を作成
            Node firstNode = graphDb.createNode();
            Node secondNode = graphDb.createNode();
            Relationship relationship = firstNode.createRelationshipTo(secondNode, MyRelationshipTypes.KNOWS);

            //それぞれのノードにラベルを追加
            Label firstLabel = DynamicLabel.label("bird");
            firstNode.addLabel(firstLabel);
            Label secondLabel = DynamicLabel.label("little bird");
            secondNode.addLabel(secondLabel);

            //それぞれのノードにプロパティを追加
            firstNode.setProperty("message", "haine ");
            secondNode.setProperty("message", "pooh");
            relationship.setProperty("message", "like ");
            tx.success();

            //コンソールに印字
            System.out.print(firstNode.getProperty("message"));
            System.out.print(relationship.getProperty("message"));
            System.out.print(secondNode.getProperty("message"));
        }
        finally {
            tx.close();
            graphDb.shutdown();
        }
    }
}

次に、Delete。

まずはIDを指定して削除する方法。
Deleteをするためには、まず検索をしてどのノード、どの関係をDeleteするか指定しなければいけない。
ブラウザで削除するノード・関係のIDを確認しておく。

なお、ノードを削除する際にはそのノードから出ている関係が全て削除されていないとエラーが出る模様

DeleteById.java
package com.example;

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

/**
 * IDの検索をしてノード・関係を削除する
 */
public class DeleteById {

    public enum MyRelationshipTypes implements RelationshipType {
        KNOWS,
        LIKES;
    }

    public static void main(String[] args) {
        GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("data/graphdb");

        Transaction tx = graphDb.beginTx();
        try {

            //2つのノードと1つの関係を作成
            Node firstNode;
            Node secondNode;
            Relationship relationship;

            //ノードのIDを指定して検索
            firstNode = graphDb.getNodeById(2);
            secondNode = graphDb.getNodeById(3);
            relationship = graphDb.getRelationshipById(1);

            //確認のためにコンソールにいろいろ表示
            System.out.println("relation=" + relationship.getProperty("message"));
            System.out.println("label=" + firstNode.getLabels());    
            System.out.println(firstNode.getSingleRelationship( MyRelationshipTypes.LIKES, Direction.OUTGOING ));         

            //関係とノードの削除
            relationship.delete();
            firstNode.delete();
            secondNode.delete();

            tx.success();
        }
        finally {
            tx.close();
            graphDb.shutdown();
        }
    }
}

次に、ノードを指定して、そのノードから出ている(または入ってくる)関係を削除する方法。

DeleteByRelation.java
package com.example;

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

/**
 * 関係を検索し削除
 */
public class DeleteByRelation {

    public enum MyRelationshipTypes implements RelationshipType {
        KNOWS,
        LIKES;
    }

    public static void main(String[] args) {
        GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("data/graphdb");

        Transaction tx = graphDb.beginTx();
        try {

            //2つのノードと1つの関係を作成
            Node firstNode;
            Node secondNode;

            //ノードのIDを指定
            firstNode = graphDb.getNodeById(0);
            secondNode = graphDb.getNodeById(1);

            //指定したノードから出ている関係を削除
            firstNode.getSingleRelationship(MyRelationshipTypes.KNOWS, Direction.OUTGOING).delete();
            firstNode.delete();
            secondNode.delete();

            tx.success();
        }
        finally {
            //tx.finish();
            tx.close();
            graphDb.shutdown();
        }
    }
}

firstNode.getSingleRelationship(MyRelationshipTypes.KNOWS, Direction.OUTGOING).delete();
はfirstノードからKNOWSという関係が出ているという意味を示す。
逆に入ってくる関係を削除したければ
firstNode.getSingleRelationship(MyRelationshipTypes.KNOWS, Direction.INCOMING).delete();
に変えれば良い。

検索の仕方によってさまざまなノード・関係をとってこれるようになれば、あとは同じ方法で削除できるはず。

以降、いろいろ試しながら有用なのがあったら追記していく予定。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4