【问题标题】:Batch Import Neo4j Unique Nodes With Labels批量导入带有标签的 Neo4j 唯一节点
【发布时间】: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


    【解决方案1】:

    您使用的唯一索引用于 Neo4j 中的旧索引。我建议你看看 Neo4j 2.0 中新的唯一性约束,创建一个类似

    的约束
    CREATE CONSTRAINT ON (n:Candidate) ASSERT n.name IS UNIQUE;
    

    然后使用 cypher MERGE 功能对独特的事物进行获取或创建:

    MERGE (n:Candidate {name:"some name"})
    // And then do more things with n here
    

    另外,您可以使用新的事务端点对这些密码查询进行批处理:

    http://docs.neo4j.org/chunked/stable/rest-api-transactional.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2015-03-24
      • 2016-10-27
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多