Cypher查询加载复杂的CSV文件

时间:2014-10-14 11:53:34

标签: cypher

我一直在尝试使用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有可能吗?命令似乎存在,但我不能让它们一起工作。

1 个答案:

答案 0 :(得分:0)

酷用例。

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

关于你的第二个问题:

  1. 我们正在考虑添加条件更新功能
  2. 有一种解决方法
  3. 保持你的陈述更简单将使他们更快
  4. 我目前正在撰写有关此内容的博客文章,目前尚未完成, 随便看看并应用提示: https://dl.dropboxusercontent.com/u/14493611/load_csv_with_success.adoc

    ESP。 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是一个表达。