【问题标题】:Cypher query loading complex CSV fileCypher 查询加载复杂的 CSV 文件
【发布时间】:2014-10-14 11:53:33
【问题描述】:

我一直在尝试使用 Cypher 加载一组 CSV 文件来构建 VMware 运行状态的视图。我能够执行以下查询:

LOAD CSV WITH HEADERS FROM 'file:///Users/rmorgan/Downloads/All_vm_hierrachy.csv' as csvline
with csvline
where csvline.type = 'VirtualMachine'
MERGE (host:HostSystem { name: csvline.hostid, description: csvline.hosttype })
MERGE (pool:ResourcePool { name: csvline.resourcePool})
MERGE (parent:Folder { name: csvline.parentid, description: csvline.parenttype })
MERGE (vm:VirtualMachine { name: csvline.moid, description: csvline.name })
CREATE (host)-[:HAS_VM]->(vm)
CREATE (vm)-[:HAS_PARENT]->(parent)
CREATE (vm)-[:HAS_RESOURCEPOOL]->(pool);

这会加载有关主机的详细信息,我找到匹配的主机对象,然后使用 SET 添加更多属性。

LOAD CSV WITH HEADERS FROM 'file:///Users/rmorgan/Downloads/All_vm_hierrachy.csv' as csvline
with csvline
where csvline.type = 'HostSystem' and csvline.parenttype = 'ClusterComputeResource'
match (h:HostSystem {name:csvline.moid})
set h.fullName = csvline.name
merge (p:ClusterComputeResource {name: csvline.parentid, type: csvline.parenttype})
CREATE (p)-[:HAS_CHILD]->(h);

两个查询都加载同一个文件,但实例化不同的对象。有没有办法将它们结合起来?我想在里面放一个 CASE 声明:

LOAD CSV WITH HEADERS FROM 'file:///Users/rmorgan/Downloads/All_vm_hierrachy.csv' as csvline
with csvline
CASE csvline.type = 'HostSystem' 
   // create some objects and add attributes
CASE csvline.type = 'VirtualMachine'
   // create some objects and add other attributes
END
// create some common objects / attributes irrespective of the path above

我还想嵌套这些以产生更复杂的执行路径

LOAD CSV WITH HEADERS FROM 'file:///Users/rmorgan/Downloads/All_vm_hierrachy.csv' as csvline
with csvline
CASE csvline.type = 'HostSystem' 
   // create host object
   CASE csvline.parenttype = 'ClusterComputeResource'
   // create ClusterComputeResource object, link to host
   CASE csvline.parenttype = 'ComputeResource'
   // create ComputeResource object, link to host
CASE csvline.type = 'VirtualMachine'
   // create some objects and add other attributes
END
// create some common objects / attributes irrespective of the path above

这在 Cypher 中可行吗?这些命令似乎存在,但我无法让它们一起工作。

【问题讨论】:

    标签: cypher


    【解决方案1】:

    很酷的用例。

    • 为您合并的唯一属性创建索引
      • 例如create index on :HostSystem(name);
    • 不要对多个属性使用合并,尤其是。进口时
      • 例如MERGE (host:HostSystem { name: csvline.hostid }) ON CREATE SET host.description= csvline.hosttype

    关于你的第二个问题:

    1. 我们正在考虑添加条件更新功能
    2. 有一种解决方法
    3. 让你的陈述更简单会让它们更快

    我目前正在写一篇关于此的博客文章,还没有完成, 随意查看并应用提示: https://dl.dropboxusercontent.com/u/14493611/load_csv_with_success.adoc

    特别是。如果您有 大量 数据,加载 Eager 的部分可能会影响您。

    解决方法是,使用 filter 和 foreach 的组合来创建“工作项”的单个或零元素集合

    LOAD CSV WITH HEADERS FROM 'file:///Users/rmorgan/Downloads/All_vm_hierrachy.csv' as csvline
    with csvline
    foreach (line in 
       filter(x in [csvline] where x.type = 'HostSystem' 
                               and x.parenttype = 'ClusterComputeResource') 
       | MERGE (h:HostSystem {name:line.moid}) 
         SET h.fullName = csvline.name
         MERGE (p:ClusterComputeResource {name: csvline.parentid}) 
           ON CREATE  SET p.type= csvline.parenttype
         CREATE (p)-[:HAS_CHILD]->(h)
    )
    

    CASE ... WHEN THEN ELSE END 是一个表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 2019-10-19
      相关资源
      最近更新 更多