AWS Kinesis Firehose:如何使用 aws cli & bash 放置包含 JSON 的多个文件

AWS Kinesis Firehose: how to put multiple files containing JSONs using aws cli & bash

我有超过 100 个文件,其中每一行都是 json。它看起来像这样(没有逗号和没有 []):

{"one":"one","two":{"tree":...}}
{"one":"one","two":{"tree":...}}
...
{"one":"one","two":{"tree":...}}

为了能够使用 aws firehose put-record-batch,文件需要采用以下格式:

[
  {
    "Data": blob
  },
  {
    "Data": blob
  },
  ...
]

我想将所有这些文件从终端放入 aws Firehose。

我想写一个 shell 脚本,看起来像这样:

for file in files
do
  aws firehose put-record-batch --delivery-stream-name <name> --records file://$file
done

所以有 2 个问题:

  1. 如何将文件转换成适用的格式
  2. 以及,如何遍历所有文件
for file in *.json;
do
    jq -s . "${file}" >${file}.tmp && mv ${file}.tmp $file    
done

这将读取当前目录下的所有json文件并将其更改为所需的形式并保存到文件中。

或者,如果您没有 jq,这里是使用 python's json 模块的替代方法。

for file in *.json;do
  while read line ; do 
      echo $line | python -m json.tool 
  done < ${file} |awk 'BEGIN{print "["}{print}END{print "]"}'
done