Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Load csv by Cypher

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Avatar for asako asako
October 20, 2016

Load csv by Cypher

Load csv by Cypher with Neo4j 3.0.6
refer : https://github.com/asakoRaven/livedoor-dataset

Avatar for asako

asako

October 20, 2016

More Decks by asako

Other Decks in Programming

Transcript

  1. Livedoor άϧϝ Dataset masterܥ ౎ಓ෎ݝϚελ prefs.csv ΤϦΞϚελ areas.csv ӺϚελ station.csv

    ΧςΰϦϚελ categories.csv ೔ຊྉཧͳͲϨετϥϯͷΧςΰϦ छผ
  2. category categories.csv ΧςΰϦϚελ σʔλ ఆٛ • id ΧςΰϦID • name

    ΧςΰϦ໊ • name_kana ΧςΰϦ໊ͻΒ͕ͳ • parent1, parent2 ਌ΧςΰϦID • similar ྨࣅΧςΰϦ໊ parent1, parent2ʹσʔλ͕͸ ͍ͬͯͨΓɺ0͕ઃఆ͞Ε͍ͯ ͨΓɺnullͱ0ͷ۠ผ͕ͳ͍ Load csvʹwhen then else endͷ۟Λૠೖͯ͠ɺload৚݅ Λهड़͢Δ͜ͱ͕Ͱ͖ͳ͍
  3. LOAD CSVίϚϯυ Ͱ when then else end ͷهड़͕ૠೖͰ͖ͳ ͍ Ұ୴ɺcategories.csvͷશ

    ߦΛϩʔυ Category node࡞੒ޙmatch ͰΫΤϦͰ͖ͨ΋ͷ͚ͩ ਌ category - ࢠcategoryͷ relationshipΛઃఆ
  4. MATCHͰnode not foundͩ ͱnull͕ฦ͞ΕΔ null nodeͱrelationshipΛੜ ੒͓͠Α͏ͱ͢ΔͱɺΤϥʔ ͱͳΔ nullͷnode͕ฦ͖ͬͯͨͱ͖ ͸ɺignoreͯ͠ɺॲཧΛܧଓ

    ͯ͠ཉ͍͠ FOREACHΛ࢖༻͠ɺWHEN node IS NULLͷॲཧΛهड़ ͜Ε͸known trickͰ͋ΔΑ͏ͩ http:// www.markhneedham.com/ blog/2014/08/22/neo4j- load-csv-handling- empty-columns/
  5. rating ratings.csv ޱίϛσʔλ • id ޱίϛID • restaurant_id ର৅͓ళID •

    user_id ϢʔβID • total ૯߹ධՁ(0-5) • food ྉཧධՁ(0-5) • service αʔϏεධՁ(0-5) • atmosphere งғؾධՁ(0-5) • cost_performance ίετύϑΥʔϚϯεධՁ (0-5) • title ޱίϛίϝϯτλΠτϧ • body ޱίϛίϝϯτ • purpose ར༻໨త • created_on ౤ߘ೔࣌ Rating id͕ॏෳ͍ͯ͠ΔՕॴ ͕͋Δ
  6. CREATE (rating:Rating { id: line.id, total: line.total, title: line.title, comment:

    line.body }) ͸௨Δͷʹ MERGE (rating:Rating { id: line.id, total: line.total, title: line.title, comment: line.body }) ͸NGͰ͋Δ ʲݪҼʳ http://stackoverflow.com/ questions/35381968/cypher-node- already-exists-issue-with-merge MERGE͸ࢦఆnode͕ɺͳ͚Ε͹ɺ࡞੒͢Δ Id͕ಉ͡ɺ͔ͭɺpropertyͷҧ͏nodeͷmerge Λࢦఆ͍ͯ͠ΔͷͰɺnode࡞੒ΛτϥΠ͢Δ id unique constraintʹώοτ͠ɺΤϥʔͱͳΔ ʲରࡦʳ هड़ΛҎԼͷΑ͏ʹมߋ͢Δ MERGE (rating:Rating {id: line.id}) SET rating.total = line.total, rating.title = line.title, rating.comment = line.body
  7. ratingͱrestaurantͷ relationshipΛ࡞੒͢Δͱ ͖ɺҎԼͷΤϥʔ͕ͰΔ WARNING: Expected to find a node at

    restaurant but found nothing Some(null) (Failure when processing URL 'file://ratings.csv' on line 205833 (which is the last row in the file). Possibly the last row committed during import is line 204999. Note that this information might not be accurate.) Categoryͷϩʔυͷͱ͖ʹ ࢖༻ͨ͠ FOREACHͷτϦ οΫΛ࢖༻͠ nullΦϒδΣ Ϋτ൑ఆΛ࣮ࢪ͢Δ ೔࣌ͳͲͰηάϝϯτΛ͖ ΓɺҰ෦σʔλΛఏڙ͞Ε ͨ৔߹ͳͲͰ͸ɺࢀরઌ͕ ͳ͍Մೳੑ͕͋Δ
  8. Livedoor gourmet dataset ΛόΧͪΐΜͰϩʔυ͢Δʹ͸ 1. Download Livedoor gourmet data set

    2. Extract data set 3. check neo4j config 
 dbms.directories.importύϥϝʔλͷ֬ೝ 4. Copy & past Load_livedoor_gourmet.cql 5. Load_livedoor_gourmet.cqlͷઌ಄෦෼ͷfilepathఆٛΛࣗ෼ͷ؀ڥʹ߹Θ ͤͯमਖ਼ 6. neo4j shell ίϚϯυ࣮ߦ
 $ neo4j-shell -file load_livedoor_gourmet.cql