sample.csv を cypher-shell で読んで、ADD(住所)が'広島'の人の名前を result.csvに出力する。
sample.csv
id,name,add,job,relation
a001,田中,東広島,販売,a002
a002,山本,北広島,販売,a003
a003,佐藤,久留米,飲食,a001
a004,山崎,,建設,a002
a005,村上,東久留米,販売,a002
a006,広田,南大東島,醸造,a001
sample.csv
id,name,add,job,relation
a001,田中,東広島,販売,a002
a002,山本,北広島,販売,a003
a003,佐藤,久留米,飲食,a001
a004,山崎,,建設,a002
a005,村上,東久留米,販売,a002
a006,広田,南大東島,醸造,a001
$ sudo neo4j start
$ cypher-shell -u neo4j -p XXXX
$ cypher-shell -u neo4j -p XXXX
// coalesce(a,"b") は、セルがaならaを返す。なければ文字列bを返す
neo4j@neo4j> LOAD CSV WITH HEADERS FROM 'file:///sample.csv' AS row
MERGE (p:Person{ID:coalesce(row.id,"Unknown"), NAME:coalesce(row.name,"Unknown"),ADD:coalesce(row.add,"Unknown"), JOB:coalesce(row.job,"Unknown"), REL:coalesce(row.relation,"Unknown")});
neo4j@neo4j> call apoc.export.csv.query("match (a:Person) where a.ADD contains '広島' return a.NAME, a.ADD", "result.csv", {});
+-------------------------------------------------------------------------------------------------------------------------------------+
| file | source | format | nodes | relationships | properties | time | rows | batchSize | batches | done | data |
+-------------------------------------------------------------------------------------------------------------------------------------+
| "result.csv" | "statement: cols(2)" | "csv" | 0 | 0 | 4 | 1 | 2 | 20000 | 1 | TRUE | NULL |
+-------------------------------------------------------------------------------------------------------------------------------------+
1 row available after 23 ms, consumed after another 63 ms
別ターミナルからファイルを見てみる$ cat /var/lib/neo4j/import/result.csv
"a.NAME","a.ADD"
"田中","東広島"
"山本","北広島"
neo4j@neo4j> match (n) detach delete n;
neo4j@neo4j>:quit
$ sudo neo4j stop // 終了
neo4j@neo4j>:quit
$ sudo neo4j stop // 終了
===========================================
同じ例を neo4jブラウザでやってみる
$ sudo neo4j start
ブラウザ localhost:7474 Username: XXXXX Pssword:XXXXXX
neo4j$ LOAD CSV WITH HEADERS FROM 'file:///sample.csv' AS row
MERGE (p:Person{ID:coalesce(row.id,"Unknown"), NAME:coalesce(row.name,"Unknown"),ADD:coalesce(row.add,"Unknown"), JOB:coalesce(row.job,"Unknown"), REL:coalesce(row.relation,"Unknown")}); //Ctrl+Enter
neo4j$ MATCH (a:Person) WHERE a.ADD CONTAINS '広島' RETURN a.NAME, a.ADD; //Enter
$ sudo neo4j start
ブラウザ localhost:7474 Username: XXXXX Pssword:XXXXXX
neo4j$ LOAD CSV WITH HEADERS FROM 'file:///sample.csv' AS row
MERGE (p:Person{ID:coalesce(row.id,"Unknown"), NAME:coalesce(row.name,"Unknown"),ADD:coalesce(row.add,"Unknown"), JOB:coalesce(row.job,"Unknown"), REL:coalesce(row.relation,"Unknown")}); //Ctrl+Enter
neo4j$ MATCH (a:Person) WHERE a.ADD CONTAINS '広島' RETURN a.NAME, a.ADD; //Enter
上のバーから Expor CSV をクリック
出力をターミナルから見てみる$ cat export.csv
a.NAME,a.ADD
田中,東広島
山本,北広島
$ MATCH (n) DETACH DELETE n; してneo4j 左の赤っぽいアイコンで終了。$ sudo neo4j stop
===========================
結局、文字列指定のダブルクォートの差以外は、同じ結果でした。
以上、まるっきり素人の私には、いろいろ覚えなくてもいい方を選ぶ。
環境 Debian 10, neo4j 4.2.7 community 版, Cypher-Shell 4.2.7