org.neo4j.graphdb.Node类的使用及代码示例

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

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

Node介绍

[英]A node in the graph with properties and relationships to other entities. Along with Relationship, nodes are the core building blocks of the Neo4j data representation model. Nodes are created by invoking the GraphDatabaseService#createNode method.

Node has three major groups of operations: operations that deal with relationships, operations that deal with properties (see PropertyContainer) and operations that deal with Label.

The relationship operations provide a number of overloaded accessors (such as getRelationships(...) with "filters" for type, direction, etc), as well as the factory method #createRelationshipTo that connects two nodes with a relationship. It also includes the convenience method #getSingleRelationship for accessing the commonly occurring one-to-zero-or-one association.

The property operations give access to the key-value property pairs. Property keys are always strings. Valid property value types are all the Java primitives (int, byte, float, etc), java.lang.Strings and arrays of primitives and Strings.

Please note that Neo4j does NOT accept arbitrary objects as property values. #setProperty(String,Object) takes a java.lang.Object only to avoid an explosion of overloaded setProperty() methods. For further documentation see PropertyContainer.

A node's id is unique, but note the following: Neo4j reuses its internal ids when nodes and relationships are deleted, which means it's bad practice to refer to them this way. Instead, use application generated ids.
[中]图中具有属性和与其他实体的关系的节点。除了关系之外,节点也是Neo4j数据表示模型的核心构建块。通过调用GraphDatabaseService#createNode方法创建节点。
节点有三个主要的操作组:处理关系的操作、处理属性的操作(请参见PropertyContainer)和处理标签的操作。
关系操作提供了大量重载访问器(例如getRelationships(...)和类型、方向等的“过滤器”),以及工厂方法#createRelationshipTo,该方法通过关系连接两个节点。它还包括方便的方法#getSingleRelationship,用于访问常见的一对零或一关联。
属性操作允许访问键值属性对。属性键始终是字符串。有效的属性值类型是所有Java原语(intbytefloat等)、java.lang.String以及原语和字符串数组。
请注意,Neo4j不接受任意对象作为属性值#setProperty(String,Object)只接受java.lang.Object以避免重载setProperty()方法爆炸。有关更多文档,请参阅PropertyContainer。
节点的id是唯一的,但请注意:当删除节点和关系时,Neo4j重用其内部id,这意味着以这种方式引用它们是不好的做法。相反,使用应用程序生成的ID。

代码示例

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

private long createNodeWithProperty( String propertyKey, Object value )
{
  Node node;
  try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
  {
    node = graphDb.createNode();
    node.setProperty( propertyKey, value );
    ctx.success();
  }
  return node.getId();
}

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

@Test
public void testRandomPropertyName()
{
  Node node1 = getGraphDb().createNode();
  String key = "random_"
    + new Random( System.currentTimeMillis() ).nextLong();
  node1.setProperty( key, "value" );
  assertEquals( "value", node1.getProperty( key ) );
  node1.delete();
}

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

private static void bareStartAndEnd( GraphDatabaseService graphDb )
{
  try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() )
  {
    bare = graphDb.createNode().getId();
    Node x = graphDb.createNode(), y = graphDb.createNode();
    start = x.getId();
    end = y.getId();
    x.createRelationshipTo( y, withName( "GEN" ) );
    tx.success();
  }
}

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

private static void delete( Node node )
{
  for ( Relationship rel : node.getRelationships() )
  {
    rel.delete();
  }
  node.delete();
}

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

private long createTheThirdNode()
{
  long secondNodeId;
  try ( Transaction transaction = db.beginTx() )
  {
    Node hej = db.createNode( label( "hej" ) );
    secondNodeId = hej.getId();
    hej.setProperty( "hej", "villa" );
    hej.setProperty( "ho", "value3" );
    transaction.success();
  }
  return secondNodeId;
}

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

private void createData( GraphDatabaseService database, int numberOfNodes )
{
  for ( int i = 0; i < numberOfNodes; i++ )
  {
    try ( Transaction transaction = database.beginTx() )
    {
      Node node = database.createNode( Label.label( FOOD_LABEL ), Label.label( CLOTHES_LABEL ),
          Label.label( WEATHER_LABEL ) );
      node.setProperty( PROPERTY_NAME, "Node" + i );
      Relationship relationship = node.createRelationshipTo( node, RelationshipType.withName( FOOD_LABEL ) );
      relationship.setProperty( PROPERTY_NAME, "Relationship" + i );
      transaction.success();
    }
  }
}

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

private long initWithRel( GraphDatabaseService db )
{
  try ( Transaction tx = db.beginTx() )
  {
    Node node = db.createNode();
    node.setProperty( "a", "prop" );
    Relationship rel = node.createRelationshipTo( db.createNode(), RelationshipType.withName( "T" ) );
    long id = rel.getId();
    tx.success();
    return id;
  }
}

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

private long addRelationshipToExplicitIndex()
  {
    long relId;
    try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() )
    {
      Relationship rel =
          graphDb.createNode().createRelationshipTo( graphDb.createNode(), RelationshipType.withName( "R" ) );
      relId = rel
          .getId();
      graphDb.index().forRelationships( INDEX_NAME ).add( rel, KEY, VALUE );
      ctx.success();
    }
    return relId;
  }
}

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

@Test
public void testRollbackDeleteRelationship()
{
  Node node1 = getGraphDb().createNode();
  Node node2 = getGraphDb().createNode();
  Relationship rel1 = node1.createRelationshipTo( node2, TEST );
  newTransaction();
  node1.delete();
  rel1.delete();
  getTransaction().failure();
  getTransaction().close();
  setTransaction( getGraphDb().beginTx() );
  node1.delete();
  node2.delete();
  rel1.delete();
}

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

@Before
public void setup()
{
  db = (GraphDatabaseFacade) new TestGraphDatabaseFactory().newImpermanentDatabase();
  try ( Transaction tx = db.beginTx() )
  {
    Node node = db.createNode();
    node.createRelationshipTo( db.createNode(), withName( "a" ) );
    node.createRelationshipTo( db.createNode(), withName( "b" ) );
    relId = node.createRelationshipTo( db.createNode(), withName( "c" ) ).getId();
    tx.success();
  }
}

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

private static void generateTransaction( GraphDatabaseAPI database )
{
  try ( Transaction transaction = database.beginTx() )
  {
    Node startNode = database.createNode( Label.label( "startNode" ) );
    startNode.setProperty( "key", "value" );
    Node endNode = database.createNode( Label.label( "endNode" ) );
    endNode.setProperty( "key", "value" );
    startNode.createRelationshipTo( endNode, RelationshipType.withName( "connects" ) );
    transaction.success();
  }
}

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

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

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

private void deleteNode( long nodeId )
{
  try ( Transaction tx = db.beginTx() )
  {
    db.getNodeById( nodeId ).delete();
    tx.success();
  }
}

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

private long ensurePropertyIsCachedLazyProperty( GraphDatabaseAPI slave, String key )
{
  long nId;
  try ( Transaction tx = slave.beginTx() )
  {
    Node n = slave.createNode();
    nId = n.getId();
    n.setProperty( key, new long[]{-1, 2, 2, 3, 4, 5, 5} );
    tx.success();
  }
  try ( Transaction tx = slave.beginTx() )
  {
    slave.getNodeById( nId ).hasProperty( key );
    tx.success();
  }
  return nId;
}

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

@Name( GET_CONNECTED_NODES )
@PluginTarget( Node.class )
public Iterable<Node> getAllConnectedNodes( @Source Node start )
{
  ArrayList<Node> nodes = new ArrayList<>();
  try ( Transaction tx = start.getGraphDatabase().beginTx() )
  {
    for ( Relationship rel : start.getRelationships() )
    {
      nodes.add( rel.getOtherNode( start ) );
    }
    tx.success();
  }
  return nodes;
}

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

private long createNode( Label label, int number )
{
  try ( Transaction tx = db.beginTx() )
  {
    Node node = db.createNode( label );
    node.setProperty( NUM_BANANAS_KEY, number );
    tx.success();
    return node.getId();
  }
}

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

private static Function<Node,StartRelationship> outgoing( String type )
{
  return node ->
  {
    GraphDatabaseService db = node.getGraphDatabase();
    RelationshipType relType = withName( type );
    return new StartRelationship(
        node.createRelationshipTo( db.createNode(), relType ).getId(),
        Direction.OUTGOING,
        relType );
  };
}

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

@Test
public void testPointTypeWithOneOtherProperty()
{
  Point point = Values.pointValue( CoordinateReferenceSystem.Cartesian, 1, 1 );
  String key = "location";
  node1.setProperty( "prop1", 1 );
  node1.setProperty( key, point );
  newTransaction();
  Object property = node1.getProperty( key );
  assertEquals( point, property );
}

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

@Test
public void testBooleanType()
{
  String key = "testbool";
  node1.setProperty( key, Boolean.TRUE );
  newTransaction();
  Boolean propertyValue = (Boolean) node1.getProperty( key );
  assertEquals( Boolean.TRUE, propertyValue );
  node1.setProperty( key, Boolean.FALSE );
  newTransaction();
  propertyValue = (Boolean) node1.getProperty( key );
  assertEquals( Boolean.FALSE, propertyValue );
  node1.removeProperty( key );
  newTransaction();
  assertTrue( !node1.hasProperty( key ) );
}

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

private void assertCanCreateAndFind( GraphDatabaseService db, Label label, String propertyKey, Object value )
{
  Node created = createNode( db, map( propertyKey, value ), label );
  try ( Transaction tx = db.beginTx() )
  {
    Node found = db.findNode( label, propertyKey, value );
    assertThat( found, equalTo( created ) );
    found.delete();
    tx.success();
  }
}

相关文章