Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 解析JSON和将数据存储到数据库的性能问题_Ruby On Rails_Ruby_Database_Algorithm_Neo4j - Fatal编程技术网

Ruby on rails 解析JSON和将数据存储到数据库的性能问题

Ruby on rails 解析JSON和将数据存储到数据库的性能问题,ruby-on-rails,ruby,database,algorithm,neo4j,Ruby On Rails,Ruby,Database,Algorithm,Neo4j,我正在尝试构建一个将草图转换为幻灯片的web应用程序。幻灯片上对象的位置和类别将由JSON给出,如下所示: [ { "attachment": "https://s3-us-west-2.amazonaws.com/some_picture.jpg", "response": { "annotations": [ { "width": 72, "height": 20, "left": 2

我正在尝试构建一个将草图转换为幻灯片的web应用程序。幻灯片上对象的位置和类别将由JSON给出,如下所示:

[
  {
    "attachment": "https://s3-us-west-2.amazonaws.com/some_picture.jpg",
    "response": {
      "annotations": [
        {
          "width": 72,
          "height": 20,
          "left": 24,
          "top": 180,
          "label": "text"
        },
        {
          "width": 96,
          "height": 19,
          "left": 26,
          "top": 212,
          "label": "picture"
        }
      ]
    }
  }
]
我使用each循环遍历JSON文件中的对象,然后将它们存储到Neo4j数据库中

def save_detection_to_db(detection_json)
  detection_json.each do |single_picture|
    annotations = single_picture["response"]["annotations"]
    annotations.each do |single_annotation|
      label = single_annotation["label"]
      determine_node_label(label).create(width: single_annotation["width"], 
                                         height: single_annotation["height"],
                                         top: single_annotation["top"],
                                         left: single_annotation["left"],
                                         category: single_annotation["label"])
    end
  end
end

# determine_node_label("text") #=> Text
# determine_node_label("picture") #=> Picture
这样,我可以在0.6秒内存储大约100个对象。但是考虑到这个应用程序是为许多人设计用来生成幻灯片的,它无法完成这项工作。我假设each循环不是一个很好的方法。我还应该尝试其他什么方法?如果您有任何建议,我们将不胜感激。

请尝试对neo4j进行深入研究。关于如何操作,有许多示例。

您可以尝试:

def save_detection_to_db(detection_json)
  detection_json.each do |single_picture|
    annotations = single_picture["response"]["annotations"].group_by{|x| x["label"]}
    annotations.each do |k, values|
      klass = determine_node_label(k)
      values.each do |value|
         value["category"] = value.delete("label")
         klass.create(value)
      end 
    end
end
在上面的代码中,我们没有为每个记录找到节点,group_by将对节点进行分组并仅为它们进行查找,这将减少更多的循环以及每个val

我对neo4jr没有任何了解,如果您在neo4j中有任何批插入,只需获取值并从中插入即可

value["category"] = value.delete("label")
指:

在循环中,我们将得到一个值:

{"width"=>72, "height"=>20, "left"=>24, "top"=>180, "label"=>"text"}

在DB中,我们有“category”属性,但没有“label”,这就是原因,我正在用代码段使用的“category”替换“label”键。

这非常有用!但是你能解释一下
value[“category”]=value.delete(“label”)
?value[“category”]=value.delete(“label”)的意思是:在循环中,我们将有一个值:{“width”=>72,“height”=>20,“left”=>24,“top”=>180,“label”=>“text”}在DB中,我们有“category”属性,我们没有“label”,这就是原因,我正在替换一个“label”通过代码段使用“Category”键。同样的解释在帖子中更新。