You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Shell变量传递转义JSON至AWS Glue创建Schema失败求助

问题原因及解决办法

核心原因

  1. Shell转义符解析冲突:手动执行命令时,你输入的转义双引号(\")会被Shell解析为普通双引号,最终传递给AWS CLI的是合法的JSON Schema。但如果用双引号给变量赋值(比如current_schema="{\"type\":\"record\"}"),Shell会提前解析转义符,导致变量内的Schema丢失必要的转义逻辑,后续传递时容易出现格式错误。
  2. 变量传递的分词破坏:如果传递变量时不用双引号包裹(比如--schema-definition $current_schema),Shell会将变量内容按空格、制表符等分割成多个参数,直接破坏JSON的结构,导致AWS CLI接收到不完整的Schema。

解决办法

方法1:单引号赋值+双引号传递变量

用单引号包裹Schema内容赋值给变量,Shell会原样保留所有字符(包括双引号和特殊符号);传递时用双引号包裹变量,确保整个JSON作为单个参数传递给命令。

# 正确赋值变量(单引号包裹完整Schema)
current_schema='{"type":"record","name":"Test","fields":[{"name":"id","type":"int"},{"name":"username","type":"string"}]}'

# 执行注册命令
aws glue create-schema \
  --registry-name your-registry-name \
  --schema-name your-schema-name \
  --schema-definition "$current_schema" \
  --data-format AVRO

可通过echo "$current_schema"验证变量内容,输出应为完整合法的JSON。

方法2:从文件读取Schema(推荐复杂场景)

将AVRO Schema保存到单独的JSON文件(比如avro-schema.json),通过file://路径传递给AWS CLI,完全避免Shell变量的转义和分词问题。

# 示例avro-schema.json内容
{"type":"record","name":"Test","fields":[{"name":"id","type":"int"},{"name":"username","type":"string"}]}

# 执行注册命令
aws glue create-schema \
  --registry-name your-registry-name \
  --schema-name your-schema-name \
  --schema-definition file://avro-schema.json \
  --data-format AVRO

方法3:用Here Document传递Schema

适合在脚本中直接编写Schema的场景,Here Document会将内容原样传递给命令,无需处理转义。

aws glue create-schema \
  --registry-name your-registry-name \
  --schema-name your-schema-name \
  --data-format AVRO <<EOF
{"type":"record","name":"Test","fields":[{"name":"id","type":"int"},{"name":"username","type":"string"}]}
EOF

内容的提问来源于stack exchange,提问作者DimarZio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 15:25:25