sample.csv を cypher-shell で読んで、田中さんの友達グループ Tanaka_G.csvを出力する。
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 XXXXX -p XXXX
//sample.csvからノードを作成
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> load csv with headers from "file:///sample.csv" as row
match (p1:Person{ID:row.id})
match (p2:Person{ID:row.relation})
merge (p1)-[r:Know]->(p2);
0 rows available after 262 ms, consumed after another 0 ms
Created 6 relationshi
// 田中さんグループ Tanaka_G.csv への出力。[:Know]の'*'はrelation shipが続く限り全部。
neo4j@neo4j> with "match (a{NAME:'田中'})-[:Know*]->(b)
return b.NAME as name, b.ADD as address" as Friends
call apoc.export.csv.query(Friends, "Tanaka_G.csv",{})
yield file // これをしないと
return file; // returnが無いエラー
+----------------+
| file |
+----------------+
| "Tanaka_G.csv" |
+----------------+
1 row available after 21 ms, consumed after another 8 ms
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 XXXXX -p XXXX
//sample.csvからノードを作成
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> load csv with headers from "file:///sample.csv" as row
match (p1:Person{ID:row.id})
match (p2:Person{ID:row.relation})
merge (p1)-[r:Know]->(p2);
0 rows available after 262 ms, consumed after another 0 ms
Created 6 relationshi
// 田中さんグループ Tanaka_G.csv への出力。[:Know]の'*'はrelation shipが続く限り全部。
neo4j@neo4j> with "match (a{NAME:'田中'})-[:Know*]->(b)
return b.NAME as name, b.ADD as address" as Friends
call apoc.export.csv.query(Friends, "Tanaka_G.csv",{})
yield file // これをしないと
return file; // returnが無いエラー
+----------------+
| file |
+----------------+
| "Tanaka_G.csv" |
+----------------+
1 row available after 21 ms, consumed after another 8 ms
別のターミナルから出力ファイルを見てみる
neo4j@neo4j>:quit
$ sudo neo4j stop
$ cat Tanaka_G.csvneo4j@neo4j> match (n) detach delete n;
"name","address"
"山本","北広島"
"佐藤","久留米"
"田中","東広島"
neo4j@neo4j>:quit
$ sudo neo4j stop
---------------------
neo4jブラウザで同じことをやってみる。
// systemctlからneo4jを起動すると、起動表示はなくなる。
$ systemctl start neo4j
ブラウザから localhost:7474 Username:XXXXX Password:XXXXX
nao4j$ 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$ LOAD CSV WITH HEADERS FROM "file:///sample.csv" AS row
MATCH (p1:Person{ID:row.id})
MATCH (p2:Person{ID:row.relation})
MERGE (p1)-[r:Know]->(p2); // Ctrl+Enter
// 本題とはちがうけどGraph表示
// neo4j$ MATCH (p1)-[r:Know]->(p2) RETURN p1,r,p2;
neo4j$ MATCH (a{NAME:'田中'})-[r:Know*]->(b) RETURN b.NAME AS name, b.ADD AS address; // Enter
ここで、[ Export CSV ]をクリック
// 本題とはちがうけどGraph表示
// neo4j$ MATCH (a{NAME:'田中'})-[r:Know*]->(b) RETURN a,r,b;
ターミナルから出力ファイルを見てみる
$ cat export.csvneo4j$ MATCH (n) DETACH DELETE n;
name,address
山本,北広島
佐藤,久留米
田中,東広島
Clear local data
$ systemctl stop neo4j
以上、素人の個人的実験でした。