使用JQ替换后,将JSON传递给AWS胶水创建工作

发布于 2025-02-04 01:10:07 字数 1728 浏览 4 评论 0原文

我有以下我执行的bash脚本,以通过CLI创建新的胶作业:

#!/usr/bin/env bash

set -e
NAME=$1
PROFILE=$2
SCRIPT_LOCATION='s3://bucket/scripts/'$1'.py'

echo [*]--- Creating new job on AWS
aws glue create-job --profile $PROFILE --name $NAME --cli-input-json | jq '.Command.ScriptLocation = '\"$SCRIPT_LOCATION\"'' ./resources/config.json

我正在使用 jq ,因为我需要在运行时替换的值之一,然后才能通过 。如何将替换值的JSON传递给此命令?截至目前,它打印出JSON内容(尽管已更换了值)。 在上面运行命令会导致以下错误:

[*]--- Creating new job on AWS
{
  "Description": "Template for Glue Job",
  "LogUri": "",
  "Role": "arn:aws:iam::11111111111:role/role",
  "ExecutionProperty": {
    "MaxConcurrentRuns": 1
  },
  "Command": {
    "Name": "glueetl",
    "ScriptLocation": "s3://bucket/scripts/script.py",
    "PythonVersion": "3"
  },
  "DefaultArguments": {
    "--TempDir": "s3://temp/admin/",
    "--job-bookmark-option": "job-bookmark-disable",
    "--enable-metrics": "",
    "--enable-glue-datacatalog": "",
    "--enable-continuous-cloudwatch-log": "",
    "--enable-spark-ui": "true",
    "--spark-event-logs-path": "s3://assets/sparkHistoryLogs/"
  },
  "NonOverridableArguments": {
    "KeyName": ""
  },
  "MaxRetries": 0,
  "AllocatedCapacity": 0,
  "Timeout": 2880,
  "MaxCapacity": 0,
  "Tags": {
    "KeyName": ""
  },
  "NotificationProperty": {
    "NotifyDelayAfter": 60
  },
  "GlueVersion": "3.0",
  "NumberOfWorkers": 2,
  "WorkerType": "G.1X"
}

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

aws.exe: error: argument --cli-input-json: expected one argument

I have the following bash script that I execute in order to create new Glue Job via CLI:

#!/usr/bin/env bash

set -e
NAME=$1
PROFILE=$2
SCRIPT_LOCATION='s3://bucket/scripts/'$1'.py'

echo [*]--- Creating new job on AWS
aws glue create-job --profile $PROFILE --name $NAME --cli-input-json | jq '.Command.ScriptLocation = '\"$SCRIPT_LOCATION\"'' ./resources/config.json

I'm using jq as i need one of the values to be replaced on runtime before i pass the .json as --cli-input-json argument. How can i pass json with replaced value to this command? As of now, it prints out the json content (although with value already replaced).
Running the command above causes the following error:

[*]--- Creating new job on AWS
{
  "Description": "Template for Glue Job",
  "LogUri": "",
  "Role": "arn:aws:iam::11111111111:role/role",
  "ExecutionProperty": {
    "MaxConcurrentRuns": 1
  },
  "Command": {
    "Name": "glueetl",
    "ScriptLocation": "s3://bucket/scripts/script.py",
    "PythonVersion": "3"
  },
  "DefaultArguments": {
    "--TempDir": "s3://temp/admin/",
    "--job-bookmark-option": "job-bookmark-disable",
    "--enable-metrics": "",
    "--enable-glue-datacatalog": "",
    "--enable-continuous-cloudwatch-log": "",
    "--enable-spark-ui": "true",
    "--spark-event-logs-path": "s3://assets/sparkHistoryLogs/"
  },
  "NonOverridableArguments": {
    "KeyName": ""
  },
  "MaxRetries": 0,
  "AllocatedCapacity": 0,
  "Timeout": 2880,
  "MaxCapacity": 0,
  "Tags": {
    "KeyName": ""
  },
  "NotificationProperty": {
    "NotifyDelayAfter": 60
  },
  "GlueVersion": "3.0",
  "NumberOfWorkers": 2,
  "WorkerType": "G.1X"
}

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

aws.exe: error: argument --cli-input-json: expected one argument

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熊抱啵儿 2025-02-11 01:10:07

命令行
aws胶创建job - profile $ profile -name $ name-cli-input-json | JQ'.Command.ScriptLocation ='\“ $ Script_location \”'''./ resources/config.json
执行命令
AWS胶创建JOB - profile $ profile -name $ name-cli-input-json
采用其标准输出并将其用作输入
JQ'.Command.ScriptLocation ='\“ $ Script_location \”''./ resources/config.json
(它将忽略输入并从给出的文件中读取)。另请注意,$ script_location中的空白或空格会破坏您的脚本,因为它没有引用(您的报价已关闭)。

要在另一个命令的参数列表中使用一个命令的输出,您必须使用命令替换outer_command -some-arg” $(innit_command)”

因此,您的命令应成为:

aws glue create-job --profile $PROFILE --name $NAME --cli-input-json "$(jq '.Command.ScriptLocation = "'"$SCRIPT_LOCATION"'"' ./resources/config.json)"
# or simplified with only double quotes:
aws glue create-job --profile $PROFILE --name $NAME --cli-input-json "$(jq ".Command.ScriptLocation = \"$SCRIPT_LOCATION\"" ./resources/config.json)"

请参阅 https://superuser.com /Question/1306071/aws-cli using-cli-input-json-in-a-a-pipeline 以获取其他示例。

虽然,我必须承认我不是100%确定可以直接在命令行上传递JSON内容。通过查看文档和一些官方示例,看起来此参数期望文件名,而不是JSON文档的内容。因此,实际上您的命令可能需要:

# if "-" filename is specially handled:
jq ".Command.ScriptLocation = \"$SCRIPT_LOCATION\"" ./resources/config.json | aws glue create-job --profile $PROFILE --name $NAME --cli-input-json -
# "-" filename not recognized:
jq ".Command.ScriptLocation = \"$SCRIPT_LOCATION\"" ./resources/config.json > ./resources/config.replaced.json && aws glue create-job --profile $PROFILE --name $NAME --cli-input-json file://./resources/config.replaced.json

让我们知道哪一个起作用。

The command line
aws glue create-job --profile $PROFILE --name $NAME --cli-input-json | jq '.Command.ScriptLocation = '\"$SCRIPT_LOCATION\"'' ./resources/config.json
executes the command
aws glue create-job --profile $PROFILE --name $NAME --cli-input-json,
takes its standard output and uses it as input to
jq '.Command.ScriptLocation = '\"$SCRIPT_LOCATION\"'' ./resources/config.json
(which will ignore the input and read from the file given as argument). Please also note that blanks or spaces in $SCRIPT_LOCATION will break your script, because it is not quoted (your quotes are off).

To use the output of one command in the argument list of another command, you must use Command Substitution: outer_command --some-arg "$(inner_command)".

So your command should become:

aws glue create-job --profile $PROFILE --name $NAME --cli-input-json "$(jq '.Command.ScriptLocation = "'"$SCRIPT_LOCATION"'"' ./resources/config.json)"
# or simplified with only double quotes:
aws glue create-job --profile $PROFILE --name $NAME --cli-input-json "$(jq ".Command.ScriptLocation = \"$SCRIPT_LOCATION\"" ./resources/config.json)"

See https://superuser.com/questions/1306071/aws-cli-using-cli-input-json-in-a-pipeline for additional examples.

Although, I have to admit I am not 100% certain that the JSON content can be passed directly on the command line. From looking at the docs and some official examples, it looks like this parameter expects a file name, not a JSON document's content. So it could be possible that your command in fact needs to be:

# if "-" filename is specially handled:
jq ".Command.ScriptLocation = \"$SCRIPT_LOCATION\"" ./resources/config.json | aws glue create-job --profile $PROFILE --name $NAME --cli-input-json -
# "-" filename not recognized:
jq ".Command.ScriptLocation = \"$SCRIPT_LOCATION\"" ./resources/config.json > ./resources/config.replaced.json && aws glue create-job --profile $PROFILE --name $NAME --cli-input-json file://./resources/config.replaced.json

Let us know which one worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文