简体   繁体   English

json 格式的 InfluxDB 查询转换为带有 jq 的 csv,包括标签和字段

[英]InfluxDB query in json format transform to csv with jq including tags and fields

I want to process data with a bash script but have trouble to get the InfluxDB output to the desired csv output with all tags and fields. I want to process data with a bash script but have trouble to get the InfluxDB output to the desired csv output with all tags and fields.

Below an example output from an influx query:下面是来自流入查询的示例 output:

{
  "results": [
    {
      "series": [
        {
          "name": "tickerPrice",
          "tags": {
            "symbol": "AAVE",
            "symbolTo": "EUR"
          },
          "columns": [
            "time",
            "priceMean"
          ],
          "values": [
            [
              1614402874120627200,
              282.398263888889
            ]
          ]
        },
        {
          "name": "tickerPrice",
          "tags": {
            "symbol": "BTC",
            "symbolTo": "EUR"
          },
          "columns": [
            "time",
            "priceMean"
          ],
          "values": [
            [
              1614402874120627200,
              39189.756944444445
            ]
          ]
        }
      ]
    }
  ]
}

And I would like to transform it to:我想将其转换为:

"name","symbol","symbolTo","time","priceMean"
"tickerPrice","AAVE","EUR",1614402874120627200,282.398263888889
"tickerPrice","BTC","EUR",1614402874120627200,39189.756944444445

I have managed (google) to get the fields to a csv format but till now not managed to get all data in the csv.我已经设法(谷歌)将字段转换为 csv 格式,但到目前为止还没有设法获取 csv 中的所有数据。 Here is the commands that I use for that:这是我使用的命令:

$ jq -r '(.results[0].series[0].columns), (.results[0].series[].values[])'

Because this is not the only query I want to do it would be nice that it is universal for the content, so the number of fields and tags could be different.因为这不是我想要做的唯一查询,所以它对内容是通用的会很好,所以字段和标签的数量可能会有所不同。

The following produces the required output in a way that allows for multiple values of "time" in each .values array, but does not refer to the specific headers except for "name":以下以允许每个.values数组中的多个“时间”值的方式生成所需的 output,但不引用除“名称”之外的特定标题:

def headers:
 (.tags | keys_unsorted) as $tags
 | (["name"] + $tags + .columns); 

.results[0]
| (.series[0] | headers),
  (.series[] | ([.name, .tags[]] + .values[]))
| @csv

This of course assumes that the separate "series" are conformal.这当然假设单独的“系列”是保形的。

Why you just don't specify csv format directly in influxdb CLI https://docs.influxdata.com/influxdb/v1.8/tools/shell/ :为什么你不直接在 influxdb CLI https://docs.influxdata.com/influxdb/v1.8/tools/shell/中指定 csv 格式:

-format 'json|csv|column' Specifies the format of the server responses. -format 'json|csv|column' 指定服务器响应的格式。

So you won't need any result post processing.因此,您不需要任何结果后处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM