最终,我希望通过在特定日期创建的项目总数创建折线图。到目前为止我所做的是使用mapreduce来处理按日期分组的组。然而,我似乎无法做的是将结果和过程转化为可用的东西。
为了让你知道我的意思,我的mapreduce返回以下内容(DateTime需要我稍后会做的一些工作):
[
{
"_id" => 1970-01-01 00:00:00 UTC,
"value" =>{ "count" => 25.0 }
},
{
"_id" =>2014-10-04 00:00:00 UTC,
"value" =>{ "count"=>2.0 }
},
{
"_id" => 2014-11-04 00:00:00 UTC,
"value" => { "count"=> 2.0 }
},
{
"_id" => 2014-12-04 00:00:00 UTC,
"value" => { "count"=> 9.0 }
}
]
我需要做的是采取每个_id
并以count
的方式展示1970-01-01 00:00:00 UTC = 25
2014-10-04 00:00:00 UTC = 2
2014-11-04 00:00:00 UTC = 2
2014-12-04 00:00:00 UTC = 9
,以便我可以轻松地将其传递给Google图表。所以我需要的是:
{{1}}
但无论我尝试什么,我都看不到遍历嵌套哈希数组并将我需要的数据拉入新变量。任何帮助将不胜感激!
答案 0 :(得分:1)
str = [{"_id"=>"1970-01-01 00:00:00 UTC", "value"=>{"count"=>25.0}}, {"_id"=>"2014-10-04 00:00:00 UTC", "value"=>{"count"=>2.0}}, {"_id"=>"2014-11-04 00:00:00 UTC", "value"=>{"count"=>2.0}}, {"_id"=>"2014-12-04 00:00:00 UTC", "value"=>{"count"=>9.0}}].map do |row|
"#{row["_id"]} = #{row["value"]["count"].to_i}"
end.join("\n")
puts str
#>>
1970-01-01 00:00:00 UTC = 25
2014-10-04 00:00:00 UTC = 2
2014-11-04 00:00:00 UTC = 2
2014-12-04 00:00:00 UTC = 9