gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:17:12 36 4
gpt4 key购买 nike

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

}

最佳答案

正如异常所示,您正在调用

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

当你有多个来自firstNode的KNOWS类型的传出关系时(第一个节点->第二个节点和第一个节点->第三个节点)

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

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

关于java - 使用 graphdb 在节点之间建立关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22370559/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com