以下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
吗?
答案 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”}