2017-09-14 49 views
0

我正在從系統監控工具(薩爾是具體)的統計信息,我試圖重新構造它們。我收到JSON格式的所有數據。在Java中,接收到的JSON表示爲Map<String, Object>並存儲在變量dataMap中。如何基於Java中的某個列表來轉換列表的地圖?

下面是1分鐘的有價值的處理器統計的例子:

{ 
    "processor_time":["08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM","08:59:01 AM"], 
    "processor_CPU":["all",0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23], 
    "processor_%user":[1.6,8.78,2.32,1.67,1.78,1.23,3.83,3.57,0.93,0.62,0.32,0.48,0.37,3.94,1.25,1.22,1.04,0.78,0.65,1.2,0.57,0.4,0.87,0.25,0.38], 
    "processor_%nice":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], 
    "processor_%system":[0.4,2.57,0.65,0.45,0.62,0.32,0.93,0.22,0.2,0.17,0.15,0.15,0.13,0.75,0.32,0.18,0.37,0.25,0.22,0.13,0.22,0.1,0.22,0.08,0.22], 
    "processor_%iowait":[0.01,0,0.1,0,0,0,0,0,0,0,0,0,0,0,0.03,0,0,0,0,0,0,0,0,0,0], 
    "processor_%steal":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], 
    "processor_%idle":[97.99,88.65,96.93,97.88,97.6,98.45,95.23,96.22,98.87,99.22,99.53,99.37,99.5,95.31,98.4,98.6,98.6,98.97,99.13,98.67,99.22,99.5,98.92,99.67,99.4] 
} 

我已經拉了這一點的dataMap並整理它以每分鐘計算。下面的代碼顯示這一點:

// declared per minute map 
Map<Integer, Object> perMinuteMap = new LinkedHashMap<>(); 
// Narrowed in on the sar stat details. 
// detailsList would be pull out of dataMap, the inbound JSON map 
List<Map<String, Object>> detailsList = new ArrayList<>(); 
for (Map<String, Object> entry : detailsList) 
{ 
    List<String> headers = (List<String>) entry.get("headers"); 
    String type = (String) entry.get("type"); 
    List<List<Object>> data = (List<List<Object>>) entry.get("data"); 
    // Loop through the list of data a given type 
    for (List<Object> dataDetails : data) 
    { 
     // Variables 
     String timeString = (String) dataDetails.get(0); 
     Integer extractedMinute = new Integer(-1); 

     // Minute extraction 
     if (!timeString.contains("Average")) 
     { 
     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a"); 
     LocalTime time = LocalTime.parse(timeString, formatter); 
     DateTimeFormatter formatter2 = DateTimeFormatter.ISO_DATE; 
     extractedMinute = Integer.valueOf(time.getMinute()); 
     } 
     String field = ""; 
     int dataDetailsSize = dataDetails.size(); 
     // loop through the individual data points. 
     for (int i = 0; i < dataDetailsSize; i++) 
     { 
     List<Object> dataList; 
     Object dataPoint = dataDetails.get(i); 
     if (i < headers.size()) 
     { 
      field = type + "_" + headers.get(i); 
     } 
     else 
     { 
      field = type + "_unknown"; 
     } 

     if (extractedMinute != Integer.valueOf(-1)) 
     { 
      LinkedHashMap<String, Object> explodedMinute = (LinkedHashMap<String, Object>) perMinuteMap 
        .get(extractedMinute); 

      if (null != explodedMinute) 
      { 
       dataList = (List<Object>) explodedMinute.get(field); 
       if (null != dataList) 
       { 
        dataList.add(dataPoint); 
        explodedMinute.put(field, dataList); 
       } 
       else 
       { 
        dataList = new ArrayList<>(); 
        dataList.add(dataPoint); 
        explodedMinute.put(field, dataList); 
       } 
       perMinuteMap.put(extractedMinute, explodedMinute); 
      } 
      else 
      { 
       explodedMinute = new LinkedHashMap<>(); 
       dataList = new ArrayList<>(); 
       dataList.add(dataPoint); 
       explodedMinute.put(field, dataList); 
       perMinuteMap.put(extractedMinute, explodedMinute); 
      } 
     } 
     } 
    } 
} 

我用以下,再加上一些手動格式,來構建我已經張貼以上的JSON。

// Loop through each key, value of a Map 
    perMinuteMap.forEach((key, value) -> 
    { 
    String json_minute_string; 
    try 
    { 
     json_minute_string = mapper.writeValueAsString(value); 
     System.out.println(json_minute_string); 
    } 
    catch (JsonProcessingException e) 
    { 
     log.error("Unexpected Exception", e); 
    } 
    }); 

我期待重組的數據,並整理統計鍵控上的processor_CPU值。例如,它應該像類似以下內容:

{ 
    "processor_%user": { 
    "CPU_all": [1.6], 
    "CPU_1": [8.78] 
    }, 
    "processor_%nice": { 
    "CPU_all": [0], 
    "CPU_1": [0] 
    } 
} 

重組的數據不會有processor_timeprocessor_CPU領域。

最終,我的目標是循環訪問數據並將每分鐘的數據點追加到這些內部列表中,以便它們每個中最終都有60個元素。

請告訴我如何做這種轉變。

+2

你有沒有編寫任何代碼?你具體在哪裏卡住? – shmosel

+0

你從哪裏得到'[1]'和'[6]'?它應該是「[1.6]」還是「[8.78]」? – Sedrick

+0

我已經編寫了代碼,需要花費1小時的數據,並將其重新構建到包含Map的每分鐘Map中,其中鍵是字段名稱,值是值的列表(按順序)在那一刻的領域。我堅持嘗試將我描述的轉換應用於給定段的數分鐘數據。在這種情況下,段是處理器數據。 – Andrew

回答

0

由於沒有解釋,這是一個可怕的答案。你可以閱讀代碼,看看你能否理解。如果我得到的票數足夠多,我會在幾天內刪除它。

import java.util.ArrayList; 
import java.util.LinkedHashMap; 
import java.util.List; 
import java.util.Map; 

/** 
* 
* @author Sedrick 
*/ 
public class JavaApplication3 { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 

     String str1 = "\"processor_time\":\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\"," 
        + "\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\"," 
        + "\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\",\"08:59:01 AM\","; 

     String str2 = "\"processor_CPU\":\"all\",0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23"; 
     String str3 = "\"processor_%user\":1.6,8.78,2.32,1.67,1.78,1.23,3.83,3.57,0.93,0.62,0.32,0.48,0.37,3.94,1.25,1.22,1.04,0.78,0.65,1.2,0.57,0.4,0.87,0.25,0.38"; 
     String str4 = "\"processor_%nice\":0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"; 
     String str5 = "\"processor_%system\":0.4,2.57,0.65,0.45,0.62,0.32,0.93,0.22,0.2,0.17,0.15,0.15,0.13,0.75,0.32,0.18,0.37,0.25,0.22,0.13,0.22,0.1,0.22,0.08,0.22"; 
     String str6 = "\"processor_%iowait\":0.01,0,0.1,0,0,0,0,0,0,0,0,0,0,0,0.03,0,0,0,0,0,0,0,0,0,0"; 
     String str7 = "\"processor_%steal\":0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"; 
     String str8 = "\"processor_%idle\":97.99,88.65,96.93,97.88,97.6,98.45,95.23,96.22,98.87,99.22,99.53,99.37,99.5,95.31,98.4,98.6,98.6,98.97,99.13,98.67,99.22,99.5,98.92,99.67,99.4"; 

     List<String> strings = new ArrayList(); 
     strings.add(str1); 
     strings.add(str2); 
     strings.add(str3); 
     strings.add(str4); 
     strings.add(str5); 
     strings.add(str6); 
     strings.add(str7); 
     strings.add(str8); 

     Map<String, List<String>> map1 = new LinkedHashMap(); 
     Map<String, List<String>> map2 = new LinkedHashMap(); 


     for(int i = 0; i < 2; i++) 
     { 
      String key = stringToList(strings.get(i)).get(0); 
      List<String> values = stringToList(strings.get(i)).subList(1, stringToList(strings.get(i)).size() - 1); 
      map1.put(key, values); 
     } 

     List<String> keyHolder = new ArrayList();   
     for(int i = 2; i < strings.size(); i++) 
     { 
      String key = stringToList(strings.get(i)).get(0); 
      List<String> values = stringToList(strings.get(i)).subList(1, stringToList(strings.get(i)).size() - 1); 
      map2.put(key, values); 
      keyHolder.add(key); 
     }   

     StringBuilder stringBuilder = new StringBuilder(); 

     stringBuilder.append("{\n"); 
     for(int i = 0; i < keyHolder.size(); i++)//Take first two 
     { 
      stringBuilder.append("\t").append(keyHolder.get(i)).append(": {\n"); 
      for(int z = 0; z < map1.get("\"processor_CPU\"").size(); z++) 
      { 
       stringBuilder.append("\t\t\"CPU_").append(map1.get("\"processor_CPU\"").get(z).replace("\"", "")).append("\": [").append(map2.get(keyHolder.get(i)).get(z)).append("],\n"); 
      } 

      String tempString = stringBuilder.substring(0, stringBuilder.length() - 2); 
      stringBuilder.setLength(0); 
      stringBuilder.append(tempString).append("\n\t},\n"); 
     } 

     String tempString = stringBuilder.substring(0, stringBuilder.length() - 2); 
     stringBuilder.setLength(0); 
     stringBuilder.append(tempString).append("\n}"); 

     System.out.println(stringBuilder.toString()); 
    } 


    static public List<String> stringToList(String str) 
    { 
     List<String> tempList = new ArrayList(); 

     String key = str.split(":", 2)[0].trim(); 
     String[] tailItems = str.split(":", 2)[1].trim().split(","); 

     tempList.add(key); 
     for(int i = 0; i < tailItems.length; i++) 
     { 
      tempList.add(tailItems[i].trim()); 
     } 

     return tempList; 
    } 
} 

Outputun:

{ 
    "processor_%user": { 
     "CPU_all": [1.6], 
     "CPU_0": [8.78], 
     "CPU_1": [2.32], 
     "CPU_2": [1.67], 
     "CPU_3": [1.78], 
     "CPU_4": [1.23], 
     "CPU_5": [3.83], 
     "CPU_6": [3.57], 
     "CPU_7": [0.93], 
     "CPU_8": [0.62], 
     "CPU_9": [0.32], 
     "CPU_10": [0.48], 
     "CPU_11": [0.37], 
     "CPU_12": [3.94], 
     "CPU_13": [1.25], 
     "CPU_14": [1.22], 
     "CPU_15": [1.04], 
     "CPU_16": [0.78], 
     "CPU_17": [0.65], 
     "CPU_18": [1.2], 
     "CPU_19": [0.57], 
     "CPU_20": [0.4], 
     "CPU_21": [0.87], 
     "CPU_22": [0.25] 
    }, 
    "processor_%nice": { 
     "CPU_all": [0], 
     "CPU_0": [0], 
     "CPU_1": [0], 
     "CPU_2": [0], 
     "CPU_3": [0], 
     "CPU_4": [0], 
     "CPU_5": [0], 
     "CPU_6": [0], 
     "CPU_7": [0], 
     "CPU_8": [0], 
     "CPU_9": [0], 
     "CPU_10": [0], 
     "CPU_11": [0], 
     "CPU_12": [0], 
     "CPU_13": [0], 
     "CPU_14": [0], 
     "CPU_15": [0], 
     "CPU_16": [0], 
     "CPU_17": [0], 
     "CPU_18": [0], 
     "CPU_19": [0], 
     "CPU_20": [0], 
     "CPU_21": [0], 
     "CPU_22": [0] 
    }, 
    "processor_%system": { 
     "CPU_all": [0.4], 
     "CPU_0": [2.57], 
     "CPU_1": [0.65], 
     "CPU_2": [0.45], 
     "CPU_3": [0.62], 
     "CPU_4": [0.32], 
     "CPU_5": [0.93], 
     "CPU_6": [0.22], 
     "CPU_7": [0.2], 
     "CPU_8": [0.17], 
     "CPU_9": [0.15], 
     "CPU_10": [0.15], 
     "CPU_11": [0.13], 
     "CPU_12": [0.75], 
     "CPU_13": [0.32], 
     "CPU_14": [0.18], 
     "CPU_15": [0.37], 
     "CPU_16": [0.25], 
     "CPU_17": [0.22], 
     "CPU_18": [0.13], 
     "CPU_19": [0.22], 
     "CPU_20": [0.1], 
     "CPU_21": [0.22], 
     "CPU_22": [0.08] 
    }, 
    "processor_%iowait": { 
     "CPU_all": [0.01], 
     "CPU_0": [0], 
     "CPU_1": [0.1], 
     "CPU_2": [0], 
     "CPU_3": [0], 
     "CPU_4": [0], 
     "CPU_5": [0], 
     "CPU_6": [0], 
     "CPU_7": [0], 
     "CPU_8": [0], 
     "CPU_9": [0], 
     "CPU_10": [0], 
     "CPU_11": [0], 
     "CPU_12": [0], 
     "CPU_13": [0.03], 
     "CPU_14": [0], 
     "CPU_15": [0], 
     "CPU_16": [0], 
     "CPU_17": [0], 
     "CPU_18": [0], 
     "CPU_19": [0], 
     "CPU_20": [0], 
     "CPU_21": [0], 
     "CPU_22": [0] 
    }, 
    "processor_%steal": { 
     "CPU_all": [0], 
     "CPU_0": [0], 
     "CPU_1": [0], 
     "CPU_2": [0], 
     "CPU_3": [0], 
     "CPU_4": [0], 
     "CPU_5": [0], 
     "CPU_6": [0], 
     "CPU_7": [0], 
     "CPU_8": [0], 
     "CPU_9": [0], 
     "CPU_10": [0], 
     "CPU_11": [0], 
     "CPU_12": [0], 
     "CPU_13": [0], 
     "CPU_14": [0], 
     "CPU_15": [0], 
     "CPU_16": [0], 
     "CPU_17": [0], 
     "CPU_18": [0], 
     "CPU_19": [0], 
     "CPU_20": [0], 
     "CPU_21": [0], 
     "CPU_22": [0] 
    }, 
    "processor_%idle": { 
     "CPU_all": [97.99], 
     "CPU_0": [88.65], 
     "CPU_1": [96.93], 
     "CPU_2": [97.88], 
     "CPU_3": [97.6], 
     "CPU_4": [98.45], 
     "CPU_5": [95.23], 
     "CPU_6": [96.22], 
     "CPU_7": [98.87], 
     "CPU_8": [99.22], 
     "CPU_9": [99.53], 
     "CPU_10": [99.37], 
     "CPU_11": [99.5], 
     "CPU_12": [95.31], 
     "CPU_13": [98.4], 
     "CPU_14": [98.6], 
     "CPU_15": [98.6], 
     "CPU_16": [98.97], 
     "CPU_17": [99.13], 
     "CPU_18": [98.67], 
     "CPU_19": [99.22], 
     "CPU_20": [99.5], 
     "CPU_21": [98.92], 
     "CPU_22": [99.67] 
    } 
} 
BUILD SUCCESSFUL (total time: 0 seconds) 
相關問題