jq 过滤器将 Elasticsearch 结果转换为 CSV
jq filter to convert Elasticsearch result to CSV
我正在尝试使用 jq 将我的 json 搜索结果从 Elasticsearch 转换为 csv 格式。
我实际的 jq 命令如下所示:
.aggregations["byLabels"].buckets | map({key,doc_count,range: .byRange.buckets | map({str:(.from_as_string + "-" + .to_as_string), dc:.doc_count} | [.str, .dc])} | flatten )
实际结果:
[
[
"Page 1",
15001,
"0.0-1000.0",
7,
"1000.0-2000.0",
1005,
"2000.0-",
8907
],
[
"Page 2",
340,
"0.0-1000.0",
23,
"1000.0-2000.0",
261,
"2000.0-",
18
]
]
但我需要的 csv 输出应该是这样的(带或不带引号):
"Page1",15001,"0.0-1000.0",7,1000.0-2000.0,1005,"2000.0-*",8907
"Page2",340,"0.0-1000.0",23,1000.0-2000.0,261,"2000.0-*",18
我尝试添加额外的 add[] 或 flatten 或 join(",") 过滤器,但仅在尝试使用 |@csv 时出现错误。
这是我第一次使用 jq,所以我认为这对于任何有经验的 jq 用户来说都不是问题。
原始 json 文件如下所示:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 51632,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"byLabels" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 2478,
"buckets" : [ {
"key" : "Page 1",
"doc_count" : 15001,
"byRange" : {
"buckets" : {
"0.0-1000.0" : {
"from" : 0.0,
"from_as_string" : "0.0",
"to" : 1000.0,
"to_as_string" : "1000.0",
"doc_count" : 7
},
"1000.0-2000.0" : {
"from" : 1000.0,
"from_as_string" : "1000.0",
"to" : 2000.0,
"to_as_string" : "2000.0",
"doc_count" : 1005
},
"2000.0-*" : {
"from" : 2000.0,
"from_as_string" : "2000.0",
"doc_count" : 8907
}
}
}
}, {
"key" : "Page 2",
"doc_count" : 340,
"byRange" : {
"buckets" : {
"0.0-1000.0" : {
"from" : 0.0,
"from_as_string" : "0.0",
"to" : 1000.0,
"to_as_string" : "1000.0",
"doc_count" : 23
},
"1000.0-2000.0" : {
"from" : 1000.0,
"from_as_string" : "1000.0",
"to" : 2000.0,
"to_as_string" : "2000.0",
"doc_count" : 261
},
"2000.0-*" : {
"from" : 2000.0,
"from_as_string" : "2000.0",
"doc_count" : 18
}
}
}
} ]
}
}
}
感谢您的帮助。
@csv 仅适用于平面数组,因此您可以添加 '| .[] | @csv' 到您的管道。
我正在尝试使用 jq 将我的 json 搜索结果从 Elasticsearch 转换为 csv 格式。
我实际的 jq 命令如下所示:
.aggregations["byLabels"].buckets | map({key,doc_count,range: .byRange.buckets | map({str:(.from_as_string + "-" + .to_as_string), dc:.doc_count} | [.str, .dc])} | flatten )
实际结果:
[
[
"Page 1",
15001,
"0.0-1000.0",
7,
"1000.0-2000.0",
1005,
"2000.0-",
8907
],
[
"Page 2",
340,
"0.0-1000.0",
23,
"1000.0-2000.0",
261,
"2000.0-",
18
]
]
但我需要的 csv 输出应该是这样的(带或不带引号):
"Page1",15001,"0.0-1000.0",7,1000.0-2000.0,1005,"2000.0-*",8907
"Page2",340,"0.0-1000.0",23,1000.0-2000.0,261,"2000.0-*",18
我尝试添加额外的 add[] 或 flatten 或 join(",") 过滤器,但仅在尝试使用 |@csv 时出现错误。
这是我第一次使用 jq,所以我认为这对于任何有经验的 jq 用户来说都不是问题。
原始 json 文件如下所示:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 51632,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"byLabels" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 2478,
"buckets" : [ {
"key" : "Page 1",
"doc_count" : 15001,
"byRange" : {
"buckets" : {
"0.0-1000.0" : {
"from" : 0.0,
"from_as_string" : "0.0",
"to" : 1000.0,
"to_as_string" : "1000.0",
"doc_count" : 7
},
"1000.0-2000.0" : {
"from" : 1000.0,
"from_as_string" : "1000.0",
"to" : 2000.0,
"to_as_string" : "2000.0",
"doc_count" : 1005
},
"2000.0-*" : {
"from" : 2000.0,
"from_as_string" : "2000.0",
"doc_count" : 8907
}
}
}
}, {
"key" : "Page 2",
"doc_count" : 340,
"byRange" : {
"buckets" : {
"0.0-1000.0" : {
"from" : 0.0,
"from_as_string" : "0.0",
"to" : 1000.0,
"to_as_string" : "1000.0",
"doc_count" : 23
},
"1000.0-2000.0" : {
"from" : 1000.0,
"from_as_string" : "1000.0",
"to" : 2000.0,
"to_as_string" : "2000.0",
"doc_count" : 261
},
"2000.0-*" : {
"from" : 2000.0,
"from_as_string" : "2000.0",
"doc_count" : 18
}
}
}
} ]
}
}
}
感谢您的帮助。
@csv 仅适用于平面数组,因此您可以添加 '| .[] | @csv' 到您的管道。