【发布时间】:2014-04-29 07:01:15
【问题描述】:
我想不出在批量导入期间向节点添加标签的方法,我已经编写了一个任务来将所有用户从 mysql 导出到 neo4j,我在 userId 上添加了一个约束。我已经修改了批次以接受标签,它工作得很好。现在,由于每个 userId 都是唯一的,因此批量导入工作正常,但是我有另一个表作为技能,我在技能名称上添加了一个约束,对于多个用户可能有相似的技能名称,现在因为技能名称不是唯一的,我的批量更新失败。我想知道是否有办法在批量导入中添加多个技能并对技能名称有限制。我也想知道批量导入时是否有另一种添加标签的方法。
task :create_graph => :environment do
people = []
neo = Neography::Rest.new
puts "Fetching Candidates"
User.all.each do |user|
begin
people << { :userId => user.id, :firstName => user.first_name }
rescue Exception => e
puts e.message
end
end
puts "Creating Candidate Nodes"
people_nodes = {}
people.each_slice(100) do |slice|
commands = []
slice.each_with_index do |person, index|
commands << [:create_unique_node_with_label, "UserIndex", "userId", person[:userId], person]
end
label = ["CANDIDATE"]
puts commands
batch_results = neo.batch(label,*commands)
end
end
这是我在 gem 中修改的 batch.rb 文件中的修改代码。
def do_batch(label,*args)
batch = []
Array(args).each_with_index do |c, i|
batch << {:id => i }.merge(get_batch(c))
if c[0]==:create_unique_node_with_label
batch << {:to => "{"+i.to_s+"}/labels",:body => label,:method => "POST"}
end
end
options = {
:body => batch.to_json,
:headers => json_content_type
}
puts options
@connection.post("/batch", options)
end
def batch_create_unique_node_with_label(index, key, value, properties)
post "/index/node/%{index}?unique" % {:index => index} do
{
:key => key,
:value => value,
:properties => properties
}
end
end
【问题讨论】:
标签: batch-file import neo4j constraints unique