繁体   English   中英

如何在组中迭代哈希元素?

[英]How to iterate hash elements in groups?

我有一个非常类似于以下JSON对象的哈希; 这只是一个例子而不是确切的输出。

我需要迭代哈希并按hostservice_name对其进行排序,然后在视图中输出我正在处理反映这些排序的块。

如果我转到我的观点期望看到主机列出的内容,并且我在该主机上有十个服务,我需要在每个主机的视图上有一个列表。 同样,当涉及到service_name

如果我有100个service_names匹配,我需要阻止它们并将它们列在一起。

我是Rails的新手,并且通常会通过JavaScript和JSON处理这个问题,因为正在做的最终计划用于重度DOM操作,但是主要人员坚持认为这必须首先在服务器端完成,所以我很茫然。

{
    "status": "successful",
    "service_list": [
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "oozie",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "single-namenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "single-database",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "datanode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "secondarynamenode",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "web",
            "status": "DEAD",
            "status_message": "Running Master Service",
            "host": "1"
        },
        {
            "service_name": "tasktracker",
            "status": "RUNNING",
            "status_message": "Running Service",
            "host": "1"
        },
        {
            "service_name": "jobtracker",
            "status": "RUNNING",
            "status_message": "Running Master Service",
            "host": "1"
        }
    ]
}

这是使用service_name和textual(而不是HTML)输出对它们进行分组的代码。 HTML将是相同的,但具有合理的HTML标记。 服务名称按字母顺序排序; 除了作为一个例子,没有理由。

svcs = val[:service_list]
puts "#{svcs.size} service entries."

grouped = svcs.group_by { |svc| svc[:service_name] }
puts "#{grouped.size} named services:"

grouped.keys.sort.each do |name|
    puts "  service: #{name}"
    grouped[name].each do |svc|
        puts "     host: #{svc[:host]}"
        puts "   status: #{svc[:status]}"
        puts "           #{svc[:status_message]}"
    end
    puts '-'*40
end

group_by返回数组的哈希值,由service_name字符串键入(在本例中)(例如, "web""tasktracker"等)。如果需要在每个组的信息中进行排序,请使用sort 例如,要通过:status值在每个组中进行排序:

grouped.keys.sort.each do |name|
    puts "  service: #{name}"
    group_info = grouped[name]
    sorted_info = group_info.sort { |a, b| a[:status] <=> b[:status] }
    sorted_info.each do |svc|
        puts "     host: #{svc[:host]}"
        puts "   status: #{svc[:status]}"
        puts "           #{svc[:status_message]}"
    end
    puts '-'*40
end

您还可以保存排序方法以供以后使用:

sort_by_status = Proc.new { |a, b| a[:status] <=> b[:status] }
sorted_info = group_info.sort &sort_by_status   

这里是JSON转换为Ruby哈希,以备不时之需。

val = {
    :status => "successful",
    :service_list => [
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "oozie",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "single-namenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "single-database",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "datanode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "secondarynamenode",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "web",
            :status => "DEAD",
            :status_message => "Running Master Service",
            :host => "1"
        },
        {
            :service_name => "tasktracker",
            :status => "RUNNING",
            :status_message => "Running Service",
            :host => "1"
        },
        {
            :service_name => "jobtracker",
            :status => "RUNNING",
            :status_message => "Running Master Service",
            :host => "1"
        }
    ]
}

使用find_allsort_by

# Get all the thingies with the service name "web"
thingies.find_all {|thingy| thingy[:service_name] == 'web' }

# Sort all the thingies by status
thingies.sort_by {|thingy| thingy[:status] }

# Put them together
thingies.find_all {|thingy| thingy[:service_name] == 'web' } \
        .sort_by  {|thingy| thingy[:status]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM