{"id":293,"date":"2020-03-23T10:30:56","date_gmt":"2020-03-23T01:30:56","guid":{"rendered":"http:\/\/localhost:8000\/?p=293"},"modified":"2021-01-17T10:34:10","modified_gmt":"2021-01-17T01:34:10","slug":"neo4j-csv-load","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/03\/neo4j-csv-load.html","title":{"rendered":"Neo4j\u306bCSV\u30ed\u30fc\u30c9\u3059\u308b4\u3064\u306e\u65b9\u6cd5"},"content":{"rendered":"
Neo4j\u306bCSV\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5\u306f\u5c11\u306a\u304f\u3068\u3082\uff14\u3064\u306f\u3042\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n
\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3053\u306e4\u3064\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u3001python\u304b\u3089\u5b9f\u884c\u3059\u308b\u5177\u4f53\u7684\u306a\u5b9f\u88c5\u3092\u8aac\u660e\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
\u30b5\u30f3\u30d7\u30eb\u5b9f\u88c5\u3067\u30ed\u30fc\u30c9\u3059\u308b\u30c7\u30fc\u30bf\u306f\u3053\u3061\u3089\u3067\u3059\u3002\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3 \u4eca\u56de\u306f\u3001docker-hub<\/a>\u306e CSV\u30a4\u30f3\u30dd\u30fc\u30c8\u306f\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u3067\u5b9f\u884c\u3055\u308c\u308b\u305f\u3081\u3001\u30ed\u30fc\u30c9\u7528\u306eCSV\u30d5\u30a1\u30a4\u30eb\u306fHTTP\u3067\u63d0\u4f9b\u3059\u308b\u304b\u3001\u30b5\u30fc\u30d0\u306b\u914d\u7f6e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u4eca\u56de\u306f\u30b5\u30fc\u30d0\u306b\u914d\u7f6e\u3059\u308b\u5f62\u5f0f\u306a\u306e\u3067\u3001\u30db\u30b9\u30c8\u30de\u30b7\u30f3\u5074\u306e\u30d5\u30a9\u30eb\u30c0\u3092docker\u30b3\u30f3\u30c6\u30ca\u5074\u306e \u516c\u5f0f\u30b5\u30a4\u30c8\u306f\u3053\u3061\u3089<\/a><\/p>\n \u5b9f\u969b\u307f\u308b\u3068\u30ea\u30ec\u30fc\u30b7\u30e7\u30f310\u4e07\u4ef6\u306730\u5206\u304b\u304b\u3063\u305f\u306e\u3067\u30011\u4e07\u4ef6\u884c\u304b\u306a\u3044\u3050\u3089\u3044\u306e\u30c7\u30fc\u30bf\u91cf\u3067\u6c17\u8efd\u306bCSV\u30ed\u30fc\u30c9\u3057\u305f\u3044\u306a\u3089\u3053\u306e\u65b9\u5f0f\u304c\u3044\u3044\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u601d\u3044\u307e\u3057\u305f\u3002<\/p>\n \u4f4e\u901f\u3067\u306f\u3042\u308b\u3082\u306e\u306e\u3001\u7279\u5225\u306a\u914d\u616e\u306a\u304f\u901a\u5e38\u306eCypher\u30af\u30a8\u30ea\u3068\u540c\u69d8\u306b\u30ed\u30fc\u30c9\u3067\u304d\u307e\u3059\u306e\u3067\u3001\u4e00\u756a\u6c17\u697d\u306b\u5229\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n \u666e\u901a\u306eCSV\u306eformat\u3067\u3059\u3002<\/p>\n nodes.csv<\/p>\n relations.csv<\/p>\n \u5c11\u3057\u3067\u3082\u9ad8\u901f\u5316\u3059\u308b\u305f\u3081\u306b\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u305b\u305a\u3001 \u516c\u5f0f\u30b5\u30a4\u30c8\u306f\u3053\u3061\u3089<\/a><\/p>\n \u516c\u5f0f\u30b5\u30a4\u30c8\u306f\u3053\u3061\u3089<\/a><\/p>\n \u65e2\u5b58\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u5c0f\u898f\u6a21\u304b\u3089\u4e2d\u898f\u6a21\u306e\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3057\u305f\u3044\u5834\u5408\u3001\u3053\u3061\u3089\u3092\u5229\u7528\u3059\u308b\u3068\u826f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u5b9f\u969b\u306b\u3084\u3063\u3066\u307f\u305f\u3068\u3053\u308d\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3100\u4e07\u4ef6\u306750\u79d2\u3060\u3063\u305f\u306e\u3067\u30011000\u4e07\u4ef6\u3050\u3089\u3044\u307e\u3067\u3060\u3063\u305f\u3089\u5341\u5206\u5b9f\u7528\u7684\u3067\u306f\u306a\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n nodes.csv<\/p>\n relations.csv<\/p>\n \u30ce\u30fc\u30c9\u306e \u516c\u5f0f\u30b5\u30a4\u30c8\u306f\u3053\u3061\u3089<\/a><\/p>\n \u4eca\u56de\u306f\u5927\u91cf\u30c7\u30fc\u30bf\u3092\u60f3\u5b9a\u3057\u3066\u3001\u8907\u6570CSV\u30d5\u30a1\u30a4\u30eb\u306b\u5225\u308c\u3066\u3001CSV\u306e\u30d8\u30c3\u30c0\u30fc\u3092\u5225\u30d5\u30a1\u30a4\u30eb\u3067\u6307\u5b9a\u3059\u308b\u30b1\u30fc\u30b9\u3092\u60f3\u5b9a\u3057\u307e\u3059\u3002\u30d5\u30a1\u30a4\u30eb\u3092\u5206\u5272\u3057\u3066\u308b\u3060\u3051\u3067\u3001\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u306f nodes_header.csv<\/p>\n nodes_data_01.csv<\/p>\n nodes_data_02.csv<\/p>\n relations_header.csv<\/p>\n relations_data_01.csv<\/p>\n relations_data_02.csv<\/p>\n \u3044\u304f\u3064\u304b\u30dd\u30a4\u30f3\u30c8\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n python\u306e\u5b9f\u884c\u30ed\u30b0<\/p>\n docker-compose \u30ed\u30b0 Neo4j\u306bCSV\u30ed\u30fc\u30c9\u3059\u308b\u65b9\u6cd5\u306f\u5c11\u306a\u304f\u3068\u3082\uff14\u3064\u306f\u3042\u308b\u3088\u3046\u3067\u3059\u3002 CSV LOAD apoc.load.csv apoc.import.csv neo4j-admin import \u3053\u306e\u8a18\u4e8b\u3067\u306f\u3053\u306e4\u3064\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u3001python\u304b\u3089\u5b9f\u884c\u3059\u308b\u5177\u4f53\u7684\u306a\u5b9f\u88c5\u3092\u8aac\u660e\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u30ed\u30fc\u30c9\u3059\u308b\u30c7\u30fc\u30bf \u30b5\u30f3\u30d7\u30eb\u5b9f\u88c5\u3067\u30ed\u30fc\u30c9\u3059\u308b\u30c7\u30fc\u30bf\u306f\u3053\u3061\u3089\u3067\u3059\u3002\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3LINKED\u306b\u306fscore\u3068\u3044\u3046\u30ea\u30f3\u30af\u306e\u5f37\u3055\u3092\u8868\u3059\u30d7\u30ed\u30d1\u30c6\u30a3\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002 \u691c\u8a3c\u74b0\u5883 \u4eca\u56de\u306f\u3001docker-hub\u306eneo4j:latest\u30a4\u30e1\u30fc\u30b8\u3067\u3001docker-compose\u3067\u691c\u8a3c\u3057\u307e\u3059\u3002docker-compose.yaml\u306e\u8a2d\u5b9a\u306f\u57fa\u672c\u7684\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u3002 version: '3' services: neo4j: image: neo4j:latest ports: – "7474:747 <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31,7],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/293"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=293"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":294,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/293\/revisions\/294"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}LINKED<\/code>\u306b\u306fscore\u3068\u3044\u3046\u30ea\u30f3\u30af\u306e\u5f37\u3055\u3092\u8868\u3059\u30d7\u30ed\u30d1\u30c6\u30a3\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u307e\u3059\u3002
\n<\/p>\n\u691c\u8a3c\u74b0\u5883<\/h2>\n
neo4j:latest<\/code>\u30a4\u30e1\u30fc\u30b8\u3067\u3001docker-compose\u3067\u691c\u8a3c\u3057\u307e\u3059\u3002docker-compose.yaml\u306e\u8a2d\u5b9a\u306f\u57fa\u672c\u7684\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n
version: '3'\n\nservices:\n neo4j:\n image: neo4j:latest\n ports:\n - "7474:7474" # \u7ba1\u7406\u753b\u9762\u7528\n - "7687:7687" # bolt\u7528\n volumes:\n - ${HOME}\/neo4j\/data:\/data\n - ${HOME}\/neo4j\/logs:\/logs\n - ${HOME}\/neo4j\/conf:\/conf\n - ${HOME}\/neo4j\/import:\/import\n\n environment:\n - NEO4J_AUTH=neo4j\/password\n # admin memrec\u306b\u5f93\u3063\u3066\u8a2d\u5b9a\n - NEO4J_dbms_memory_heap_max__size=4G\n - NEO4J_dbms_memory_heap_initial__size=4G\n - NEO4J_dbms_memory_pagecache_size=454900k\n - NEO4J_dbms_tx__state_max__off__heap__memory=2500m\n # APOC\u95a2\u4fc2\n - NEO4JLABS_PLUGINS=["apoc"]\n - NEO4J_apoc_import_file_use__neo4j__config=true # import\u6642\u306b\/import\u30d5\u30a9\u30eb\u30c0\u304b\u3089\u306e\u76f8\u5bfe\u30d1\u30b9\u3092\u6307\u5b9a\n - NEO4J_apoc_import_file_enabled=true<\/code><\/pre>\n
\/import<\/code>\u30d5\u30a9\u30eb\u30c0\u306b\u30de\u30a6\u30f3\u30c8\u3057\u3066\u3001\u305d\u3053\u306bCSV\u30d5\u30a1\u30a4\u30eb\u3092\u914d\u7f6e\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
\u65b9\u6cd51: CSV LOAD<\/h2>\n
\u7279\u5fb4<\/h3>\n
\n
USING PERIODIC COMMIT<\/code>\u3092\u4f7f\u3063\u3066\u4e00\u62ec\u30b3\u30df\u30c3\u30c8\u3059\u308b\u3053\u3068\u3067\u3001\u3042\u308b\u7a0b\u5ea6\u306e\u901f\u5ea6\u306f\u51fa\u308b\u3002<\/li>\n
MATCH<\/code>\u3055\u305b\u3066\u304b\u3089
CREATE<\/code>\u3059\u308b<\/li>\n<\/ul>\n
CSV<\/h3>\n
\n
\nid\n1\n2\n3\n4\n5\n6\n7\n8\n9\n<\/pre>\n<\/p>\n<\/li>\n
\nfrom_id,to_id,score\n1,2,0.412\n1,3,0.623\n3,4,0.512\n4,5,0.79\n6,7,0.31\n7,8,0.79\n<\/pre>\n<\/p>\n<\/li>\n<\/ul>\n
python\u5b9f\u88c5<\/h3>\n
from neo4j import GraphDatabase, Driver\n\ndriver: Driver = GraphDatabase.driver('bolt:\/\/localhost:7687', auth=('neo4j', 'password'), encrypted=False)\nwith driver.session() as session:\n # \u30ce\u30fc\u30c9\u7528CSV\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\n nodes_query: str = """\n USING PERIODIC COMMIT 10000\n LOAD CSV WITH HEADERS FROM $csv_path AS line\n CREATE (:Node {id:toInteger(line.id)})\n """\n session.run(nodes_query, csv_path='file:\/\/\/nodes.csv')\n\n # \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u7528CSV\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\n relations_query: str = """\n USING PERIODIC COMMIT 10000\n LOAD CSV WITH HEADERS FROM $csv_path AS line\n MATCH (nf:Node {id:toInteger(line.from_id)}), (nt:Node {id:toInteger(line.to_id)})\n CREATE (nf)-[:LINKED {score: toFloat(line.score)}]->(nt)\n """\n session.run(relations_query, csv_path='file:\/\/\/relations.csv')<\/code><\/pre>\n
USING PERIODIC COMMIT 10000<\/code>\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002\u307e\u305f\u3001CSV\u30c7\u30fc\u30bf\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u6587\u5b57\u5217\u6271\u3044\u306b\u306a\u308b\u305f\u3081\u3001
toInteger<\/code>\u3084
toFloat<\/code>\u3067\u76ee\u7684\u306e\u578b\u306b\u5909\u63db\u3057\u3066\u3042\u3052\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002 <\/p>\n
\u65b9\u6cd52: apoc.load.csv<\/h2>\n
LOAD CSV<\/code>\u540c\u69d8\u306b\u3001\u30c7\u30fc\u30bf\u91cf\u304c\u5c11\u306a\u304f
LOAD CSV<\/code>\u3067\u63d0\u4f9b\u3055\u308c\u3066\u306a\u3044\u6a5f\u80fd\uff08\u4f8b\u3048\u3070\u884c\u756a\u53f7\u3092ID\u306b\u8a2d\u5b9a\u3057\u305f\u3044\u3001\u3068\u304b\uff09\u306e\u5834\u5408\u306b\u5229\u7528\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
APOC<\/h3>\n
apoc<\/code>\u306f\u3001Neo4j\u672c\u4f53\u3067\u5b9f\u88c5\u3055\u308c\u3066\u3044\u306a\u3044\u3001\u30c7\u30fc\u30bf\u30a4\u30f3\u30c6\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3001\u30b0\u30e9\u30d5\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3001\u30c7\u30fc\u30bf\u5909\u63db\u306a\u3069\u306e\u30d7\u30ed\u30b7\u30fc\u30b8\u30e3\u3084\u95a2\u6570\u3092\u63d0\u4f9b\u3057\u3066\u304f\u308c\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002
\n\u4eca\u56de\u306f\u3001docker-compose\u3067\u691c\u8a3c\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u5229\u7528\u306e\u305f\u3081\u306b\u306fdocker-compose.yaml<\/code>\u3067\u74b0\u5883\u5909\u6570\u306e\u8a2d\u5b9a\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
\u7279\u5fb4<\/h3>\n
\n
LOAD CSV<\/code>\u3068\u540c\u69d8\u3060\u304c\u3001\u8272\u3005\u306a\u6a5f\u80fd\u304c\u8ffd\u52a0\u3067\u8ffd\u52a0\u3067\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\n
\n
apoc.periodic.iterate<\/code>\u3067
batchSize<\/code>\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001
LAOD CSV<\/code>\u3068\u540c\u69d8\u306b\u4e00\u62ec\u30b3\u30df\u30c3\u30c8\u3059\u308b\u3053\u3068\u3067\u9ad8\u901f\u5316\u3067\u304d\u308b\u3002\u8a73\u7d30\u306f\u3053\u3061\u3089<\/a><\/li>\n
MATCH<\/code>\u3055\u305b\u3066\u304b\u3089
CREATE<\/code>\u3059\u308b<\/li>\n<\/ul>\n
CSV<\/h3>\n
LOAD CSV<\/code>\u3068\u540c\u69d8\u3067\u3059\u3002<\/p>\n
python\u5b9f\u88c5<\/h3>\n
from neo4j import GraphDatabase, Driver\n\ndriver: Driver = GraphDatabase.driver('bolt:\/\/localhost:7687', auth=('neo4j', 'password'), encrypted=False)\n\nwith driver.session() as session:\n nodes_query: str = """\n CALL apoc.load.csv($csv_path) yield map as line\n CREATE (:Node {id:toInteger(line.id)})\n """\n session.run(nodes_query, csv_path='file:\/\/\/nodes.csv')\n\n # \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u7528CSV\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\n relations_query: str = """\n CALL apoc.load.csv($csv_path) yield map as line\n MATCH (nf:Node {id:toInteger(line.from_id)}), (nt:Node {id:toInteger(line.to_id)})\n CREATE (nf)-[:LINKED {score: toFloat(line.score)}]->(nt)\n """\n session.run(relations_query, csv_path='file:\/\/\/relations.csv')\n\n # \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u7528CSV\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\uff08\u30d0\u30c3\u30c1\u30a4\u30f3\u30b5\u30fc\u30c8\u7248\uff09\n relations_batch_query: str = """\n CALL apoc.periodic.iterate('\n CALL apoc.load.csv($path) yield map as line\n ', '\n MATCH (nf:Node {id:toInteger(line.from_id)}), (nt:Node {id:toInteger(line.to_id)})\n CREATE (nf)-[:LINKED {score: toFloat(line.score)}]->(nt)\n ', {batchSize:10000, iterateList:true, parallel:false, params:{path:$csv_path}});\n """\n session.run(relations_batch_query, csv_path='file:\/\/\/relations.csv') <\/code><\/pre>\n
\u65b9\u6cd53: apoc.import.csv<\/h2>\n
\u7279\u5fb4<\/h3>\n
\n
CSV<\/h3>\n
CSV LOAD<\/code>\u306eCSV\u30d5\u30a1\u30a4\u30eb\u3068\u3001\u30d8\u30c3\u30c0\u30fc\u306e\u8a18\u8f09\u65b9\u6cd5\u3060\u3051\u5909\u308f\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n
\n
\n:ID\n1\n2\n3\n4\n5\n6\n7\n8\n9\n<\/pre>\n<\/p>\n<\/li>\n
\nNode:START_ID,Node:END_ID,score:float\n1,2,0.412\n1,3,0.623\n3,4,0.512\n4,5,0.79\n6,7,0.31\n7,8,0.79\n<\/pre>\n<\/p>\n<\/li>\n<\/ul>\n
python\u5b9f\u88c5<\/h3>\n
LABEL<\/code>\u3084\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e
Type<\/code>\u306fprocedure\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3068\u3057\u3066\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n
from neo4j import GraphDatabase, Driver\n\ndriver: Driver = GraphDatabase.driver('bolt:\/\/localhost:7687', auth=('neo4j', 'password'), encrypted=False)\n\nwith driver.session() as session:\n query: str = """\n CALL apoc.import.csv(\n [{fileName: $node_csv_path, labels: ['Node']}],\n [{fileName: $relation_csv_path, type: 'LINKED'}],\n {}\n )\n """\n session.run(query, node_csv_path=f'file:\/\/\/nodes.csv', relation_csv_path=f'file:\/\/\/relations.csv')<\/code><\/pre>\n
\u65b9\u6cd54: neo4j-admin import<\/h2>\n
neo4j-admin<\/code>\u306e\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u30c4\u30fc\u30eb\u306e\u300c
import<\/code>\u30b3\u30de\u30f3\u30c9\u3092\u5229\u7528\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002\u7a7a\u306eDB\u306b\u3001\u5104\u3092\u8d85\u3048\u308b\u5927\u91cf\u30c7\u30fc\u30bf\u3092\u30ed\u30fc\u30c9\u3059\u308b\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3067\u5229\u7528\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002
\n\u5b9f\u969b\u306b\u3084\u3063\u3066\u307f\u305f\u3068\u3053\u308d\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f33\u5104\u4ef6\u306760\u5206\u307b\u3069\u3067\u3057\u305f\u3002<\/p>\n\u7279\u5fb4<\/h3>\n
\n
\n
\u51e6\u7406\u306e\u6d41\u308c<\/h3>\n
neo4j-admin<\/code>\u306fCLI\u30c4\u30fc\u30eb\u3067\u3001DB\u30b5\u30fc\u30d0\u5185\u3067\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001python\u304b\u3089\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u7d50\u69cb\u30c8\u30ea\u30c3\u30ad\u30fc\u306a\u6d41\u308c\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n
\n
subprocess.call<\/code>\u3067
docker-compose up<\/code>\u3092\u5b9f\u884c\u3059\u308b<\/li>\n
EXTENSION_SCRIPT<\/code>\u3067\u6307\u5b9a\u3057\u305fbash\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u3059\u308b<\/li>\n
CSV<\/h3>\n
apoc.import.csv<\/code>\u3068\u540c\u69d8\u3067\u3059\u3002<\/p>\n
\n
id:ID<\/code><\/pre>\n<\/li>\n
1\n2\n3\n4<\/code><\/pre>\n<\/li>\n
5\n6\n7\n8\n9<\/code><\/pre>\n<\/li>\n
Node:START_ID,Node:END_ID,score:float<\/code><\/pre>\n<\/li>\n
1,2,0.412\n1,3,0.623\n3,4,0.512<\/code><\/pre>\n<\/li>\n
4,5,0.79\n6,7,0.31\n7,8,0.79<\/code><\/pre>\n<\/li>\n<\/ul>\n
docker-compose.yaml<\/h3>\n
EXTENSION_SCRIPT<\/code>\u3067\u30b3\u30f3\u30c6\u30ca\u8d77\u52d5\u6642\u306b\u5b9f\u884c\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002
\n\u307e\u305f\u3001\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u306fDB\u30b5\u30fc\u30d0\u5185\u306b\u5b58\u5728\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001\u30db\u30b9\u30c8\u30de\u30b7\u30f3\u5074\u306e\u30d5\u30a9\u30eb\u30c0\u3092docker\u30b3\u30f3\u30c6\u30ca\u5074\u306e\/script<\/code>\u30d5\u30a9\u30eb\u30c0\u306b\u30de\u30a6\u30f3\u30c8\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
version: '3'\n\nservices:\n neo4j:\n image: neo4j:latest\n ports:\n - "7474:7474" # \u7ba1\u7406\u753b\u9762\u7528port\n - "7687:7687" # websocket\u7528port\n volumes:\n - ${HOME}\/neo4j\/data:\/data\n - ${HOME}\/neo4j\/logs:\/logs\n - ${HOME}\/neo4j\/conf:\/conf\n - ${HOME}\/neo4j\/import:\/import # \u3053\u3053\u306bCSV\u30d5\u30a1\u30a4\u30eb\u3092\u914d\u7f6e\n - ${HOME}\/neo4j\/script:\/script # \u3053\u3053\u306b\u8d77\u52d5\u6642\u5b9f\u884c\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u914d\u7f6e\n\n environment:\n - NEO4J_AUTH=neo4j\/password\n - EXTENSION_SCRIPT=\/script\/import_csv.sh # \u8d77\u52d5\u6642\u306b\u5b9f\u884c\u3059\u308b\u30b9\u30af\u30ea\u30d7\u30c8<\/code><\/pre>\n
import_csv.sh\uff08\u30b3\u30f3\u30c6\u30ca\u8d77\u52d5\u6642\u306b\u5b9f\u884c\u3055\u308c\u308b\u30b7\u30a7\u30eb\uff09<\/h3>\n
\/import<\/code>\u30d5\u30a9\u30eb\u30c0\u306bCSV\u30d5\u30a1\u30a4\u30eb\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u3001\u65e2\u5b58\u306eDB\u3092\u524a\u9664\u3057\u3066\u3001CSV\u30ed\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002
\nCSV\u30d5\u30a1\u30a4\u30eb\u306f\u6b63\u898f\u8868\u73fe\u3092\u4f7f\u3063\u3066\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n#!\/bin\/bash\nset -euC\n\n# CSV\u30d5\u30a1\u30a4\u30eb\u304c\u306a\u3051\u308c\u3070\u4f55\u3082\u3057\u306a\u3044\nif [[ "$(ls -1 \/import | wc -l)" == "0" ]]; then\n echo "import csv skipped."\n return\nfi\n\n# \u30c7\u30fc\u30bf\u3092\u5168\u524a\u9664\necho "delete database started."\nrm -rf \/var\/lib\/neo4j\/data\/databases\nrm -rf \/var\/lib\/neo4j\/data\/transactions\necho "delete database finished."\n\n# CSV\u30a4\u30f3\u30dd\u30fc\u30c8\necho "importing csv started."\n\/var\/lib\/neo4j\/bin\/neo4j-admin import \\\n --id-type=INTEGER \\\n --nodes="\/import\/nodes_header.csv,\/import\/nodes_data_[0-9]+.csv" \\\n --relationships="\/import\/relations_header.csv,\/import\/relations_data_[0-9]+.csv"\necho "importing csv finished."<\/code><\/pre>\n
python\u5b9f\u88c5<\/h3>\n
\n
with<\/code>\u3067\u6271\u3048\u308b\u3088\u3046\u306b
__ente__<\/code>\u3067\u30b5\u30fc\u30d0\u3092\u8d77\u52d5\u3057\u3001
__exit__<\/code>\u3067\u30b5\u30fc\u30d0\u3092\u505c\u6b62\u3059\u308b\u4f5c\u308a\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/li>\n
-d<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u3064\u3051\u3066\u3044\u307e\u3059<\/li>\n
ServiceUnavailable<\/code>\u304c\u767a\u751f\u3059\u308b\u304b\u3069\u3046\u304b\u3067\u5224\u65ad\u3057\u3066\u3044\u307e\u3059<\/li>\n<\/ul>\n
from neo4j import GraphDatabase, Driver\nfrom neobolt.exceptions import ServiceUnavailable\nimport subprocess\nimport time\nimport traceback\n\nclass Neo4jServer:\n def __enter__(self):\n """\n docker\u30b3\u30f3\u30c6\u30ca\u3092\u8d77\u52d5\u3059\u308b\n """\n subprocess.call(['docker-compose', 'up', '-d'])\n while self.__neo4j_available() is False:\n time.sleep(10)\n return self\n\n def __exit__(self, exc_type, exc_value, tb):\n """\n docker\u30b3\u30f3\u30c6\u30ca\u3092\u505c\u6b62\u3059\u308b\n """\n if tb is not None:\n print(''.join(traceback.format_tb(tb)))\n subprocess.call(['docker-compose', 'stop'])\n\n def __neo4j_available(self) -> bool:\n """\n Neo4j\u304c\u5229\u7528\u53ef\u80fd\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\n """\n try:\n GraphDatabase.driver('bolt:\/\/localhost:7687', auth=('neo4j', 'password'), encrypted=False)\n except ServiceUnavailable:\n print('neo4j is not available yet.')\n return False\n print('neo4j is already available.')\n return True\n\n# \u5b9f\u969b\u306e\u5229\u7528\u30a4\u30e1\u30fc\u30b8\nwith Neo4jServer():\n driver: Driver = GraphDatabase.driver('bolt:\/\/localhost:7687', auth=('neo4j', 'password'), encrypted=False)\n with driver.session() as session:\n print(f"node_count: {session.run('MATCH (n:Node) RETURN count(n) as cnt').single()['cnt']}")\n print(f"relationship_count: {session.run('MATCH ()-[l:LINKED]->() RETURN count(l) as cnt').single()['cnt']}")<\/code><\/pre>\n
\u30ed\u30b0<\/h3>\n
\n
Starting nayose_processing_neo4j_1 ... done \/\/ docker\u30b3\u30f3\u30c6\u30ca\u7acb\u3061\u4e0a\u3052\nneo4j is not available yet. \/\/ CSV\u30ed\u30fc\u30c9\u4e2d\nneo4j is not available yet. \/\/ CSV\u30ed\u30fc\u30c9\u4e2d\nneo4j is not available yet. \/\/ CSV\u30ed\u30fc\u30c9\u4e2d\nneo4j is not available yet. \/\/ CSV\u30ed\u30fc\u30c9\u4e2d\nneo4j is not available yet. \/\/ CSV\u30ed\u30fc\u30c9\u4e2d\nneo4j is already available. \/\/ CSV\u30ed\u30fc\u30c9\u7d42\u4e86\uff0bNeo4j\u30b5\u30fc\u30d0\u7acb\u3061\u4e0a\u3052\u5b8c\u4e86\nnode_count: 9 \/\/ \u30af\u30a8\u30ea\u3067\u30ce\u30fc\u30c9\u304c9\u4ef6\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\nrelationship_count: 6 \/\/ \u30af\u30a8\u30ea\u3067\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u304c6\u4ef6\u767b\u9332\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\nStopping nayose_processing_neo4j_1 ... done \/\/ docker\u30b3\u30f3\u30c6\u30ca\u505c\u6b62<\/code><\/pre>\n<\/li>\n
\ndocker-compose logs<\/code>\u3067\u78ba\u8a8d\u3057\u305f\u30ed\u30b0\u3067\u3059\u3002\u3061\u3087\u3063\u3068\u9577\u3044\u3067\u3059\u304c\u3001\u53c2\u8003\u307e\u3067\u306b\u8f09\u305b\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n
neo4j_1 | Warning: Folder mounted to "\/data\/databases" is not writable from inside container. Changing folder owner to neo4j.\nneo4j_1 | Changed password for user 'neo4j'.\nneo4j_1 | delete database started.\nneo4j_1 | delete database finished.\nneo4j_1 | importing csv started.\nneo4j_1 | Neo4j version: 4.0.1\nneo4j_1 | Importing the contents of these files into \/data\/databases\/neo4j:\nneo4j_1 | Nodes:\nneo4j_1 | \/import\/nodes_header.csv\nneo4j_1 | \/import\/nodes_data_01.csv\nneo4j_1 | \/import\/nodes_data_02.csv\nneo4j_1 | \nneo4j_1 | Relationships:\nneo4j_1 | \/import\/relations_header.csv\nneo4j_1 | \/import\/relations_data_01.csv\nneo4j_1 | \/import\/relations_data_02.csv\nneo4j_1 | \nneo4j_1 | \nneo4j_1 | Available resources:\nneo4j_1 | Total machine memory: 9.735GiB\nneo4j_1 | Free machine memory: 8.790GiB\nneo4j_1 | Max heap memory : 3.833GiB\nneo4j_1 | Processors: 4\nneo4j_1 | Configured max memory: 5.311GiB\nneo4j_1 | High-IO: false\nneo4j_1 | \nneo4j_1 | Type normalization:\nneo4j_1 | Property type of 'score' normalized from 'float' --> 'double' in \/import\/relations_header.csv\nneo4j_1 | \nneo4j_1 | Import starting 2020-03-23 01:23:06.332+0000\nneo4j_1 | Estimated number of nodes: 9.00 \nneo4j_1 | Estimated number of node properties: 9.00 \nneo4j_1 | Estimated number of relationships: 6.00 \nneo4j_1 | Estimated number of relationship properties: 6.00 \nneo4j_1 | Estimated disk space usage: 963B\nneo4j_1 | Estimated required memory usage: 1020MiB\nneo4j_1 | \nneo4j_1 | (1\/4) Node import 2020-03-23 01:23:06.380+0000\nneo4j_1 | Estimated number of nodes: 9.00 \nneo4j_1 | Estimated disk space usage: 513B\nneo4j_1 | Estimated required memory usage: 1020MiB\nneo4j_1 | -......... .......... .......... .......... .......... 5% \u220697ms\nneo4j_1 | .......... .......... .......... .......... .......... 10% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 15% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 20% \u22063ms\nneo4j_1 | .......... .......... .......... .......... .......... 25% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 30% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 35% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 40% \u22063ms\nneo4j_1 | .......... .......... .......... .......... .......... 45% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 50% \u22063ms\nneo4j_1 | .......... .......... .......... .......... .......... 55% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 60% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 65% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 70% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 75% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 80% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 85% \u22069ms\nneo4j_1 | .......... .......... .......... .......... .......... 90% \u22065ms\nneo4j_1 | .......... .......... .......... .......... .......... 95% \u22066ms\nneo4j_1 | .......... .......... .......... .......... .......... 100% \u22066ms\nneo4j_1 | \nneo4j_1 | (2\/4) Relationship import 2020-03-23 01:23:06.649+0000\nneo4j_1 | Estimated number of relationships: 6.00 \nneo4j_1 | Estimated disk space usage: 450B\nneo4j_1 | Estimated required memory usage: 1.004GiB\nneo4j_1 | .......... .......... .......... .......... .......... 5% \u220672ms\nneo4j_1 | .......... .......... .......... .......... .......... 10% \u22065ms\nneo4j_1 | .......... .......... .......... .......... .......... 15% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 20% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 25% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 30% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 35% \u22063ms\nneo4j_1 | .......... .......... .......... .......... .......... 40% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 45% \u22063ms\nneo4j_1 | .......... .......... .......... .......... .......... 50% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 55% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 60% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 65% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 70% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 75% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 80% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 85% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 90% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 95% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 100% \u22060ms\nneo4j_1 | \nneo4j_1 | (3\/4) Relationship linking 2020-03-23 01:23:06.754+0000\nneo4j_1 | Estimated required memory usage: 1020MiB\nneo4j_1 | -......... .......... .......... .......... .......... 5% \u220628ms\nneo4j_1 | .......... .......... .......... .......... .......... 10% \u22065ms\nneo4j_1 | .......... .......... .......... .......... .......... 15% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 20% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 25% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 30% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 35% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 40% \u22064ms\nneo4j_1 | .......... .......... .......... .......... .......... 45% \u22063ms\nneo4j_1 | .......... .......... .......... .......... .......... 50% \u22062ms\nneo4j_1 | .......... .......... .......... .......... .......... 55% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 60% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 65% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 70% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 75% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 80% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 85% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 90% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 95% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 100% \u22060ms\nneo4j_1 | \nneo4j_1 | (4\/4) Post processing 2020-03-23 01:23:06.938+0000\nneo4j_1 | Estimated required memory usage: 1020MiB\nneo4j_1 | -......... .......... .......... .......... .......... 5% \u220615s 312ms\nneo4j_1 | .......... .......... .......... .......... .......... 10% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 15% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 20% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 25% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 30% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 35% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 40% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 45% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 50% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 55% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 60% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 65% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 70% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 75% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 80% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 85% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 90% \u22061ms\nneo4j_1 | .......... .......... .......... .......... .......... 95% \u22060ms\nneo4j_1 | .......... .......... .......... .......... .......... 100% \u22061ms\nneo4j_1 | \nneo4j_1 | \nneo4j_1 | IMPORT DONE in 16s 290ms. \nneo4j_1 | Imported:\nneo4j_1 | 9 nodes\nneo4j_1 | 6 relationships\nneo4j_1 | 15 properties\nneo4j_1 | Peak memory usage: 1.004GiB\nneo4j_1 | importing csv finished.\nneo4j_1 | Directories in use:\nneo4j_1 | home: \/var\/lib\/neo4j\nneo4j_1 | config: \/var\/lib\/neo4j\/conf\nneo4j_1 | logs: \/logs\nneo4j_1 | plugins: \/var\/lib\/neo4j\/plugins\nneo4j_1 | import: \/import\nneo4j_1 | data: \/var\/lib\/neo4j\/data\nneo4j_1 | certificates: \/var\/lib\/neo4j\/certificates\nneo4j_1 | run: \/var\/lib\/neo4j\/run\nneo4j_1 | Starting Neo4j.\nneo4j_1 | 2020-03-23 01:23:23.348+0000 INFO ======== Neo4j 4.0.1 ========\nneo4j_1 | 2020-03-23 01:23:23.357+0000 INFO Starting...\nneo4j_1 | 2020-03-23 01:23:43.913+0000 INFO Bolt enabled on 0.0.0.0:7687.\nneo4j_1 | 2020-03-23 01:23:43.917+0000 INFO Started.\nneo4j_1 | 2020-03-23 01:23:44.947+0000 INFO Remote interface available at http:\/\/0.0.0.0:7474\/\nneo4j_1 | 2020-03-23 01:23:53.825+0000 INFO Neo4j Server shutdown initiated by request\nneo4j_1 | 2020-03-23 01:23:53.880+0000 INFO Stopping...\nneo4j_1 | 2020-03-23 01:23:59.137+0000 INFO Stopped.<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"