Rest接口操作Kylin

    xiaoxiao2021-03-26  32

    由于官方网站的介绍中,涉及的Rest接口较少,现通过官方提供在github上的源码包,使用一些其他实用的Rest接口。 找到Kylin Rest源码: https://github.com/apache/kylin/tree/master/server-base 到 org.apache.kylin.rest.controller目录下查找对应的controller类 例如: org.apache.kylin.rest.controller .CubeController @Controller @RequestMapping(value = "/cubes") public class CubeController extends BasicController {      ...     @RequestMapping(value = "/{cubeName}/purge", method = { RequestMethod.PUT })     @ResponseBody     public CubeInstance purgeCube(@PathVariable String cubeName) {         try {             CubeInstance cube = cubeService.getCubeManager().getCube(cubeName);             if (cube == null) {                 throw new InternalErrorException("Cannot find cube " + cubeName);             }             return cubeService.purgeCube(cube);         } catch (Exception e) {             String message = "Failed to purge cube: " + cubeName;             logger.error(message, e);      throw new InternalErrorException(message + " Caused by: " + e.getMessage(), e);      }      } ... } purge(清空表)的操作Rest为: localhost:7070/kylin/api /cubes /TestCube/purge 此处的      /cubes 为本类的 @ RequestMapping(value = "/cubes" )      / TestCube /purge   即为 @ RequestMapping(value = "/{cubeName}/purge" )      所传参数 @ PathVariable String cubeName,对应的TestCube。 再找一个,这次以Cube的controller为例: org.apache.kylin.rest.controller. CubeController @Controller @RequestMapping(value = "/cubes") public class CubeController extends BasicController {      ...      @RequestMapping(value = "/{cubeName}/build", method = { RequestMethod.PUT })      @ResponseBody      public JobInstance build(@PathVariable String cubeName, @RequestBody JobBuildRequest req) {           return rebuild(cubeName, req);      }      ... } 可以简单看出这个build cube的Rest操作为:localhost:7070/kylin/api /cubes /TestCube/build 但是所传参数中有个 @ RequestBody 进入 JobBuildRequest类看看此类长什么样 package org.apache.kylin.rest.request; public class JobBuildRequest {     private long startTime;     private long endTime;     private String buildType;     private boolean force;     ... } 可以看出我们需要在参数带上这几个 具体在网页或者Rest Client工具的操作就不做演示了,设置好对应的请求类型以及参数即可 下面贴上自己的演示代码(build)                   String obj = "{\"startTime\":\"" + starttime +"\","                   + "\"endTime\":\""+ endtime + "\","                   + "\"buildType\":\"BUILD\"}";             String method = "put";                   String url = "http://"+host+":"+port+"/kylin/api/cubes/"+cubeName+"/build";                   URL target = new URL(url);                   conn = (HttpURLConnection) target.openConnection();                   conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");                   conn.addRequestProperty("Content-Type", "application/json;charset=UTF-8");                   String encoding = new sun.misc.BASE64Encoder().encode((userName+":"+password).getBytes());                   conn.setRequestProperty("Authorization", "Basic " + encoding);                   conn.setDoOutput(true);                   conn.setDoInput(true);                   conn.setRequestMethod(method);                   conn.setRequestProperty("Content-Length", paramObj.toString().getBytes("UTF-8").length + "");  //设置文件请求的长度                   out = conn.getOutputStream();                   out.write(paramObj.toString().getBytes());                   out.flush();                   BufferedReader brd = new BufferedReader(new InputStreamReader(conn.getInputStream()));                   StringBuilder sbout = new StringBuilder();                   String line;                   while((line = brd.readLine()) != null){                         sbout.append(line);                   }                   return sbout.toString();                   conn.setConnectTimeout(timeout);                   out.close();                   conn.disconnect(); 建立Cube: Rest地址:localhost:7070/kylin/api /cubes 请求类型:POST 参数: project      cubeName     cubeDescData cubeDescData : 在页面版上,通过编辑Cube的json获取 建立Model Rest地址:localhost:7070/kylin/api /models 请求类型:POST 参数: project      modelName     modelDescData modelDescData : 在页面版上,建立完成Model后,log会打印出相应json代码 localhost:7070/kylin/api/models?modelName=TestModel    GET 注意:需要把json中的  "  改成  /" 贴上示例代码: public class KylinRestTest {     public static String call(final String URL, final String METHOD) {         String result = null;         HttpURLConnection conn = null;         try {             URL target = new URL(URL);             conn = (HttpURLConnection) target.openConnection();             conn.setDoOutput(true);             conn.setDoInput(true);             conn.setRequestMethod(METHOD);             conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");             String username = "ADMIN";             String password = "KYLIN";             String input = username + ":" + password;             String encoding = new sun.misc.BASE64Encoder().encode(input.getBytes());             conn.setRequestProperty("Authorization", "Basic " + encoding);             //             conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //            conn.connect(); //            String obj = "{\"startTime\":\"0\",\"endTime\":\"1472832000000\",\"buildType\":\"BUILD\"}"; //            String obj = "{\"startTime\":\"1472832000000\",\"endTime\":\"1473177600000\",\"buildType\":\"BUILD\"}"; //            String obj = "{\"startTime\":\"1472832000000\",\"endTime\":\"1473177600000\",\"buildType\":\"BUILD\"}";             String obj = "{\"startTime\":\"1475020800000\",\"endTime\":\"1475193600000\",\"buildType\":\"BUILD\"}"; //            String obj = "{\"startTime\":\"1473177600000\",\"endTime\":\"1473465600000\",\"buildType\":\"REFRESH\"}"; //            String obj = "{\"startTime\":\"1472832000000\",\"endTime\":\"1473177600000\",\"buildType\":\"MERGE\"}";             conn.setRequestProperty("Content-Length", obj.toString().getBytes().length + "");  //设置文件请求的长度             OutputStream out = conn.getOutputStream();             out.write(obj.toString().getBytes());             out.flush();             out.close(); //            conn.setRequestProperty("startTime", "0"); //            conn.setRequestProperty("endTime", "1472832000000"); //            conn.setRequestProperty("buildType", "BUILD");             if (200 != conn.getResponseCode()) { //                throw new RuntimeException("failed, error code is " + conn.getResponseCode() + "\n" + conn.getErrorStream().toString() + "\n" + conn.getResponseMessage()); //                throw new RuntimeException(conn.getErrorStream().toString()); //                throw new RuntimeException(conn.getResponseMessage()); //                throw new RuntimeException("failed, error code is " + conn.getResponseCode());                 System.out.println(conn.getErrorStream());             }             byte[] temp = new byte[conn.getInputStream().available()];             if (conn.getInputStream().read(temp) != -1) {                 result = new String(temp);             }         } catch (MalformedURLException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         } finally {             conn.disconnect();         }         return result;     }     public static void main(String[] args) { //        System.out.println(call("http://192.168.88.215:7070/kylin/api/user/authentication", "POST")); //        System.out.println(call("http://192.168.88.215:7070/kylin/api/cubes?cubeName=test2MC&limit=15&offset=0", "GET")); //        System.out.println(call("http://192.168.88.215:7070/kylin/api/jobs/f7ffbe3d-7907-45f2-b815-dcca831f08b5", "GET")); //        System.out.println(call("http://192.168.88.215:7070/kylin/api/cubes/test2MC/build", "PUT"));         System.out.println(call("http://192.168.88.215:7070/kylin/api/cubes/test2MC/build", "PUT"));     } }
    转载请注明原文地址: https://ju.6miu.com/read-663055.html

    最新回复(0)