通过http调用操作hue管理的oozie


前提关闭csrf验证
编辑/opt/cloudera/parcels/CDH-5.8.3-1.cdh5.8.3.p0.2/lib/hue/desktop/core/src/desktop/settings.py
注释掉MIDDLEWARE_CLASSES中的django.middleware.csrf.CsrfViewMiddleware

=========

package com.xxx.yyy.service.oozieClient;

import com.alibaba.fastjson.JSON;
import org.apache.http.Header;
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.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author allen
 * 需要关闭hue 服务端的csrf验证
 * @version V1.0
 * @Title: HueHttpClient.java
 * @Package com.xxx.yyy.service.oozieClient
 * @Description
 * @date 2018 07-16 20:19.
 */
public class HueHttpClient {

    static Logger logger = LoggerFactory.getLogger(HueHttpClient.class);

    /**
     * hue cookie中sessionId的key
     */
    public static final String COOKIE_SESSION_ID_KEY = "sessionid";

    private static final String loginUrl = "http://hue.data.xxx.cn/accounts/login/";
    private static final String loginUser = "hdfs";
    private static final String loginPsw = "xxx";



    /**
     * 状态查询
     * @param url
     * @param sessionId
     * @return
     */
    public static String getWfStatus(String url, String sessionId ,int maxRetryTimes) {
        //执行一次
        maxRetryTimes ++;

        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet get = new HttpGet(url);
        if (sessionId != null && !"".equals(sessionId)) {
            get.addHeader(new BasicHeader("Cookie", COOKIE_SESSION_ID_KEY+"=" + sessionId));
        }

        HttpClientContext context = HttpClientContext.create();

        String status = null;
        try {
            CloseableHttpResponse response = httpClient.execute(get, context);

            String responseText = EntityUtils.toString(response.getEntity(), "UTF-8");
            if(responseText != null && responseText.contains("login required") && 0 < maxRetryTimes ){
                //如果登录然后在检查状态
                logger.warn("login required,trying to login and recheck. maxRetryTimes="+maxRetryTimes);
                status = getWfStatus(url,loginAndGetSessionId(loginUrl,loginUser,loginPsw),maxRetryTimes);
            }else {
                logger.warn("check failed,rechecking... maxRetryTimes="+maxRetryTimes);
                status = JSON.parseObject(responseText).getString("status");
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
            if(0 < maxRetryTimes){
                status = getWfStatus(url,sessionId,maxRetryTimes);
            }
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                logger.error(e.getMessage());
                e.printStackTrace();
            }
        }

        return status;
    }


    /**
     * 登录获取sessionId
     * @param url
     * @param user
     * @param psw
     * @return
     */
    private static String loginAndGetSessionId(String url,String user,String psw) {

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url);

        // 创建参数队列
        List formParams = new ArrayList();
        formParams.add(new BasicNameValuePair("username", user));
        formParams.add(new BasicNameValuePair("password", psw));

        String sessionId = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(httpPost);

            try {
                //获得Cookies
                CookieStore cookieStore = httpClient.getCookieStore();

                List cookies = cookieStore.getCookies();
                for (Cookie cookie : cookies) {
                    //遍历Cookies 找到sessionid
                    if(COOKIE_SESSION_ID_KEY.equals(cookie.getName())){
                        sessionId = cookie.getValue();
                        logger.info("sessionId="+sessionId);
                        break;
                    }
                }

                //System.out.println(sessionId);
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sessionId;
    }


    /**
     * 从hue提交Oozie workflow
     * @param url
     * @param sessionId
     * @param map
     * @return
     */
    public static String submitWf(String url, String sessionId, Map map) {

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url);

        // 创建参数队列
        List formParams = new ArrayList();
        for(Iterator it = map.keySet().iterator(); it.hasNext();){
            String key = it.next();
            formParams.add(new BasicNameValuePair(key,(String)map.get(key)));
        }

        if (sessionId != null && !"".equals(sessionId)) {
            httpPost.addHeader(new BasicHeader("Cookie", COOKIE_SESSION_ID_KEY+"=" + sessionId));
        }

        String wfId = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(httpPost);

            int rCode = response.getStatusLine().getStatusCode();
            logger.info("http status code : "+rCode);
            if(302 == rCode){
                for(Header header : response.getAllHeaders()){
                    if("Location".equals(header.getName())){
                        wfId = header.getValue();
                        break;
                    }
                }
            }else {
                logger.info("http status code : "+rCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return wfId;
    }


    /**
     * 从hue结束oozie的workflow
     * @param url
     * @param sessionId
     * @param jobId
     * @return
     */
    public static String killWf(String url,String sessionId,String jobId){

        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(url);

        // 创建参数队列
        List formParams = new ArrayList();
        formParams.add(new BasicNameValuePair("action","kill"));
        formParams.add(new BasicNameValuePair("job_ids",jobId));

        if (sessionId != null && !"".equals(sessionId)) {
            httpPost.addHeader(new BasicHeader("Cookie", COOKIE_SESSION_ID_KEY+"=" + sessionId));
        }


        String result = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(httpPost);

            int rcode = response.getStatusLine().getStatusCode();
            logger.info("http status code : "+rcode);
            if(200 == rcode){
                String responseText = EntityUtils.toString(response.getEntity(), "UTF-8");
                //totalErrors为0,则返回成功
                if(0 == JSON.parseObject(responseText).getInteger("totalErrors")){
                    result = "SUCCESS";
                }else{
                    logger.info("please make sure the jobId exists.");
                }
            }else {
                logger.info("http status code : "+rcode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        return result;
    }


    public static void main(String[] args) {


        //sessionId不对或者过期会返回302
        String sessionId = "1njzi6r0jn0wkasckbriuw9zkp6bkqpc";

        //sessionId = loginAndGetSessionId(loginUrl,loginUser,loginPsw);
        //System.out.println("NEW_SESSIONID="+sessionId);

        String url = "http://hue.data.xxx.cn/oozie/list_oozie_workflow/0012516-180608171858894-oozie-oozi-W/?format=json";
        //String url = "http://hue.data.xxx.cn/oozie/list_oozie_workflow/0012538-180608171858894-oozie-oozi-W/?format=json";
        //getWfStatus(url, sessionId);
        //String status = getWfStatus(url, "sslncxh0c3rhoue0llad2erup1upvz69");
        //String status = getWfStatus(url, sessionId,3);
        //System.out.println("STATUS="+status);


        //Map map = new HashMap();
        //map.put("form-TOTAL_FORMS","1");
        //map.put("form-INITIAL_FORMS","1");
        //map.put("form-MAX_NUM_FORMS","1000");
        //map.put("form-0-name","oozie.use.system.libpath");
        //map.put("form-0-value","True");
        //String wfId = submitWf("http://hue.data.xxx.cn/oozie/editor/workflow/submit/7021",
        //        sessionId
        //        ,map);
        //
        //logger.info(wfId);

        String jobIds = "0012544-180608171859130-oozie-oozi-W";

        logger.info(killWf("http://hue.data.xxx.cn/oozie/bulk_manage_oozie_jobs/",
                sessionId,
                jobIds));
    }
}
 

你可能感兴趣的:(hadoop)