简体   繁体   English

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

[英]How to iterate hash elements in groups?

I have a hash very similar to the following JSON object; 我有一个非常类似于以下JSON对象的哈希; This is only an example not the exact output. 这只是一个例子而不是确切的输出。

I need to iterate over the hash and sort it by host or by service_name and then output in the view I am working with blocks reflecting those sorts. 我需要迭代哈希并按hostservice_name对其进行排序,然后在视图中输出我正在处理反映这些排序的块。

If I go to my view expecting to see things listed by host, and I got ten services across that one host, I need to have a list on the view with that per host. 如果我转到我的观点期望看到主机列出的内容,并且我在该主机上有十个服务,我需要在每个主机的视图上有一个列表。 Similarly, when it comes to service_name . 同样,当涉及到service_name

If I have 100 service_names matching, I need to block them and list them together. 如果我有100个service_names匹配,我需要阻止它们并将它们列在一起。

I am new to Rails, and typically would handle this via JavaScript and JSON as what's being done is ultimately planned for heavy DOM manipulation anyway, but the lead guy insists this has to be done server-side first, so I am at a loss. 我是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"
        }
    ]
}

Here's the code for grouping them by service_name with textual (not HTML) output. 这是使用service_name和textual(而不是HTML)输出对它们进行分组的代码。 HTML would be the same, but with reasonable HTML markup. HTML将是相同的,但具有合理的HTML标记。 Service names are sorted alphabetically; 服务名称按字母顺序排序; no reason other than as an example. 除了作为一个例子,没有理由。

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 returns a hash of arrays, keyed (in this case) by the service_name string (eg, "web" , "tasktracker" , etc.) If you need to sort within each group's info, use sort . group_by返回数组的哈希值,由service_name字符串键入(在本例中)(例如, "web""tasktracker"等)。如果需要在每个组的信息中进行排序,请使用sort For example, to sort within each group by the :status value: 例如,要通过: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

You can also save the sorting methods for later use: 您还可以保存排序方法以供以后使用:

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

Here's the JSON converted to a Ruby hash in case you need it. 这里是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"
        }
    ]
}

Use find_all and sort_by . 使用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