HetuEngine运维管理实践-通过restAPI接口实现计算实例和数据源管理(一)
HetuEngine依托Hadoop集群中的Yarn-Service进行资源管理,实现计算实例的租户级资源隔离。用户在某些业务场景下需要经常对计算实例或者数据源进行增删改查和批量更新,此类频繁操作需要以非界面方式实现,HetuEngine开放了相关REST API,便于对计算实例和数据源管理。具体接口内容请参考《华为云Stack 8.0.3 MapReduce服务(3.1.1-LTS)HetuEngine Rest API接口文档 01》。
开发思路
- 实现对FusionInsight Manager的cas认证;
- 创建 HSConsole的httpclient;
- 调用hsconsole接口实现功能。
数据准备
- HetuEngine服务HSConsole页面的链接地址,可以打开manager页面进入HetuEngine服务获取,也可以通过调用Manager提供的接口获取;
- 从HetuEngine的Rest API接口文档中获取用户想要实现的操作对应的Url;
- hsconsole.jks文件,从hsconsole节点的安装目录/opt/huawei/Bigdata/FusionInsight_Hetu_8.1.1/1_xx_HSConsole/etc 获取;
- 登录FusionInsight Manager的用户名密码,需要具有hetuadmin用户组权限;
样例代码
登录认证
功能介绍
实现Basic认证登录,并返回登录后的httpClient。
前提条件
将获取到的hsconsole.jks文件放在本地目录下,例如
D:\\temp\\restapidemo\\hsconsole.jks
System.setProperty("javax.net.ssl.trustStore", "D:\\temp\\restapidemo\\hsconsole.jks");
String casUrl = "https://manager_float_ip:20009/cas/login?service=https%3A%2F%2Fmanager_float_ip%3A20026%2FHetuEngine%2FHSConsole%2F15%2F";
String hsconsoleUrl = "https://manager_float_ip:20026/HetuEngine/HSConsole/15/#/hsconsole/cluster";
String user = "youruser";
String password = "yourpassword";
CloseableHttpClient httpClient = null;
HSConsoleHttpClient hsConsoleHttpClient = new HSConsoleHttpClient();
httpClient = hsConsoleHttpClient.getHttpClient(casUrl, hsconsoleUrl, user, password);`
其中,getHttpClient方法实现如下,在HSConsoleHttpClient类中:
public CloseableHttpClient getHttpClient(String casUrl, String hsconsoleUrl,
String user, String password) throws Exception {
CookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
CloseableHttpResponse response = (CloseableHttpResponse)HttpUtils.httpGet(httpClient,
hsconsoleUrl, null, null, context);
String pageIt = getPageIt(response.getEntity());
if (pageIt.isEmpty()) {
LOG.error("cannot get page it from {}, respone code is {}, response entity is {}.", hsconsoleUrl,
response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity()));
closeResponse(response);
closeClient(httpClient);
throw new Exception(hsconsoleUrl + " cas authentication failed.");
}
if (loginCas(httpClient, pageIt, casUrl, hsconsoleUrl, context, user, password)) {
return httpClient;
} else {
closeClient(httpClient);
throw new Exception("cas authentication failed.");
}
}`
查询计算实例状态
功能介绍
通过访问HetuEngine接口查询计算实例状态,只能查询出当前用户有权限看到的计算实例,返回值包含计算实例ID,集群名称等信息,具体信息可以在REST API接口文档中查询。
代码样例
以下代码片段是查询计算实例状态的示例,在main方法中。
public String getClusterInfo(CloseableHttpClient httpClient, String hsconsoleUrl) {
String url = hsconsoleUrl.substring(0, hsconsoleUrl.indexOf("#")) +
"v1/hsconsole/clusters?status=&sort=&size=10&page=0&filterType=&filterContent=&condition=";
HttpManager httpManager = new HttpManager(httpClient);
String response = httpManager.sendHttpGetRequest(url, "GET CLUSTER STATUE");
return response;
}
创建计算实例配置
功能介绍
通过访问HetuEngine接口创建计算实例配置。
代码样例
以下代码片段是创建计算实例配置的示例,在main方法中,其中json是创建配置接口的入参,具体可以在REST API接口文档中获取。
public String createClusterConfig(CloseableHttpClient httpClient, String hsconsoleUrl, String json) {
String url = hsconsoleUrl.substring(0, hsconsoleUrl.indexOf("#")) + "/v1/hsconsole/clusters/config";
HttpManager httpManager = new HttpManager(httpClient);
String response = httpManager.sendHttpPostRequestWithString(url, json, "CREATE CLUSTER");
return response;
}
启动计算实例
功能介绍
通过访问HetuEngine接口启动计算实例。
代码样例
以下代码片段是启动计算实例的示例,在main方法中。
public String startCluster(CloseableHttpClient httpClient, String hsconsoleUrl, String clusterId) {
String url = hsconsoleUrl.substring(0, hsconsoleUrl.indexOf("#")) +
String.format("/v1/hsconsole/clusters/%s/start", clusterId);
HttpManager httpManager = new HttpManager(httpClient);
String response = httpManager.sendHttpPostRequestWithString(url, null, "START CLUSTER");
return response;
}
停止计算实例
功能介绍
通过访问HetuEngine接口停止计算实例。
代码样例
以下代码片段是停止计算实例的示例,在main方法中。
public String stopCluster(CloseableHttpClient httpClient, String hsconsoleUrl, String clusterId) {
String url = hsconsoleUrl.substring(0, hsconsoleUrl.indexOf("#")) +
String.format("/v1/hsconsole/clusters/%s/stop", clusterId);
HttpManager httpManager = new HttpManager(httpClient);
String response = httpManager.sendHttpPostRequestWithString(url, null, "STOP CLUSTER");
return response;
}
删除计算实例
功能介绍
通过访问HetuEngine接口删除计算实例。
代码样例
以下代码片段是删除计算实例的示例,在main方法中,入参clusterId可以使用查询接口获取,对应HSConsole页面上计算实例ID。
public void deleteCluster(CloseableHttpClient httpClient, String hsconsoleUrl, String clusterId) {
String url = hsconsoleUrl.substring(0, hsconsoleUrl.indexOf("#")) +
String.format("/v1/hsconsole/clusters/%s/delete", clusterId);
HttpManager httpManager = new HttpManager(httpClient);
httpManager.sendHttpDeleteRequest(url, null, "DELETE Computer Cluster");
}
- 点赞
- 收藏
- 关注作者
评论(0)