我一直在尝试使用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有可能吗?命令似乎存在,但我不能让它们一起工作。
答案 0 :(得分:0)
酷用例。
create index on :HostSystem(name);
MERGE (host:HostSystem { name: csvline.hostid }) ON CREATE SET host.description= csvline.hosttype
关于你的第二个问题:
我目前正在撰写有关此内容的博客文章,目前尚未完成, 随便看看并应用提示: 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
是一个表达。