shell 使用Curl命令将多个变量导入气流UI的有效负载结构应该是什么?

bq9c1y66  于 2023-03-19  发布在  Shell
关注(0)|答案(1)|浏览(82)

我尝试使用Curl命令将多个变量导入到Airflow UI。当我尝试使用有效负载导入多个变量时出现问题。
我尝试使用Curl命令将多个变量导入气流UI。对于单个变量,以下命令工作正常。
curl -X 'POST' 'http://10.135.7.13:9081/api/v1/variables' -H 'accept: application/json' -H 'Content-Type: application/json' -d '{"key": "string","value": "string"}' --user 'usr:pwd'
但对于多变量,尝试了所有可能的方法,并得到不同的错误。

**1.**一米一米一

{
  "detail": "{'value': ['Missing data for required field.'], 'key': ['Missing data for required field.'], 'key2': ['Unknown field.'], 'key1': ['Unknown field.']}",
  "status": 400,
  "title": "Invalid Variable schema",
  "type": "https://airflow.apache.org/docs/apache-airflow/2.2.2/stable-rest-api-ref.html#section/Errors/BadRequest"

**2.**一米二米一

{
  "detail": "{'value': ['Missing data for required field.'], 'key': ['Missing data for required field.'], 'variables': ['Unknown field.']}",
  "status": 400,
  "title": "Invalid Variable schema",
  "type": "https://airflow.apache.org/docs/apache-airflow/2.2.2/stable-rest-api-ref.html#section/Errors/BadRequest"

**3.**一米三米一

"detail": "[{'key': 'string', 'value': 'string'}, {'key': 'string1', 'value': 'string1'}] is not of type 'object'",
  "status": 400,
  "title": "Bad Request",
  "type": "https://airflow.apache.org/docs/apache-airflow/2.2.2/stable-rest-api-ref.html#section/Errors/BadRequest"
}

有谁能帮我理解一下这里的有效载荷应该是什么结构。

vnzz0bqm

vnzz0bqm1#

根据文档和代码,一次导入一个变量是唯一可行的方法。
解决方法可以是在数组上执行for循环:

AIRFLOW_URL="http://localhost:8080/api/v1/variables"
AIRFLOW_USERNAME="airflow"
AIRFLOW_PASSWORD="airflow"

# Define the JSON array of variables
VARIABLES='[
    {"key": "var1", "value": "valueA"},
    {"key": "var2", "value": "valueB"},
    {"key": "var3", "value": "valueC"}
]'

# Loop through the JSON array and create each variable using the Airflow REST API
for variable in $(echo "${VARIABLES}" | jq -c '.[]'); do
    key=$(echo "${variable}" | jq -r '.key')
    value=$(echo "${variable}" | jq -r '.value')

    curl -X POST "${AIRFLOW_URL}" \
        -H "Content-Type: application/json" \
        --user "${AIRFLOW_USERNAME}:${AIRFLOW_PASSWORD}" \
        -d "{\"key\": \"${key}\", \"value\": \"${value}\"}"
done

相关问题