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_time
和processor_CPU
領域。
最終,我的目標是循環訪問數據並將每分鐘的數據點追加到這些內部列表中,以便它們每個中最終都有60個元素。
請告訴我如何做這種轉變。
你有沒有編寫任何代碼?你具體在哪裏卡住? – shmosel
你從哪裏得到'[1]'和'[6]'?它應該是「[1.6]」還是「[8.78]」? – Sedrick
我已經編寫了代碼,需要花費1小時的數據,並將其重新構建到包含Map的每分鐘Map中,其中鍵是字段名稱,值是值的列表(按順序)在那一刻的領域。我堅持嘗試將我描述的轉換應用於給定段的數分鐘數據。在這種情況下,段是處理器數據。 – Andrew