【问题标题】:Making relationships between nodes using graphdb使用 graphdb 在节点之间建立关系
【发布时间】:2014-03-13 06:18:20
【问题描述】:

我对 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();
        }
    } );
}

}

【问题讨论】:

  • 你能把整个方法贴出来吗?
  • 您的“消息”属性也没有意义,它们应该是name 属性。对于关系,您需要重复 rel 类型的 no 属性。
  • 您没有显示异常发生的位置。我认为您不应该开始使用 Java API,而是参加在线课程 (neo4j.org/learn/online_course),学习 cypher 并从那里开始。
  • @Luanne 我编辑了我的帖子并添加了我的整个代码

标签: java graph neo4j jersey


【解决方案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)

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

【讨论】:

    【解决方案2】:

    您为您的firstNode 创建了两个传出KNOWS 关系。

    如果你想全部删除:

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

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

    但是如果要删除一个节点,必须先删除它的所有关系。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多