参考文档:https://cwiki.apache.org/confluence/display/AMBARI/API+usage+scenarios%2C+troubleshooting%2C+and+other+FAQs
获取集群信息
[root@name1 ~]# curl -u admin:password http://172.16.96.62:8080/api/v1/clusters
{
"href" : "http://172.16.96.62:8080/api/v1/clusters",
"items" : [
{
"href" : "http://172.16.96.62:8080/api/v1/clusters/hdpdev",
"Clusters" : {
"cluster_name" : "hdpdev",
"version" : "HDP-2.6"
}
}
]
}
####等同于
[root@name1 ~]# curl -H "X-Requested-By: ambari" -X GET -u admin:password http://172.16.96.62:8080/api/v1/clusters
{
"href" : "http://172.16.96.62:8080/api/v1/clusters",
"items" : [
{
"href" : "http://172.16.96.62:8080/api/v1/clusters/hdpdev",
"Clusters" : {
"cluster_name" : "hdpdev",
"version" : "HDP-2.6"
}
}
]
}
查询集群主机信息
[root@name1 ~]# curl -u admin:password http://172.16.96.62:8080/api/v1/hosts
{
"href" : "http://172.16.96.62:8080/api/v1/hosts",
"items" : [
{
"href" : "http://172.16.96.62:8080/api/v1/hosts/client1.example.com",
"Hosts" : {
"cluster_name" : "hdpdev",
"host_name" : "client1.example.com"
}
},
{
"href" : "http://172.16.96.62:8080/api/v1/hosts/client2.example.com",
"Hosts" : {
"cluster_name" : "hdpdev",
"host_name" : "client2.example.com"
}
},
{
"href" : "http://172.16.96.62:8080/api/v1/hosts/name1.example.com",
"Hosts" : {
"cluster_name" : "hdpdev",
"host_name" : "name1.example.com"
}
},
############...省略部分输出
]
}
重启服务,参考文档:https://cwiki.apache.org/confluence/display/AMBARI/Restart+Host+Components
[root@name1 ~]# cat restarthiveserver.sh
#!/bin/bash
curl -u 'admin:password' -H 'X-Requested-By: ambari' -X POST -d '
{
"RequestInfo":{
"command":"RESTART",
"context":"Restart hiveserver2 on name2.example.com",
"operation_level":{
"level":"HOST",
"cluster_name":"hdpdev"
}
},
"Requests/resource_filters":[
{
"service_name":"HIVE",
"component_name":"HIVE_SERVER",
"hosts":"name2.example.com"
}
]
}' http://172.16.96.62:8080/api/v1/clusters/hdpdev/requests
[root@name1 ~]# sh restarthiveserver.sh
{
"href" : "http://172.16.96.62:8080/api/v1/clusters/hdpdev/requests/406",
"Requests" : {
"id" : 406,
"status" : "Accepted"
}
}
平时安装组件,一般很少会用api, 主要使用场景一般是,监控重启、定时重启等。