Skip to content

Instantly share code, notes, and snippets.

@jennifersmith
Forked from mneedham/neo_loading.rb
Created June 10, 2012 13:52
Show Gist options
  • Save jennifersmith/2905725 to your computer and use it in GitHub Desktop.
Save jennifersmith/2905725 to your computer and use it in GitHub Desktop.
Loading stuff into neo via the batch API
[ {
"method" : "POST",
"to" : "/node",
"body" : {
"name" : "Jen"
},
"id" : 1111
}, {
"method" : "POST",
"to" : "/index/node/people",
"body" : {
"value": "Jen",
"uri": "{1111}",
"key": "name"
}
}]
curl -d @body.txt -X POST -H "Content-Type: application/json" http://localhost:7474/db/data/batch
# So the problem is inserting data into neo using the batch API. So we have a bunch of people and we want to put them into the graph and also
# add to to the index so that we can search for them.
# The way the batch API works is that you can refer to previous commands by referencing their index in the list of commands (zero indexed)
# e.g. if I want to reference the person added in the first command I would reference that node as {0}
# You should be able to see how that works in the code below.
neo_people_to_load = Set.new
neo_people_to_load << { :name => "Mark Needham", :id => 1 }
neo_people_to_load << { :name => "Jenn Smith", :id => 2 }
neo_people_to_load << { :name => "Chris Ford", :id => 3 }
command_index = 0
people_commands = neo_people_to_load.inject([]) do |acc, person|
acc << [:create_node, {:id => person[:id], :name => person[:name]}]
acc << [:add_node_to_index, "people", "name", person[:name], "{#{command_index}}"]
command_index += 2
acc
end
# So what we want to get is the following:
# [
# [:create_node, {:id=>"1", :name=>"Mark Needham"}], [:add_node_to_index, "people", "name", "Mark Needham", "{0}"],
# [:create_node, {:id=>"2", :name=>"Jenn Smith"}], [:add_node_to_index, "people", "name", "Jenn Smith", "{2}"],
# [:create_node, {:id=>"3", :name=>"Chris Ford"}, [:add_node_to_index, "people", "name", "Chris Ford", "{4}"]
# ]
# OK so I cannot test this but http://docs.neo4j.org/chunked/snapshot/rest-api-batch-ops.html
# indicates we can just use arbitrary IDs, not indexxes. providing they are unique and numeric
# Does that make it a bit simpler?
# Assuming here you have given an auto id to person each time, if not I would use map_indexed and flatten
people_commands = neo_people_to_load.inject([]) do |acc, person|
acc << [:create_node, {:id => person[:id], :name => person[:name]}]
acc << [:add_node_to_index, "people", "name", person[:name], "{#{person[:id]}}"]
acc
end
@jennifersmith
Copy link
Author

I would just structure it like https://gist.github.com/2906469/d55492cdee790e84b994468cdb21ab1daf464bca (the non interleaved sln) for maximum goodness.

We should always write code like this.

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