访问HetuEngine HSConsole的Rest API的样例代码

举报
null1 发表于 2021/11/12 10:20:38 2021/11/12
【摘要】 FusionInsight HetuEngine访问HSConsole的样例代码

        HetuEngine的HSconsole对外提供了部分常见的计算实例操作接口,包括计算集群的创建,删除,正运行的任务的查询,查询,数据源的新增,删除等接口。本文提供了一个样例代码指导如何进行HSConsole接口的访问,重点是怎么进行FusionInsight Manager的CAS认证,只有获取到完成CAS认证通过的HttpClient才能正常访问HSConsole的rest api接口。

1. pom.xml的文件配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.huawei.hetu</groupId>
    <artifactId>hsconsoleClient</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

    </dependencies>

</project>


  1. HTTP的GET/POST/PUT等操作的封装Utils类

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

package com.huawei.hsconsole;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.protocol.HttpContext;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Optional;


/**
 * The HttpUtils
 *
 * @since 2020-01-07
 */
public class HttpUtils {
    /**
     * The http request connect timeout.
     */
    public static final Integer DEFAULT_CONNECT_TIMEOUT = 60000;
    /**
     * The socket connect timeout.
     */
    public static final Integer DEFAULT_SOCKET_TIMEOUT = 60000;

    /**
     * Http Get.
     *
     * @param httpClient The http client.
     * @param url Target server url.
     * @param paramList paramL list
     * @param requestConfig requestConfig
     * @param context http context
     * @return HttpResponse.
     * @throws Exception If failed to excute http get action.
     */
    public static HttpResponse httpGet(
            HttpClient httpClient,
            String url,
            List<NameValuePair> paramList,
            RequestConfig requestConfig,
            HttpContext context) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        if (paramList != null && !paramList.isEmpty()) {
            uriBuilder.setParameters(paramList);
        }

        HttpGet httpGet = new HttpGet(uriBuilder.build());
        setHttpConfig(httpGet, requestConfig);

        if (context == null) {
            return httpClient.execute(httpGet);
        } else {
            return httpClient.execute(httpGet, context);
        }
    }


    /**
     * Http Post.
     *
     * @param httpClient The http client.
     * @param url Target server url.
     * @param entity HttpEntity.
     * @param requestConfig requestConfig
     * @param context http context
     * @param headers http request heads
     * @return HttpResponse.
     * @throws Exception If failed to excute http post action.
     */
    public static HttpResponse httpPost(
            HttpClient httpClient,
            String url,
            HttpEntity entity,
            RequestConfig requestConfig,
            HttpContext context,
            Header[] headers) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        setHttpConfig(httpPost, requestConfig);

        if (entity != null) {
            httpPost.setEntity(entity);
        }

        if (headers != null) {
            for (Header header : headers) {
                httpPost.addHeader(header);
            }
        }

        if (context == null) {
            return httpClient.execute(httpPost);
        } else {
            return httpClient.execute(httpPost, context);
        }
    }

    /**
     * Http Put.
     *
     * @param httpClient The http client.
     * @param url Target server url.
     * @param entity HttpEntity.
     * @param requestConfig Http requestConfig
     * @param context http context
     * @param headers http head
     * @return HttpResponse.
     * @throws Exception If failed to excute http post action.
     */
    public static HttpResponse httpPut(
            HttpClient httpClient,
            String url,
            HttpEntity entity,
            RequestConfig requestConfig,
            HttpContext context,
            Header[] headers) throws Exception {
        HttpPut httpPut = new HttpPut(url);
        setHttpConfig(httpPut, requestConfig);

        if (entity != null) {
            httpPut.setEntity(entity);
        }

        if (headers != null) {
            for (Header header : headers) {
                httpPut.addHeader(header);
            }
        }

        if (context == null) {
            return httpClient.execute(httpPut);
        } else {
            return httpClient.execute(httpPut, context);
        }
    }

    /**
     * Http Delete.
     *
     * @param httpClient The http client.
     * @param url Target server url.
     * @param requestConfig requestConfig
     * @param context context
     * @param headers http head
     * @return HttpResponse.
     * @throws Exception If failed to excute http delete action.
     */
    public static HttpResponse httpDelete(
            HttpClient httpClient,
            String url,
            RequestConfig requestConfig,
            HttpContext context,
            Header[] headers) throws Exception {
        HttpDelete httpDelete = new HttpDelete(url);
        setHttpConfig(httpDelete, requestConfig);

        if (headers != null) {
            for (Header header : headers) {
                httpDelete.addHeader(header);
            }
        }

        if (context == null) {
            return httpClient.execute(httpDelete);
        } else {
            return httpClient.execute(httpDelete, context);
        }
    }

    private static void setHttpConfig(HttpRequestBase httpRequestBase, RequestConfig requestConfig) {
        RequestConfig noNullRequestConfig = Optional.ofNullable(requestConfig)
                .orElse(RequestConfig.custom()
                        .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
                        .setConnectTimeout(DEFAULT_CONNECT_TIMEOUT)
                        .build());
        httpRequestBase.setConfig(noNullRequestConfig);
    }
}


  1. 进行Cas认证并且返回HttpClient的代码,getHttpClient传入cas地址,HSConsole地址,用户名,密码进行cas认证,并且返回已经认证通过的CAS Client。

/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

package com.huawei.hsconsole;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * The HSConsoleHttpClient
 *
 * @since 2020-05-01
 */
public class HSConsoleHttpClient {
    private static final int STRING_UNFOUND = -1;


    private String getPageIt(HttpEntity entity) throws IOException {
        BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
        String tempLine = rd.readLine();
        String itFlag = "<input type=\"hidden\" name=\"lt\" value=\"";
        String lt = "";
        while (tempLine != null) {
            int index = tempLine.indexOf(itFlag);
            if (index != STRING_UNFOUND) {
                String s1 = tempLine.substring(index + itFlag.length());
                int index1 = s1.indexOf("\"");
                if (index1 != STRING_UNFOUND) {
                    lt = s1.substring(0, index1);
                }
            }
            tempLine = rd.readLine();
        }
        return lt;
    }

    /**
     * close the http client.
     *
     * @param httpClient The http client need to be closed.
     */
    public void closeClient(CloseableHttpClient httpClient) {
        if (httpClient != null) {
            try {
                httpClient.close();
            } catch (IOException e) {
                System.out.println("httpClient clone fail.");
            }
        }
    }

    /**
     * close the http response.
     *
     * @param response The http client need to be closed.
     */
    public void closeResponse(CloseableHttpResponse response) {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                System.out.println("response clone fail.");
            }
        }
    }

    /**
     * login fi cas.
     *
     * @param httpClient The http client.
     * @param lt login ticket
     * @param casUrl cas url
     * @param hsconsoleUrl hsconsole url
     * @param context http context
     */
    private boolean loginCas(CloseableHttpClient httpClient, String lt, String casUrl, String hsconsoleUrl, HttpClientContext context, String user, String passworld) throws Exception {
        List<NameValuePair> nvps = new ArrayList<>();
        nvps.add(new BasicNameValuePair("username", user));
        nvps.add(new BasicNameValuePair("password", passworld));
        nvps.add(new BasicNameValuePair("lt", lt));
        nvps.add(new BasicNameValuePair("_eventId", "submit"));
        nvps.add(new BasicNameValuePair("submit", "Login"));
        HttpEntity entity1 = new UrlEncodedFormEntity(nvps, "UTF-8");
        CloseableHttpResponse casResponse = (CloseableHttpResponse) HttpUtils.httpPost(httpClient, casUrl, entity1, null, context, null);
        closeResponse(casResponse);
        CloseableHttpResponse hsConsoleResponse = (CloseableHttpResponse) HttpUtils.httpGet(httpClient, hsconsoleUrl, null, null, context);
        String hsConsoleHtml = EntityUtils.toString(hsConsoleResponse.getEntity());
        if (hsConsoleHtml.contains("script src=\"runtime") && hsConsoleHtml.contains("script src=\"polyfills")) {
            System.out.println("log success.");
            closeResponse(hsConsoleResponse);
            return true;
        } else {
            System.out.println("log fail.");
            closeResponse(hsConsoleResponse);
            return false;
        }
    }

    /**
     * get http client.
     *
     * @param casUrl cas url.
     * @param hsconsoleUrl hsconsole url
     * @param user user
     * @param passworld passworld
     * @return http client
     * @throws Exception IOException or CasAuthException
     */
    public CloseableHttpClient getHttpClient(String casUrl, String hsconsoleUrl, String user, String passworld) 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()) {
            System.out.println(String.format("cannot get page it from %s, respone code is %s, response entity is %s.", 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, passworld)) {
            return httpClient;
        } else {
            closeClient(httpClient);
            throw new Exception("cas authentication failed.");
        }
    }
}


  1. Main函数操作样例,以获取当前正在运行的查询任务和停止某一个任务为例:

package com.huawei.hsconsole;
​
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
​
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
​
public class HsconsoleClient {
​
    public static void main(String[] args) throws Exception {
​
        System.setProperty("javax.net.ssl.trustStore", "E:\\OneQuery\\OneQueryResource\\RestApiDemo\\hsconsoleClient\\src\\main\\resources\\hsconsole.jks");
​
        String casUrl = "https://192.168.67.137:20009/cas/login?service=https%3A%2F%2F192.168.67.137%3A20026%2FHetuEngine%2FHSConsole%2F15%2F";
        String hsconsoleUrl = "https://192.168.67.137:20026/HetuEngine/HSConsole/15/#/hsconsole/cluster";
        String user = "yourName";
        String passworld = "yourPassword";
        CloseableHttpClient httpClient = null;
        HSConsoleHttpClient hsConsoleHttpClient = new HSConsoleHttpClient();
        httpClient = hsConsoleHttpClient.getHttpClient(casUrl, hsconsoleUrl, user, passworld);
        getQuery(httpClient);
        killQuery(httpClient);
    }
​
    public static void getQuery(CloseableHttpClient httpClient) throws IOException {
        // url以集群版本的API接口文档为准,不同的版本可能会有不同
        String operationUrl = "https://192.168.67.137:20026/HetuEngine/HSConsole/15/v1/hsconsole/query/running/default";
        HttpGet httpGet = new HttpGet(operationUrl);
        httpGet.addHeader("Content-Type", "application/json;charset=UTF-8");
        HttpResponse httpResponse = httpClient.execute(httpGet);
        System.out.println(httpResponse.getStatusLine().getStatusCode());
        InputStream in = httpResponse.getEntity().getContent();
        String str = IOUtils.toString(in, StandardCharsets.UTF_8);
        System.out.println(str);
    }
​
    public static void killQuery(CloseableHttpClient httpClient) throws IOException {
        String operationUrl = "https://192.168.67.137:20026/HetuEngine/HSConsole/15/v1/hsconsole/query/kill/20200927_024126_00001_t3xdi@default@HetuEngine";
        HttpDelete httpDelete = new HttpDelete(operationUrl);
        httpDelete.addHeader("Content-Type", "application/json;charset=UTF-8");
        HttpResponse httpResponse = httpClient.execute(httpDelete);
        System.out.println(httpResponse.getStatusLine().getStatusCode());
    }
}


  1. 测试验证


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。