AMBARI +如何在JSON REST API中设置值

时间:2020-02-24 21:32:58

标签: json bash api ambari

以下API示例,将stop中的kafka服务ambari

export service=kafka

curl -u admin:admin -i -H 'X-Requested-By: ambari' -X PUT -d '{"RequestInfo":{"context":"_PARSE_.STOP.$service","operation_level":{"level":"SERVICE","cluster_name":"$CLUSTER_NAME","service_name":"$service"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' http://$HOST:8080/api/v1/clusters/$CLUSTER_NAME/services/$service 

问题出在语法上-_PARSE_.STOP.$service

,我们看到实际服务价值-kafka尚未在- PARSE .STOP。$ service

中设置

所以ambri看到的名称是- PARSE .STOP。$ service,而不是 PARSE .STOP.kafka

您知道如何在kafka语法中设置值json吗?

1 个答案:

答案 0 :(得分:0)

当单引号类型的文字用于字符串时,变量不会被替换,因此您需要将字符串分解为多个部分,以便能够在结果字符串中插入一些值。

export service=kafka

curl -u admin:admin -i \
-H 'X-Requested-By: ambari' \
-X PUT \
-d '{"RequestInfo":{"context":"_PARSE_.STOP.'$service'","operation_level":{"level":"SERVICE","cluster_name":"'$CLUSTER_NAME'","service_name":"$service"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' \
http://$HOST:8080/api/v1/clusters/$CLUSTER_NAME/services/$service

下面是一个简化的示例,以使其清楚:

#!/bin/bash
service=XXX

json='{"A":"$service"}'
echo "wrong JSON: $json"

json='{"A":"'$service'"}' #this string consist of 3 parts '{"A":"' + $service + '"}'
echo "good JSON: $json"

输出:

错误的JSON:{“ A”:“ $ service”}

良好的JSON:{“ A”:“ XXX”}