Apache Kylin将curl方式改为httpclient方式调用Restful API

1.httpclient方式调用Kylin Resetful API

  static String encoding;
    StringBuffer refreshBuffer = new StringBuffer();

    /**
     * 通过httpClient方式调用kylin Restful接口
     * 
     * @param kylinIp
     * @param para
     * @param method
     * @param params
     * @return
     */
    public StringBuffer excute(String kylinIp, String para, String method, String params) {
        StringBuffer out = new StringBuffer();
        try {
            URL url = new URL("http://" + kylinIp + "/kylin/api" + para);
            System.out.println(url);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(method);
            connection.setDoOutput(true);
            connection.setRequestProperty("Authorization", "Basic " + encoding);
            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            if (params != null) {
                byte[] outputInBytes = params.getBytes("UTF-8");
                OutputStream os = connection.getOutputStream();
                os.write(outputInBytes);
                os.close();
            }
            InputStream content = (InputStream) connection.getInputStream();
            // 解决乱码问题
            BufferedReader in = new BufferedReader(new InputStreamReader(content, Charset.forName("UTF-8")));
            String line;
            while ((line = in.readLine()) != null) {
                out.append(line);
            }
            in.close();
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return out;
    }

    /**
     * 登录kylin,输入身份认证,默认用户名为ADMIN,密码为KYLIN
     * 
     * @return
     */
    public StringBuffer login() {
        String method = "POST";
        String para = "/user/authentication";
        byte[] key = ("ADMIN:KYLIN").getBytes();
        encoding = Base64.encodeBase64String(key);
        return excute(PropertyUtil.prop.getProperty("kylinIp"), para, method, null);
    }

    /**
     * 获取cube下的所有job
     * 
     * @param cubeName
     * @param params
     * @return
     */
    public StringBuffer getJob(String cubeName, String params) {
        login();
        String method = "GET";
        String para = "/api/jobs/list/OADepartment/" + cubeName;
        return excute(PropertyUtil.prop.getProperty("kylinIp"), para, method, params);
    }

    /**
     * 获取cube下的所有segment
     * 
     * @param cubeName
     * @param params
     * @return
     */
    public StringBuffer getSeg(String cubeName, String params) {
        login();
        String method = "GET";
        String para = "/cubes/" + cubeName;
        return excute(PropertyUtil.prop.getProperty("kylinIp"), para, method, params);
    }

    /**
     * 获取所有的cube名称
     * 
     * @param params
     * @return
     */
    public StringBuffer getList(String params) {
        login();
        String method = "GET";
        String para = "/cubes";
        return excute(PropertyUtil.prop.getProperty("kylinIp"), para, method, params);
    }

    /**
     * Rebuild Cube
     * 
     * @param cubeName
     * @param params
     * @return
     */
    public StringBuffer buildCube(String cubeName, String params) {
        String method = "PUT";
        String para = "/cubes/" + cubeName + "/rebuild";
        return excute(PropertyUtil.prop.getProperty("kylinIp"), para, method, params);
    }

    /**
     * discard掉某个job
     * 
     * @param jobId
     * @param params
     */
    public void discard(String jobId, String params) {
        login();
        String method = "PUT";
        String para = "/jobs" + jobId + "/cancel";
        excute(PropertyUtil.prop.getProperty("kylinIp"), para, method, params);
    }

    /**
     * refresh Cube
     * 
     * @param startTime
     * @param endTime
     * @param refreshCube
     * @return
     */
    public StringBuffer refresh(Date startTime, Date endTime, String refreshCube) {
        login();
        String body = "{\"startTime\":\"" + startTime.getTime() + "\", \"endTime\":\"" + endTime.getTime()
                + "\", \"buildType\":\"REFRESH\"}";
        StringBuffer refreshResult = new StringBuffer();
        refreshResult.append(buildCube(refreshCube, body));
        return refreshResult;
    }

    /**
     * add Segment
     * 
     * @param startTime
     * @param endTime
     * @param addCube
     * @return
     */
    public StringBuffer add(Date startTime, Date endTime, String addCube) {
        login();
        String body = "{\"startTime\":\"" + startTime.getTime() + "\", \"endTime\":\"" + endTime.getTime()
                + "\", \"buildType\":\"BUILD\"}";
        StringBuffer addResult = new StringBuffer();
        addResult.append(buildCube(addCube, body));
        return addResult;
    }
  /**
     * 通过kylin进行查询
     * 
     * @param params
     * @return
     */
    public StringBuffer query(String params) {
        String method = "POST";
        String para = "/query";
        return excute(PropertyUtil.prop.getProperty("kylinIp"), para, method, params);
    }

    /**
     * kylin查询主体信息
     * 
     * @param sql
     * @param lookupId
     * @param projectName
     * @return
     */
    public StringBuffer getTableTime(String sql, String lookupId, String projectName) {
        login();
        String body = "{\"sql\": \"" + sql + lookupId + "\",\"offset\":0,\"limit\":" + KYLIN_LIMIT
                + ",\"acceptPartial\":false, \"project\":\"" + projectName + "\"}";
        StringBuffer queryResult = query(body);
        return queryResult;
    }

2.具体引用

①实例化上述Kylin Api类

KylinApi api = new KylinApi();

②获取cube下的所有job

public StringBuffer getJobId(String projectName, String cubeName, String params) {
        logger.info("获取某个cube下的job Id:" + "projectName: " + projectName + "  " + "cubeName:  " + cubeName);
        StringBuffer job = new StringBuffer();
        job = api.getJob(projectName, cubeName, params);
        return job;
    }

③获取cube下的所有segment

public StringBuffer getSegment(String cubeName,String params) {
        logger.info("获取某个cube的segment:   "+"cubename:  "+cubeName);
        StringBuffer segment = new StringBuffer();
        segment=api.getSeg(cubeName,params);
        return segment;
}

④获取当前Kylin下所有cube名称

public StringBuffer getCubeLists(String params) {
        StringBuffer cube = new StringBuffer();
        cube = api.getList(params);
        return cube;
    }

⑤Refresh cube

public StringBuffer refreshCube(String refreshStartTime, String refreshEndTime, String refreshCube) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        logger.info("Refresh操作: " + "开始时间: " + refreshStartTime + "   " + "结束时间: " + refreshEndTime + "   "
                + "cubename: " + refreshCube);
        try {
            Date startTime = format.parse(refreshStartTime + " " + "08:00:00");
            Date endTime = format.parse(refreshEndTime + " " + "08:00:00");
            refreshBuffer = api.refresh(startTime, endTime, refreshCube);
        } catch (ParseException e) {
            logger.info(e.getMessage());
        }
        return refreshBuffer;
    }

⑥Add Segment

public StringBuffer addSegment(String addStartTime, String addEndTime, String addCube) {        
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
    logger.info("新增操作: " + "开始时间: " + addStartTime + " " + "结束时间: " + addEndTime + " " + "cubename: " + addCube);
    try {Date startTime = format.parse(addStartTime + " " + "08:00:00");            
    Date endTime = format.parse(addEndTime + " " + "08:00:00"); 
    addBuffer = api.add(startTime, endTime, addCube);       
  } catch (ParseException e) {  
          logger.info(e.getMessage());  
    }       
    return addBuffer;   
}

⑦discard某个job

public void discardJob(String jobId, String params) {
        api.discard(jobId, params);
    }

⑧通过kylin查询

 public StringBuffer testCubeQuery(String sql, String lookupId, String projectName) {
            StringBuffer timeBuf = new StringBuffer();
             timeBuf=api.getTableTime(sql,lookupId,projectName);
            return timeBuf;
        }

注:若kylin给出的Restful API对应没有params,均取null值。

3.参考文献

Kylin Java RESTful API
Use RESTful API

你可能感兴趣的:(Apache Kylin将curl方式改为httpclient方式调用Restful API)