Neo4jRB
Neo4j単体でも面白いのだけど、結果を現実世界に出すにはWEB画面を作るのが楽。というわけでNeo4jRBに触れてみる。とりあえず環境構築のみ。
- 環境構築
- とりあえずDebianで環境構築をやってみる
neo4jの準備
各環境にあったパッケージリポジトリを使用しましょう。
書いてあるとおりやればサービスとしてneo4jが入ります
// リポジトリの追加 $ wget -O - https://debian.neo4j.org/neotechnology.gpg.key | sudo apt-key add - $ echo 'deb https://debian.neo4j.org/repo stable/' | sudo tee /etc/apt/sources.list.d/neo4j.list $ sudo apt-get update // インストール $ sudo apt-get install neo4j
一回DBを起動させておいてパスワード変更しといてください
$ sudo systemctl start neo4j
この後 http://localhost:7474 へ行って、id: neo4j, pass:neo4jでログイン、初回パスをneo4jから違うものに変更しておく。
Rubyの準備
とりあえずrbenvを使ってRuby 2.4に変えておけばいいんじゃないかと
Railsの準備
Rails 5.2.1を使った。
いざモックアップ作成
とりあえず公式ドキュメントの通りコマンドを叩く、
だいたい以下のような感じで進めた。
- 初回はrbenvに入ったrailsコマンドを使いたかったので以下のようになっている
$ rbenv exec bundle install rails $ rbenv exec gem rails new myapp -m http://neo4jrb.io/neo4j/neo4j.rb -O $ cd myapp $ bundle install
Railsは基本的なCRUD処理はscaffoldで作ってくれる。以下は例。
$ rails generate scaffold User name:string email:string $ rails s
とりあえずこれでNeo4Jのノードとちょっとしたアトリビュートを画面から追加できる。リレーションなどは別APIを作ってみるか?
グラフDBのスキーマ設定
$ bundle exec rails --tasks | grep neo4j rails neo4j:generate_schema_migration[index_or_constraint,label,property_name] # Generates a migration for the specified constraint/index and label/property combination rails neo4j:legacy_migrate[task_name,subtask] # Run a script against the database to perform system-wide changes rails neo4j:migrate # A shortcut for neo4j::migrate::all rails neo4j:migrate:all # Run all pending migrations rails neo4j:migrate:down # Revert a migration given its VERSION rails neo4j:migrate:reset # Resolve an incomplete version state rails neo4j:migrate:resolve # Resolve an incomplete version state rails neo4j:migrate:status # Print a report of migrations status rails neo4j:migrate:up # Run a migration given its VERSION rails neo4j:rollback # Rollbacks migrations given a STEP number rails neo4j:schema:dump # Creates a db/neo4j/schema.yml file which represents the indexes / onstraints in the Neo4j DB rails neo4j:schema:load[remove_missing] # Loads a db/neo4j/schema.yml file into the database
ActiveNode & ActiveRel
ノードを追加するためにActiveNode、リレーションを追加するためにActiveRelを使いたい。