【问题标题】:AWS Kinesis Firehose: how to put multiple files containing JSONs using aws cli & bashAWS Kinesis Firehose:如何使用 aws cli 和 bash 放置多个包含 JSON 的文件
【发布时间】:2019-06-25 21:17:05
【问题描述】:

我有 >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

所以有两个问题:

  1. 如何将文件转换为适用的格式
  2. 还有,如何遍历所有文件

【问题讨论】:

    标签: amazon-web-services shell aws-cli amazon-kinesis-firehose


    【解决方案1】:
    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
    

    【讨论】:

    • 没有jq有办法吗?仅使用 bin/bash 命令
    • IMO ,会有,但不建议这样做,因为 jq 与其他工具不同,它是 json 感知工具。所以他们在某个地方会犯一些错误。它们不能像 jq 那样健壮。
    • 这是一些基本的sed -r 's/\{|,/&amp;\n/g;s/\}/\n&amp;/g' input.json |awk 'BEGIN{print "["}{print}END{print "]"}' 你可能需要进一步改进它。
    • @stepandel 如果你的 shell 中安装了 python,你能试试答案的第二部分吗
    • 我在 aws ec2 上运行 Linux VM,理想情况下不想在其上安装任何东西。所以希望有办法只用 bash 来做到这一点
    猜你喜欢
    • 2021-05-26
    • 2019-09-29
    • 2016-01-05
    • 2018-07-02
    • 2021-02-17
    • 2016-12-05
    • 1970-01-01
    • 2020-02-08
    • 2021-05-09
    相关资源
    最近更新 更多