AMBARI + 如何在 json REST API 中设置值
AMBARI + how to set value in json REST API
以下 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
知道如何在 json
语法中设置值 kafka
吗?
当单引号类型文字用于字符串时,变量不会被替换,因此您需要将字符串分成多个部分,以便能够将一些值插入到结果字符串中。
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"}
以下 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
知道如何在 json
语法中设置值 kafka
吗?
当单引号类型文字用于字符串时,变量不会被替换,因此您需要将字符串分成多个部分,以便能够将一些值插入到结果字符串中。
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"}