Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Last active Jun 28, 2016
Embed
What would you like to do?
Neo4j Data Import Example
require 'rest-client'
require 'json'
module Neo4jClient
SERVER = 'http://neo4j:neo4j@localhost:7474'
def self.cypher query, params = {}
resp = RestClient.post("#{SERVER}/db/data/cypher", {query: query, params: params}.to_json, content_type: :json, accept: :json)
JSON.parse resp
end
end
# IMPORT USERS AS NODES
User.find_each do |u|
Neo4jClient.cypher "CREATE (:User {props})", props: {id: u.id, name: u.name, email: u.email}
end
# IMPORT FOLLOWINGS AS RELATIONSHIPS
Follow.find_each do |f|
query = "MATCH (follower:User {id: {follower_id}}) MATCH (followee:User {id: {followee_id}}) CREATE (follower)-[:FOLLOWS {id: {id}}]->(followee)"
Neo4jClient.cypher query, id: f.id, follower_id: f.follower_id, followee_id: f.followee_id
end
# RUN A QUERY
resp = Neo4jClient.cypher("MATCH (:User {id: 1})-[:FOLLOWS]->(u:User) RETURN u")
resp['data'].each do |r|
puts r.first['data']
end
@gbuesing

This comment has been minimized.

Copy link
Owner Author

@gbuesing gbuesing commented Jun 28, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment