-
-
Save jennifersmith/2905725 to your computer and use it in GitHub Desktop.
Loading stuff into neo via the batch API
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
[ { | |
"method" : "POST", | |
"to" : "/node", | |
"body" : { | |
"name" : "Jen" | |
}, | |
"id" : 1111 | |
}, { | |
"method" : "POST", | |
"to" : "/index/node/people", | |
"body" : { | |
"value": "Jen", | |
"uri": "{1111}", | |
"key": "name" | |
} | |
}] |
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
curl -d @body.txt -X POST -H "Content-Type: application/json" http://localhost:7474/db/data/batch |
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
# 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 |
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
It's not actually pseudo code! That's the way that the neography gem builds up the commands that it sends to the REST API. I didn't realise that you could specify the ID like you've done - the way I read the docs I thought 'job id' meant its order in the list which obviously creates some evil dependencies. So Jenn's way is cooler.