Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Last active June 28, 2016 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbuesing/5d0bb6c906f035eb267618209ec7ce05 to your computer and use it in GitHub Desktop.
Save gbuesing/5d0bb6c906f035eb267618209ec7ce05 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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