使用graphdb在节点之间建立关系

时间:2014-03-13 06:18:21

标签: java graph neo4j jersey

我是graph-db的新手,现在我正在尝试使用示例来做基础知识。下面是我试过的示例代码,我试图在三个节点之间建立关系。我试图将下面的内容作为o / p。

我的代码

        private static final String DB_PATH = "/home/nnn/Documents/softwares/neo4j-community-2.0.1";

public String greeting;


GraphDatabaseService graphDb;
Node firstNode;
Node secondNode;
Node thirdNode;

Relationship relationship;

private static enum RelTypes implements RelationshipType
{
    LIKES
}

public static void main( final String[] args )
{
    G1 hello = new G1();
    hello.createDb();
    hello.removeData();
    hello.shutDown();
}

void createDb()
{
    clearDb();

    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
    registerShutdownHook( graphDb );

    try ( Transaction tx = graphDb.beginTx() )
    {

        firstNode = graphDb.createNode();
        firstNode.setProperty( "message", "Alice " );
        secondNode = graphDb.createNode();
        secondNode.setProperty( "message", "Bob" );
        thirdNode = graphDb.createNode();
        thirdNode.setProperty( "message", "Anu" );

        relationship = firstNode.createRelationshipTo( secondNode, RelTypes.LIKES);
        relationship.setProperty( "message", "likes " );

        relationship = firstNode.createRelationshipTo( thirdNode, RelTypes.LIKES);
        relationship.setProperty( "message", "likes " );

        System.out.print( firstNode.getProperty("message"));
        System.out.print( relationship.getProperty( "message" ) );
        System.out.print( secondNode.getProperty( "message" ) );


        System.out.print( firstNode.getProperty( "message" ) );
       //System.out.print( relationship.getProperty( "message" ) );
        //System.out.print( thirdNode.getProperty( "message" ) );


        greeting = ( (String) firstNode.getProperty( "message" ) )
                   + ( (String) relationship.getProperty( "message" ) )
                   + ( (String) secondNode.getProperty( "message" ) )
                   + ( (String) thirdNode.getProperty( "message" ) );

        tx.success();
    }

}

private void clearDb()
{
    try
    {
        FileUtils.deleteRecursively( new File( DB_PATH ) );
    }
    catch ( IOException e )
    {
        throw new RuntimeException( e );
    }
}

void removeData()
{
    try ( Transaction tx = graphDb.beginTx() )
    {

        firstNode.getSingleRelationship( RelTypes.LIKES, Direction.OUTGOING ).delete();
        firstNode.delete();
        secondNode.delete();
        thirdNode.delete();

        tx.success();
    }
}

void shutDown()
{
    System.out.println();
    System.out.println( "Shutting down database ..." );

    graphDb.shutdown();

}


private static void registerShutdownHook( final GraphDatabaseService graphDb )
{

    Runtime.getRuntime().addShutdownHook( new Thread()
    {
        @Override
        public void run()
        {
            graphDb.shutdown();
        }
    } );
}

}

2 个答案:

答案 0 :(得分:1)

如异常所示,您正在呼叫

firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();

当你从firstNode有多个类型为KNOWS的传出关系时 (firstNode-> secondNode和firstNode-> thirdNode)

也许您想要使用http://api.neo4j.org/2.0.1/org/neo4j/graphdb/Node.html#getRelationships(org.neo4j.graphdb.RelationshipType,%20org.neo4j.graphdb.Direction)代替

对于输出,您希望从第一个节点获得所有关系,以便能够获得每个关系的结束节点。

答案 1 :(得分:0)

您为KNOWS创建了两个外发firstNode关系。

如果要删除它们,请执行以下操作:

for (Relationship rel : node.getRelationships()) {
   rel.delete();
}

您也可以将方向和类型参数传递给该方法。

但是如果要删除节点,则必须先删除所有关系。