By Neo4j Staff | January 9, 2015 Written by Brian Underwood, originally posted on his blog.
Loading SQL to Neo4j Like MagicWhen using neo4j for the first time, most people want to import data from another database to start playing around. There are a lot of options including LOAD CSV, batch-import, and even using Groovy. All of these require some setup and configuration. I wanted to create the simplest SQL to Neo4j import process possible. Enter neo4apis-activerecord! You may be thinking: “Brian, I’m not a Ruby programmer! I don’t know anything about ActiveRecord”. No worries! I’ll get you there in 2 simple steps: setup and running the command
SetupFirstly, if you don’t already have neo4j on your computer, find installation instructions for your computer in theneo4j manual. Simply use RubyGems (RubyGems installation):
Then install the database adapter gem:
gem install neo4apis-activerecord
This can be
gem install pg
mysql2for MySQL, or
sqlite3for SQLite Then create a
config/database.ymlfile which looks something like this:
For examples on how to configure mysql or sqlite, see this github gist or the official documentation
development: adapter: postgresql encoding: unicode pool: 5 host: localhost port: 5432 database: your_database_name username: james password: reallysecret
The CommandThen to import all your data it is as simple as:
Let’s break that command down:
neo4apis activerecord all_tables --identify-model --import-all-associations
all_tablescommand finds all tables in the database and imports them.
- By default ActiveRecord table naming conventions will be used. The
--identify-modeloption however will use a looser set of assumptions and configure ActiveRecord models according to the tables it finds in your database.
--import-all-associationsoption will import ActiveRecord associations and create Neo4j relationships from them. When no existing ActiveRecord models are used, those associations come from using the
The One Small CatchIs it perfect? Close, but not quite! Taking the Chinook Database as an example, the
Customertable has a
SupportRepIdcolumn which references the
Employeetable. There’s no way to know from examining the column name what table it is refering to. These cases require a little bit of configuration on your part. To do that, you should create a
config/environment.rbfile like this:
config = YAML.load(File.read('config/database.yml'))['development'] ActiveRecord::Base.establish_connection(config) class Customer < ActiveRecord::Base belongs_to :support_rep, foreign_key: 'SupportRepId', class_name: 'Employee' end
establish_connectionlines are there because
neo4apis-activerecordassumes that this file makes the connection to ActiveRecord and won’t try to do it itself. The
belongs_tois part of ActiveRecord`s well documented and heavily used API. And of course, for those of you using ActiveRecord already this will all work out of the box! If you want to get really down and dirty with the Ruby programming, there’s even an API for doing your own custom import of your ActiveRecord models. See the README for details! Want to learn more about graph databases? Click below to get your free copy of O’Reilly’s Graph Databases ebook and discover how to use graph technologies for your application today. Download My Ebook Copy
From the CEO
Have a Graph Question?
Reach out and connect with the Neo4j staff.Stackoverflow
Share your Graph Story?
Email us: email@example.com