org.neo4j.graphdb.Node.createRelationshipTo()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(185)

本文整理了Java中org.neo4j.graphdb.Node.createRelationshipTo()方法的一些代码示例,展示了Node.createRelationshipTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node.createRelationshipTo()方法的具体详情如下:
包路径:org.neo4j.graphdb.Node
类名称:Node
方法名:createRelationshipTo

Node.createRelationshipTo介绍

[英]Creates a relationship between this node and another node. The relationship is of type type. It starts at this node and ends at otherNode.

A relationship is equally well traversed in both directions so there's no need to create another relationship in the opposite direction (in regards to traversal or performance).
[中]在该节点和另一个节点之间创建关系。关系的类型为type。它从该节点开始,在otherNode结束。
一个关系在两个方向上都被很好地遍历,因此不需要在相反方向上创建另一个关系(关于遍历或性能)。

代码示例

代码示例来源:origin: neo4j/neo4j

  1. @Override
  2. public Relationship create()
  3. {
  4. return db.createNode().createRelationshipTo( db.createNode(), type );
  5. }
  6. };

代码示例来源:origin: neo4j/neo4j

  1. @Override
  2. protected Relationship obtainEntityInTransaction( GraphDatabaseService graphDatabaseService )
  3. {
  4. return graphDatabaseService
  5. .createNode()
  6. .createRelationshipTo( graphDatabaseService.createNode(), withName( "foo" ) );
  7. }
  8. }

代码示例来源:origin: neo4j/neo4j

  1. @Override
  2. public Long apply( GraphDatabaseService graphDb )
  3. {
  4. try ( Transaction tx = graphDb.beginTx() )
  5. {
  6. Node node = graphDb.createNode();
  7. node.createRelationshipTo( graphDb.createNode(), withName( "KNOWS" ) );
  8. node.createRelationshipTo( graphDb.createNode(), withName( "KNOWS" ) );
  9. long whatThisThreadSees = countsForRelationship( null, null, null );
  10. barrier.reached();
  11. tx.success();
  12. return whatThisThreadSees;
  13. }
  14. }
  15. }, graphDb );

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void testFromRelCypherResult()
  3. {
  4. Node n = gdb.createNode();
  5. final Relationship rel = n.createRelationshipTo( n, RelationshipType.withName( "REL" ) );
  6. final ExecutionResult result = result( "rel", rel );
  7. final SubGraph graph = CypherResultSubGraph.from( result, gdb, true );
  8. assertEquals( "create (_0)" + lineSeparator() +
  9. "create (_0)-[:`REL`]->(_0)" + lineSeparator() + ";" + lineSeparator(), doExportGraph( graph ) );
  10. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void shouldOnlyReturnTypeOnce()
  3. {
  4. // Given
  5. Node node;
  6. try ( Transaction tx = db.beginTx() )
  7. {
  8. node = db.createNode();
  9. node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) );
  10. node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) );
  11. node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) );
  12. tx.success();
  13. }
  14. // Then
  15. try ( Transaction tx = db.beginTx() )
  16. {
  17. assertThat( Iterables.asList( node.getRelationshipTypes() ),
  18. equalTo( singletonList( RelationshipType.withName( "R" ) ) ) );
  19. }
  20. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void testGetRelationshipTypesOnDenseNode()
  3. {
  4. Node node = getGraphDb().createNode();
  5. Node otherNode = getGraphDb().createNode();
  6. for ( int i = 0; i < 300; i++ )
  7. {
  8. node.createRelationshipTo( otherNode, RelType.INITIAL );
  9. }
  10. testGetRelationshipTypes( node, new HashSet<>( asList( RelType.INITIAL.name() ) ) );
  11. }

代码示例来源:origin: neo4j/neo4j

  1. @Override
  2. protected Relationship create( Map<String, Object> properties )
  3. {
  4. assertEquals( value, properties.get( key ) );
  5. assertEquals( 1, properties.size() );
  6. return root.createRelationshipTo( graphDatabase().createNode(), type );
  7. }
  8. };

代码示例来源:origin: neo4j/neo4j

  1. private Relationship createRelationship( Node node )
  2. {
  3. try ( Transaction tx = node.getGraphDatabase().beginTx() )
  4. {
  5. Relationship rel = node.createRelationshipTo( node, MyRelTypes.TEST );
  6. tx.success();
  7. return rel;
  8. }
  9. }

代码示例来源:origin: neo4j/neo4j

  1. @Before
  2. public void setup()
  3. {
  4. db = (GraphDatabaseFacade) new TestGraphDatabaseFactory().newImpermanentDatabase();
  5. try ( Transaction tx = db.beginTx() )
  6. {
  7. Node node = db.createNode();
  8. node.createRelationshipTo( db.createNode(), withName( "a" ) );
  9. node.createRelationshipTo( db.createNode(), withName( "b" ) );
  10. relId = node.createRelationshipTo( db.createNode(), withName( "c" ) ).getId();
  11. tx.success();
  12. }
  13. }

代码示例来源:origin: neo4j/neo4j

  1. private Node createSomeData()
  2. {
  3. try ( Transaction tx = db.beginTx() )
  4. {
  5. Node node = db.createNode();
  6. node.createRelationshipTo( db.createNode(), MyRelTypes.TEST );
  7. node.createRelationshipTo( db.createNode(), MyRelTypes.TEST2 );
  8. tx.success();
  9. return node;
  10. }
  11. }
  12. }

代码示例来源:origin: neo4j/neo4j

  1. private void createRelationshipsBetweenNodes( Node source, Node sink,
  2. int numberOfRelationships )
  3. {
  4. for ( int i = 0; i < numberOfRelationships; i++ )
  5. {
  6. source.createRelationshipTo( sink, RelationshipType.withName( "Type" + (i % 4) ) )
  7. .setProperty( "" + i, i );
  8. }
  9. }
  10. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void should_remove_all_data()
  3. {
  4. try ( Transaction tx = db.beginTx() )
  5. {
  6. RelationshipType relationshipType = RelationshipType.withName( "R" );
  7. Node n1 = db.createNode();
  8. Node n2 = db.createNode();
  9. Node n3 = db.createNode();
  10. n1.createRelationshipTo(n2, relationshipType);
  11. n2.createRelationshipTo(n1, relationshipType);
  12. n3.createRelationshipTo(n1, relationshipType);
  13. tx.success();
  14. }
  15. cleanDatabaseContent( db );
  16. assertThat( nodeCount(), is( 0L ) );
  17. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void shouldNotGetTheSameRelationshipMoreThanOnceWhenAskingForTheSameTypeMultipleTimes()
  3. {
  4. // given
  5. Node node = getGraphDb().createNode();
  6. node.createRelationshipTo( getGraphDb().createNode(), withName( "FOO" ) );
  7. // when
  8. long relationships = Iterables.count( node.getRelationships( withName( "FOO" ), withName( "FOO" ) ) );
  9. // then
  10. assertEquals( 1, relationships );
  11. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void shouldPrintCypherEsqueRelationshipToString()
  3. {
  4. // GIVEN
  5. Node start;
  6. Node end;
  7. RelationshipType type = RelationshipType.withName( "NICE" );
  8. Relationship relationship;
  9. try ( Transaction tx = db.beginTx() )
  10. {
  11. // GIVEN
  12. start = db.createNode();
  13. end = db.createNode();
  14. relationship = start.createRelationshipTo( end, type );
  15. tx.success();
  16. // WHEN
  17. String toString = relationship.toString();
  18. // THEN
  19. assertEquals( "(" + start.getId() + ")-[" + type + "," + relationship.getId() + "]->(" + end.getId() + ")",
  20. toString );
  21. }
  22. }

代码示例来源:origin: neo4j/neo4j

  1. @Test
  2. public void canCreateRelationshipBetweenTwoNodesWithLoopsThenDeleteOneOfTheNodesAndItsRelationships()
  3. {
  4. Node source = getGraphDb().createNode();
  5. Node target = getGraphDb().createNode();
  6. source.createRelationshipTo( source, TEST );
  7. target.createRelationshipTo( target, TEST );
  8. source.createRelationshipTo( target, TEST );
  9. newTransaction();
  10. for ( Relationship rel : target.getRelationships() )
  11. {
  12. rel.delete();
  13. }
  14. target.delete();
  15. }

代码示例来源:origin: neo4j/neo4j

  1. @PluginTarget( Node.class )
  2. public Iterable<Relationship> createRelationships( @Source Node start,
  3. @Parameter( name = "type" ) RelationshipType type, @Parameter( name = "nodes" ) Iterable<Node> nodes )
  4. {
  5. List<Relationship> result = new ArrayList<>();
  6. try ( Transaction tx = start.getGraphDatabase().beginTx() )
  7. {
  8. for ( Node end : nodes )
  9. {
  10. result.add( start.createRelationshipTo( end, type ) );
  11. }
  12. tx.success();
  13. }
  14. return result;
  15. }

代码示例来源:origin: neo4j/neo4j

  1. private static void generateTransaction( GraphDatabaseAPI database )
  2. {
  3. try ( Transaction transaction = database.beginTx() )
  4. {
  5. Node startNode = database.createNode( Label.label( "startNode" ) );
  6. startNode.setProperty( "key", "value" );
  7. Node endNode = database.createNode( Label.label( "endNode" ) );
  8. endNode.setProperty( "key", "value" );
  9. startNode.createRelationshipTo( endNode, RelationshipType.withName( "connects" ) );
  10. transaction.success();
  11. }
  12. }

代码示例来源:origin: neo4j/neo4j

  1. private void createRelationshipsOnNode( GraphDatabaseService db, Node root, int numberOfRelationships )
  2. {
  3. for ( int i = 0; i < numberOfRelationships; i++ )
  4. {
  5. root.createRelationshipTo( db.createNode(), RelationshipType.withName( "Type" + (i % 4) ) )
  6. .setProperty( "" + i, i );
  7. }
  8. }

代码示例来源:origin: neo4j/neo4j

  1. @Override
  2. public Relationship create( Object... properties )
  3. {
  4. Relationship rel = graphDb.createNode().createRelationshipTo( graphDb.createNode(), TEST_TYPE );
  5. setProperties( rel, properties );
  6. return rel;
  7. }

代码示例来源:origin: neo4j/neo4j

  1. private static Function<Node,StartRelationship> loop( String type )
  2. {
  3. return node ->
  4. {
  5. RelationshipType relType = withName( type );
  6. return new StartRelationship(
  7. node.createRelationshipTo( node, relType ).getId(),
  8. Direction.BOTH,
  9. relType );
  10. };
  11. }
  12. }

相关文章