我正在使用来自系统监控工具的stats(特定的sar),我正在尝试重新构建它们。我收到了JSON格式的所有数据。在Java中,收到的JSON表示为Map<String, Object>
并存储在变量dataMap
中。
以下是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个元素。
请告知我如何进行这种转变。
答案 0 :(得分: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)