Last active
June 28, 2016 00:29
-
-
Save gbuesing/5d0bb6c906f035eb267618209ec7ce05 to your computer and use it in GitHub Desktop.
Neo4j Data Import Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://neo4j.com/docs/developer-manual/current/