我的问题是:如何以表格格式生成JQ输出,用0替换缺少的值?
因此,JQ的输入是以下弹性搜索JSON响应:
{"aggregations": {
"overall": {
"buckets": [
{
"key": "2018-01-18T00:00:00.000Z-2018-01-25T19:33:16.010Z",
"from_as_string": "2018-01-18T00:00:00.000Z",
"to": 1516908796010,
"to_as_string": "2018-01-25T19:33:16.010Z",
"doc_count": 155569,
"agg_per_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "ASSET-DD583",
"doc_count": 3016,
"totalMaxUptime_perDays": {
"buckets": [
{
"key_as_string": "2018-01-22T00:00:00.000Z",
"key": 1516579200000,
"doc_count": 161,
"totalMaxUptime": {
"value": 77598
}
},
{
"key_as_string": "2018-01-23T00:00:00.000Z",
"key": 1516665600000,
"doc_count": 251,
"totalMaxUptime": {
"value": 80789
}
},
{
"key_as_string": "2018-01-24T00:00:00.000Z",
"key": 1516752000000,
"doc_count": 192,
"totalMaxUptime": {
"value": 56885
}
},
{
"key_as_string": "2018-01-25T00:00:00.000Z",
"key": 1516838400000,
"doc_count": 2088,
"totalMaxUptime": {
"value": 7392705
}
}
]
}
},
{
"key": "ASSET-DD568",
"doc_count": 2990,
"totalMaxUptime_perDays": {
"buckets": [
{
"key_as_string": "2018-01-18T00:00:00.000Z",
"key": 1516233600000,
"doc_count": 106,
"totalMaxUptime": {
"value": 31241
}
},
{
"key_as_string": "2018-01-19T00:00:00.000Z",
"key": 1516320000000,
"doc_count": 241,
"totalMaxUptime": {
"value": 2952565
}
},
{
"key_as_string": "2018-01-20T00:00:00.000Z",
"key": 1516406400000,
"doc_count": 326,
"totalMaxUptime": {
"value": 2698235
}
},
{
"key_as_string": "2018-01-21T00:00:00.000Z",
"key": 1516492800000,
"doc_count": 214,
"totalMaxUptime": {
"value": 85436
}
},
{
"key_as_string": "2018-01-22T00:00:00.000Z",
"key": 1516579200000,
"doc_count": 279,
"totalMaxUptime": {
"value": 83201
}
},
{
"key_as_string": "2018-01-23T00:00:00.000Z",
"key": 1516665600000,
"doc_count": 50,
"totalMaxUptime": {
"value": 96467
}
},
{
"key_as_string": "2018-01-24T00:00:00.000Z",
"key": 1516752000000,
"doc_count": 5,
"totalMaxUptime": {
"value": 903
}
},
{
"key_as_string": "2018-01-25T00:00:00.000Z",
"key": 1516838400000,
"doc_count": 1769,
"totalMaxUptime": {
"value": 12337946
}
}
]
}
},
{
"key": "ASSET-42631",
"doc_count": 2899,
"totalMaxUptime_perDays": {
"buckets": [
{
"key_as_string": "2018-01-18T00:00:00.000Z",
"key": 1516233600000,
"doc_count": 132,
"totalMaxUptime": {
"value": 39054
}
},
{
"key_as_string": "2018-01-19T00:00:00.000Z",
"key": 1516320000000,
"doc_count": 172,
"totalMaxUptime": {
"value": 47634
}
},
{
"key_as_string": "2018-01-20T00:00:00.000Z",
"key": 1516406400000,
"doc_count": 214,
"totalMaxUptime": {
"value": 68264
}
},
{
"key_as_string": "2018-01-21T00:00:00.000Z",
"key": 1516492800000,
"doc_count": 220,
"totalMaxUptime": {
"value": 66243
}
},
{
"key_as_string": "2018-01-25T00:00:00.000Z",
"key": 1516838400000,
"doc_count": 128,
"totalMaxUptime": {
"value": 47660
}
}
]
}
}
]
}
}
]
}
}
}
此JSON具有一些固有属性:
-
agg\U per\U name中的bucket数可变。铲斗
-
TotalMaxUptime\u/天。buckets表示从当前日期算起的最后7天内每天的组。每天的totalMaxUptime\u。每个资产的桶数将在1到8之间,每个桶对应于特定日期。
对于给定的示例,JQ的期望输出是一个表格,其中在水平方向上,您有从key\u as\u string开始的日期(在本例中是从2018年1月18日到2018年1月25日),在垂直方向上,有所有资产键(即asset-DD583、asset-DD568等)。表中填充了totalMaxUptime。每个对应日期的值,如果结果中不存在该日期,则应输入“0”值:
XXXXXXXXXXX, 2018-01-18, 2018-01-19, 2018-01-20, 2018-01-21, 2018-01-22, 2018-01-23, 2018-01-24, 2018-01-25
ASSET-DD583, 0, 0, 0, 0, 77598, 80789, 56885, 7392705
ASSET-DD568, 31241, 2952565, 2698235, 85436, 83201, 96467, 903, 12337946
ASSET-42631, 39054, 47634, 68264, 66243, 0, 0, 0, 47660
编辑1:
这就是我取得的成绩:
input.json | jq '.aggregations.overall.buckets[0].agg_per_name.buckets[] | .key + ", " + (.totalMaxUptime_perDays.buckets[] | .key_as_string + ", " + (.totalMaxUptime.value | tostring))' | sed 's/"//g' | sed 's/T00:00:00.000Z//g'> uptime.csv
产生这种输出:
ASSET-DD583, 2018-01-22, 77598
ASSET-DD583, 2018-01-23, 80789
ASSET-DD583, 2018-01-24, 56885
ASSET-DD583, 2018-01-25, 7392705
...............