kylin之java调用Rest Api接口

1.官方文档

http://kylin.apache.org/docs15/howto/howto_build_cube_with_restapi.html

http://kylin.apache.org/docs15/howto/howto_use_restapi.html

常用的接口有:

1)获取segment列表

GET http://host:port/kylin/api/cubes?cubeName=youcubename&limit=15&offset=0

2)获取构建任务详情

GET http://host:port/kylin/api/jobs/{job_uuid}

此处的job_uuid可以从上边的接口segment列表中获取last_build_job_id即可

3)获取构建步骤的输出

GET http://host:port/kylin/api/jobs/{job_uuid}/steps/{step_id}/output

此处的step_id可以从构建任务详情的接口中找到steps数据然后得到其中每个的id即可5)获

4)构建kylin的接口

PUT  http://host:port/kylin/api/cubes/{cube_name}/rebuild

需要参数

String params="{\"startTime\": "+startTime+",\"endTime\": "+endTime+",\"buildType\":\"BUILD\"}";

5)合并segments

PUT  http://host:port/kylin/api/cubes/{cube_name}/rebuild

需要参数

String params="{\"startTime\": "+startTime+",\"endTime\": "+endTime+",\"buildType\":\"MERGE\"}";

 

 

 

2.java调用api接口的公用方法

public static String buildCube(String addr,String params,String method){
		String message="";
		try {
			URL url = new URL(addr);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			connection.setRequestMethod(method);
			connection.setDoOutput(true);
			String auth = ACCOUNT + ":" + PWD;
			String code = new String(new Base64().encode(auth.getBytes()));
			connection.setRequestProperty("Authorization", "Basic " + code);
			connection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
			if(params!=null && !"".equals(params)){
				PrintWriter out = new PrintWriter(connection.getOutputStream());
				out.write(params);
				out.close();
			}
			BufferedReader in;
			try {
				in = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			} catch (FileNotFoundException exception) {
				java.io.InputStream err = ((HttpURLConnection) connection)
					.getErrorStream();
				if (err == null)
					throw exception;
				in = new BufferedReader(new InputStreamReader(err));
			}
			StringBuffer response = new StringBuffer();
			String line;
			while ((line = in.readLine()) != null)
				response.append(line + "\n");
			in.close();
			message = response.toString();
		}catch (Exception e){
			message=e.getMessage();
		}
		return message;
	}

其中参数addr为地址,例如:

http://host:port/kylin/api/cubes?cubeName=cubename&limit=15&offset=0

param为参数,有的地址需要参数。例如构建接口

String params="{\"startTime\": "+startTime+",\"endTime\": "+endTime+",\"buildType\":\"BUILD\"}";

method为调用的方法。有三种:PUT、GET、POST

3.具体接口的地址使用参见kylin的官方api的接口地址。例如

public  String testKylin(){
		String result="";
		try {
			String params="";
			System.out.println("params=========="+params);
			String addr="http://172.16.37.107:7070/kylin/api/cubes?cubeName=cube_for_dicts&limit=15&offset=0";
			System.out.println("addr========"+addr);
			 result = UtilHttp.buildCube(addr,params,"GET");
		} catch (Exception e) {
			result=e.getMessage();
		}
		return result;
	}

 

你可能感兴趣的:(学习)