簡體   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