kylin使用Rest Ful API创建cube和model
Kylin 1.5.4的Rest Ful API用户参考手册
<http://kylin.apache.org/docs15/howto/howto_use_restapi.html>中没有提供cube和model的创建方法,在实际应用中有这样的需求,经过参考源码和实践,以下对这部分做一个补充。
1.通过curl创建cube
curl -X POST -H "Authorization: Basic QURNSU46S1lMSU4=" -H "Content-Type: application/json" -d '{ "project":"test_project","cubeName":"test_cube5","cubeDescData":"{\"name\":\"test_cube5\",\"model_name\":\"test_models_3\",\"description\":\"\",\"null_string\":null,\"dimensions\":[{\"name\":\"N1\",\"table\":\"DEFAULT.KYLIN_TEST_T1\",\"column\":\"C1\",\"derived\":null},{\"name\":\"N2\",\"table\":\"DEFAULT.KYLIN_TEST_T2\",\"column\":null,\"derived\":[\"C3\"]}],\"measures\":[{\"name\":\"_COUNT_\",\"function\":{\"expression\":\"COUNT\",\"parameter\":{\"type\":\"constant\",\"value\":\"1\",\"next_parameter\":null},\"returntype\":\"bigint\"},\"dependent_measure_ref\":null},{\"name\":\"SUM1\",\"function\":{\"expression\":\"SUM\",\"parameter\":{\"type\":\"column\",\"value\":\"C1\",\"next_parameter\":null},\"returntype\":\"bigint\"},\"dependent_measure_ref\":null}],\"dictionaries\":[],\"rowkey\":{\"rowkey_columns\":[{\"column\":\"C1\",\"encoding\":\"dict\",\"isShardBy\":false}]},\"hbase_mapping\":{\"column_family\":[{\"name\":\"F1\",\"columns\":[{\"qualifier\":\"M\",\"measure_refs\":[\"_COUNT_\",\"SUM1\"]}]}]},\"aggregation_groups\":[{\"includes\":[\"C1\"],\"select_rule\":{\"hierarchy_dims\":[],\"mandatory_dims\":[],\"joint_dims\":[]}}],\"signature\":\"4p7DRvp2saib9zvoRzmCfA==\",\"notify_list\":[],\"status_need_notify\":[\"ERROR\",\"DISCARDED\",\"SUCCEED\"],\"partition_date_start\":0,\"partition_date_end\":3153600000000,\"auto_merge_time_ranges\":[604800000,2419200000],\"retention_range\":0,\"engine_type\":2,\"storage_type\":2,\"override_kylin_properties\":{}}"}' http://localhost:7070/kylin/api/cubes
简化后如下:
curl -X POST -H "Authorization: Basic QURNSU46S1lMSU4=" -H "Content-Type: application/json" -d '{ "project":"test_project","cubeName":"test_cube5","cubeDescData":”cube描述的字符串”}' http://localhost:7070/kylin/api/cubes
“QURNSU46S1lMSU4=”是 “ADMIN:KYLIN”的base64编码
2.通过java发送http请求创建cube
private static final String baseURL = "http://localhost:7070/kylin/api";
public static String createCube(String cubeDescFile) {
String method = "POST";
String para = "/cubes";
String cubeDescData = readJsonFile(cubeDescFile);
cubeDescData = cubeDescData.replaceAll("\"", "\\\\\"");
cubeDescData = cubeDescData.replaceAll("[\r\n]", " ");
cubeDescData = cubeDescData.trim();
String body = "{"+"\"cubeDescData\":"+ "\"" + cubeDescData + "\"" +
",\"cubeName\" : \"test_cube7\"" +
",\"project\" : \"test_project\""+
"}";
return excute(para, method, body);
}
private static String excute(String para,String method,String body){
StringBuilder out = new StringBuilder();
try {
URL url = new URL(baseURL+para);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setRequestProperty ("Authorization", "Basic " + encoding);
connection.setRequestProperty("Content-Type","application/json");
if(body !=null){
byte[] outputInBytes = body.getBytes("UTF-8");
OutputStream os = connection.getOutputStream();
os.write(outputInBytes);
os.close();
}
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
out.append(line);
}
in.close();
connection.disconnect();
} catch(Exception e) {
e.printStackTrace();
}
return out.toString();
}
创建model的方式和创建cube的方法类似