我正在使用独特的Source标签创建关系图。我想知道每次在第一次创建节点后我必须检查源或目标是否已经存在?
如果我在源存在但目标是新的时使用Merge,则merge失败以创建节点。我想创建以下图表。
我正在使用以下代码使用JAVA
String CQL = "CREATE (source:Source {hashtag: '1'})-[:TIMELINE {weight: 3, date: '1417132800'}]->(target:Target {hashtag: '2'})";
ExecutionEngine execEngine = new ExecutionEngine(graphDb, StringLogger.DEV_NULL);
ExecutionResult execResult = execEngine.execute(CQL);
String results = execResult.dumpToString();
System.out.println(results);
其次请指导我如何从 ExecutionResult execResult = execEngine.execute(CQL); 获取json以在d3.js中创建地图
遵循CQL我必须运行。
CREATE CONSTRAINT ON (label:Source) ASSERT label.hashtag IS UNIQUE
// if source and target are new
CREATE (source:Source {hashtag: '1'})-[:TIMELINE {weight: 3, date: "1417132800"}]->(target:Target {hashtag: '2'})
// if source and target are already created and have another TIMELINE relation
MATCH (source:Source {hashtag: '1'}),(target:Target {hashtag: '2'}) CREATE (source)-[:TIMELINE {weight: 15, date: "1417132200"}]->(target)
// if source already exists but target is new
MATCH (source:Source {hashtag: '1'}) CREATE (source)-[:TIMELINE {weight: 20, date: "1417133200"}]->(target:Target {hashtag: '3'})
// if source is new but target already exists
MATCH (target:Target {hashtag: '2'}) CREATE (target)<-[:TIMELINE {weight: 30, date: "1417133400"}]-(source:Source {hashtag: '4'})
答案 0 :(得分:1)
不太明白你的意思
f当source存在但是target是new时,我使用Merge,然后merge无法创建节点。
MERGE
应该以简约的方式使用。如果未找到MERGE
中指定的路径的单个项目,则会创建整个模式。也就是说,如果你想最终创建起始节点,终端节点和使用之间的关系
MERGE (source:Source{hashtag:'1'})
MERGE (target:Source{hashtag:'3'})
MERGE (source)-[:TIMELINE {weight: 30, date: "1417133400"}]->(target)
如果您切换使用模式以运行Neo4j服务器并使用transactional Cypher endpoint,则直接返回JSON。
如果要直接从Java代码渲染JSON,请使用常见的嫌疑人,例如: Google's GSON。