繁体   English   中英

与现有节点创建关系Cypher Neo4j

[英]Create relation with existing nodes Cypher Neo4j

我正在创建一个具有唯一Source标签的关系图。 我想知道这是我第一次创建节点后每次都要检查源或目标是否已经存在吗?

如果源存在但目标是新目标时使用合并,则合并无法创建节点。 我想创建以下图形。

我正在使用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获取json execResult = execEngine.execute(CQL); 在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'})

不太明白你的意思

f当源存在但目标是新源时我使用合并,然后合并无法创建节点。

MERGE应该以简约的方式使用。 如果找不到在MERGE指定的路径的单个项目,则会创建整个模式。 也就是说,如果您最终要创建起始节点,终止节点以及之间的使用关系

MERGE (source:Source{hashtag:'1'})
MERGE (target:Source{hashtag:'3'})
MERGE (source)-[:TIMELINE {weight: 30, date: "1417133400"}]->(target)

关于第二个问题:创建JSON

如果切换使用模式以运行Neo4j服务器并使用事务性Cypher端点 ,则可以直接获取JSON。

如果您想直接从Java代码呈现JSON,请使用常见的可疑对象,例如Google的GSON

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM