我正在尝试创建一个图表,用于存储节点之间基于时间的迭代。我希望节点是唯一的,并且给定时间戳属性,节点之间的关系是唯一的。
我的第一次尝试创造了2个节点和1个关系,这不是我想要的。
from py2neo import neo4j, node, rel
graph_db = neo4j.GraphDatabaseService()
graph_db.get_or_create_index(neo4j.Node, "node_index")
batch = neo4j.WriteBatch(graph_db)
# a TALKED_TO b at timestamp 0
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'})
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'})
batch.get_or_create_indexed_relationship('rel_index', 'type', 'TALKED_TO', 0, 'TALKED_TO', 1, {"timestamp": 0})
# a TALKED_TO b at timestamp 1
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'})
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'})
batch.get_or_create_indexed_relationship('rel_index', 'type', 'TALKED_TO', 3, 'TALKED_TO', 4, {"timestamp": 1})
# a TALKED_TO b at timestamp 2
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'})
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'})
batch.get_or_create_indexed_relationship('rel_index', 'type', 'TALKED_TO', 6, 'TALKED_TO', 7, {"timestamp": 0})
results = batch.submit()
print results
#[Node('http://localhost:7474/db/data/node/2'),
#Node('http://localhost:7474/db/data/node/3'),
#Relationship('http://localhost:7474/db/data/relationship/0'),
#Node('http://localhost:7474/db/data/node/2'),
#Node('http://localhost:7474/db/data/node/3'),
#Relationship('http://localhost:7474/db/data/relationship/0'),
#Node('http://localhost:7474/db/data/node/2'),
#Node('http://localhost:7474/db/data/node/3'),
#Relationship('http://localhost:7474/db/data/relationship/0')]
我的第二次尝试创建了2个节点和0个关系,不确定为什么它无法创建任何关系。
from py2neo import neo4j, node, rel
graph_db = neo4j.GraphDatabaseService()
graph_db.get_or_create_index(neo4j.Node, "node_index")
batch = neo4j.WriteBatch(graph_db)
# a TALKED_TO b at timestamp 0
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'})
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'})
batch.create(rel(0, 'TALKED_TO', 1, {"timestamp": 0}))
# a TALKED_TO b at timestamp 1
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'})
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'})
batch.create(rel(3, 'TALKED_TO', 4, {"timestamp": 1}))
# a TALKED_TO b at timestamp 2
batch.get_or_create_indexed_node('node_index', 'name', 'a', {'name': 'a'})
batch.get_or_create_indexed_node('node_index', 'name', 'b', {'name': 'b'})
batch.create(rel(6, 'TALKED_TO', 7, {"timestamp": 0}))
results = batch.submit()
print results
#[Node('http://localhost:7474/db/data/node/2'),
#Node('http://localhost:7474/db/data/node/3'),
#None]
那么我如何实现下图所示的内容?
答案 0 :(得分:0)
好的,所以我想我弄清楚了,但我不确定它是否有效。有谁知道比以下更好的方式?
# Create nodes a and b if they do not exist.
query = """MERGE (p:Person { name: {name} }) RETURN p"""
cypher_query = neo4j.CypherQuery(neo4j_graph, query )
result = cypher_query .execute(name='a')
result = cypher_query .execute(name='b')
# Create a relationship between a and b if it does not exist with the given timestamp value.
query = """
MATCH (a:Person {name: {a}}), (b:Person {name: {b}})
MERGE (a)-[r:TALKED_TO {timestamp: {timestamp}}]->(b)
RETURN r
"""
cypher_query = neo4j.CypherQuery(neo4j_graph, query)
result = cypher_query.execute(a='a', b='b', timestamp=0)
result = cypher_query.execute(a='a', b='b', timestamp=1)