JSONArray排序

JSONArray中封装的是JSONObject,根据JSONObject中的属性排序。JSONObject格式如下:

[ {
  "status" : "COMPLETE",
  "stageId" : 16,
  "attemptId" : 0,
  "numTasks" : 1,
  "numActiveTasks" : 0,
  "numCompleteTasks" : 1,
  "numFailedTasks" : 0,
  "numKilledTasks" : 0,
  "numCompletedIndices" : 1,
  "executorRunTime" : 5777,
  "executorCpuTime" : 110747908,
  "submissionTime" : "2021-11-16T19:26:17.787GMT",
  "firstTaskLaunchedTime" : "2021-11-16T19:26:17.825GMT",
  "completionTime" : "2021-11-16T19:26:23.725GMT",
  "inputBytes" : 411861,
  "inputRecords" : 1785,
  "outputBytes" : 411861,
  "outputRecords" : 1785,
  "shuffleReadBytes" : 0,
  "shuffleReadRecords" : 0,
  "shuffleWriteBytes" : 0,
  "shuffleWriteRecords" : 0,
  "memoryBytesSpilled" : 0,
  "diskBytesSpilled" : 0,
  "name" : "processCmd at CliDriver.java:381",
  "description" : "Merge Small File",
  "details" : "",
  "schedulingPool" : "default",
  "rddIds" : [ 64, 63 ],
  "accumulatorUpdates" : [ ],
  "killedTasksSummary" : { }
}, {
  "status" : "COMPLETE",
  "stageId" : 15,
  "attemptId" : 0,
  "numTasks" : 2400,
  "numActiveTasks" : 0,
  "numCompleteTasks" : 2400,
  "numFailedTasks" : 0,
  "numKilledTasks" : 0,
  "numCompletedIndices" : 2400,
  "executorRunTime" : 579666,
  "executorCpuTime" : 137432903086,
  "submissionTime" : "2021-11-16T19:26:10.905GMT",
  "firstTaskLaunchedTime" : "2021-11-16T19:26:10.991GMT",
  "completionTime" : "2021-11-16T19:26:17.584GMT",
  "inputBytes" : 0,
  "inputRecords" : 0,
  "outputBytes" : 662353,
  "outputRecords" : 1785,
  "shuffleReadBytes" : 462441,
  "shuffleReadRecords" : 1853,
  "shuffleWriteBytes" : 0,
  "shuffleWriteRecords" : 0,
  "memoryBytesSpilled" : 0,
  "diskBytesSpilled" : 0,
  "name" : "processCmd at CliDriver.java:381",
  "description" : "",
  "schedulingPool" : "default",
  "rddIds" : [ 62, 60, 61, 55, 54, 59 ],
  "accumulatorUpdates" : [ ],
  "killedTasksSummary" : { }
}, {
  "status" : "COMPLETE",
  "stageId" : 14,
  "attemptId" : 0,
  "numTasks" : 2400,
  "numActiveTasks" : 0,
  "numCompleteTasks" : 2400,
  "numFailedTasks" : 0,
  "numKilledTasks" : 0,
  "numCompletedIndices" : 2400,
  "executorRunTime" : 260509,
  "executorCpuTime" : 60778532996,
  "submissionTime" : "2021-11-16T19:26:04.591GMT",
  "firstTaskLaunchedTime" : "2021-11-16T19:26:04.620GMT",
  "completionTime" : "2021-11-16T19:26:10.861GMT",
  "inputBytes" : 0,
  "inputRecords" : 0,
  "outputBytes" : 0,
  "outputRecords" : 0,
  "shuffleReadBytes" : 204204,
  "shuffleReadRecords" : 1993,
  "shuffleWriteBytes" : 455994,
  "shuffleWriteRecords" : 1785,
  "memoryBytesSpilled" : 0,
  "diskBytesSpilled" : 0,
  "name" : "processCmd at CliDriver.java:381",
  "description" : "",
  "schedulingPool" : "default",
  "rddIds" : [ 53, 52, 51 ],
  "accumulatorUpdates" : [ ],
  "killedTasksSummary" : { }
}, {
  "status" : "COMPLETE",
  "stageId" : 13,
  "attemptId" : 0,
  "numTasks" : 1,
  "numActiveTasks" : 0,
  "numCompleteTasks" : 1,
  "numFailedTasks" : 0,
  "numKilledTasks" : 0,
  "numCompletedIndices" : 1,
  "executorRunTime" : 4043,
  "executorCpuTime" : 2109142086,
  "submissionTime" : "2021-11-16T19:26:00.483GMT",
  "firstTaskLaunchedTime" : "2021-11-16T19:26:00.491GMT",
  "completionTime" : "2021-11-16T19:26:04.581GMT",
  "inputBytes" : 46933,
  "inputRecords" : 2236,
  "outputBytes" : 0,
  "outputRecords" : 0,
  "shuffleReadBytes" : 0,
  "shuffleReadRecords" : 0,
  "shuffleWriteBytes" : 204204,
  "shuffleWriteRecords" : 1993,
  "memoryBytesSpilled" : 0,
  "diskBytesSpilled" : 0,
  "name" : "processCmd at CliDriver.java:381",
  "description" : "",
  "schedulingPool" : "default",
  "rddIds" : [ 50, 49, 48 ],
  "accumulatorUpdates" : [ ],
  "killedTasksSummary" : { }
}, {
  "status" : "COMPLETE",
  "stageId" : 12,
  "attemptId" : 0,
  "numTasks" : 1,
  "numActiveTasks" : 0,
  "numCompleteTasks" : 1,
  "numFailedTasks" : 0,
  "numKilledTasks" : 0,
  "numCompletedIndices" : 1,
  "executorRunTime" : 1683,
  "executorCpuTime" : 1399481189,
  "submissionTime" : "2021-11-16T19:26:00.471GMT",
  "firstTaskLaunchedTime" : "2021-11-16T19:26:00.482GMT",
  "completionTime" : "2021-11-16T19:26:02.229GMT",
  "inputBytes" : 295328,
  "inputRecords" : 40742,
  "outputBytes" : 0,
  "outputRecords" : 0,
  "shuffleReadBytes" : 0,
  "shuffleReadRecords" : 0,
  "shuffleWriteBytes" : 6447,
  "shuffleWriteRecords" : 68,
  "memoryBytesSpilled" : 0,
  "diskBytesSpilled" : 0,
  "name" : "processCmd at CliDriver.java:381",
  "description" : "",
  "details" : "",
  "schedulingPool" : "default",
  "rddIds" : [ 58, 57, 56 ],
  "accumulatorUpdates" : [ ],
  "killedTasksSummary" : { }
}, {
  "status" : "COMPLETE",
  "stageId" : 11,
  "attemptId" : 0,
  "numTasks" : 2400,
  "numActiveTasks" : 0,
  "numCompleteTasks" : 2400,
  "numFailedTasks" : 0,
  "numKilledTasks" : 0,
  "numCompletedIndices" : 2400,
  "executorRunTime" : 42127,
  "executorCpuTime" : 13898517691,
  "submissionTime" : "2021-11-16T19:25:50.874GMT",
  "firstTaskLaunchedTime" : "2021-11-16T19:25:54.703GMT",
  "completionTime" : "2021-11-16T19:25:59.929GMT",
  "inputBytes" : 0,
  "inputRecords" : 0,
  "outputBytes" : 0,
  "outputRecords" : 0,
  "shuffleReadBytes" : 69347,
  "shuffleReadRecords" : 627,
  "shuffleWriteBytes" : 0,
  "shuffleWriteRecords" : 0,
  "memoryBytesSpilled" : 0,
  "diskBytesSpilled" : 0,
  "name" : "call at FutureTask.java:266",
  "description" : "broadcast exchange (runId a1cb60d9-1e23-402e-be95-af1295483838)",
  "details" : "",
  "schedulingPool" : "default",
  "rddIds" : [ 47, 45, 46 ],
  "accumulatorUpdates" : [ ],
  "killedTasksSummary" : { }
}]

排序方法如下:

JSONArray sparkStages = 如上;
sparkStages.sort(Comparator.comparing(stage -> {
            JSONObject stageJson = (JSONObject) stage;
            String firstTaskLaunchedTime = stageJson.getString("firstTaskLaunchedTime");
            String completionTime = stageJson.getString("completionTime");
            Long duration = DataFormatUtils.GTMtoTimestamp(completionTime) - DataFormatUtils.GTMtoTimestamp(firstTaskLaunchedTime);
            stageJson.put("duration",duration);
            return duration;
        }).reversed());
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值