kylin使用Restful API 创建 cube和model

公司最近需要自动化创建kylin cube和model,便不得不放弃使用web端的方式,而用REST API的方式,找了各种方案,终于找到了可行的。大家可能会问道,代码中定义的cubeDescData 字符串是从哪取的,你可以在web端在创建model和cube时按下F12,查看rest的request请求json串,然后,需要稍微改一下变成我下面代码中这种即可。数据使用的是kylin官方自带的sales数据。

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class KylinRestAPI2 {
    public static void main(String[] args) {
        try {
            createModel();
            System.out.println("创建model中.....");
            Thread.sleep(10000);
            createCube();
            System.out.println("创建cube中.....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static final String baseURL = "http://192.168.xxx.xxx:7070/kylin/api";

    public static String createCube() {
        String method = "POST";
        String para = "/cubes";
        String cubeDescData = "{\"name\":\"cube_test\",\"model_name\":\"model_test\",\"description\":\"\",\"dimensions\":" +
                "[ {\"name\":\"TRANS_ID\",\"table\": \"KYLIN_SALES\",\"column\": \"TRANS_ID\" }, {\"name\": \"PART_DT\",\"table\": " +
                "\"KYLIN_SALES\",\"column\":\"PART_DT\"},{\"name\":\"LSTG_FORMAT_NAME\",\"table\":\"KYLIN_SALES\",\"column\":" +
                "\"LSTG_FORMAT_NAME\"},{\"name\":\"CAL_DT\",\"table\":\"KYLIN_CAL_DT\",\"derived\":[\"CAL_DT\"]},{\"name\":" +
                "\"YEAR_BEG_DT\",\"table\":\"KYLIN_CAL_DT\",\"derived\":[\"YEAR_BEG_DT\"]},{\"name\":\"QTR_BEG_DT\",\"table\":" +
                "\"KYLIN_CAL_DT\",\"derived\":[\"QTR_BEG_DT\"]}],\"measures\":[{\"name\":\"_COUNT_\",\"function\":{\"expression\":" +
                "\"COUNT\",\"returntype\":\"bigint\",\"parameter\":{\"type\":\"constant\",\"value\":\"1\"},\"configuration\":{}}}]," +
                "\"dictionaries\":[],\"rowkey\":{\"rowkey_columns\":[{\"column\":\"KYLIN_SALES.TRANS_ID\",\"encoding\":\"dict\",\"isShardBy\":" +
                "\"false\",\"encoding_version\":1},{\"column\":\"KYLIN_SALES.PART_DT\",\"encoding\":\"dict\",\"isShardBy\":\"false\"," +
                "\"encoding_version\":1},{\"column\":\"KYLIN_SALES.LSTG_FORMAT_NAME\",\"encoding\":\"dict\",\"isShardBy\":\"false\"," +
                "\"encoding_version\":1}]},\"aggregation_groups\":[{\"includes\":[\"KYLIN_SALES.TRANS_ID\",\"KYLIN_SALES.PART_DT\"," +
                "\"KYLIN_SALES.LSTG_FORMAT_NAME\"],\"select_rule\":{\"hierarchy_dims\":[],\"mandatory_dims\":[],\"joint_dims\":[]}}]," +
                "\"partition_date_start\":0,\"notify_list\":[],\"hbase_mapping\":{\"column_family\":[{\"name\":\"F1\",\"columns\":" +
                "[{\"qualifier\":\"M\",\"measure_refs\":[\"_COUNT_\"]}]}]},\"retention_range\":\"0\",\"status_need_notify\":" +
                "[\"ERROR\",\"DISCARDED\",\"SUCCEED\"],\"auto_merge_time_ranges\":[],\"engine_type\":2,\"storage_type\":2,\"override_kylin_properties\":{}}";
        cubeDescData = cubeDescData.replaceAll("\"", "\\\\\"");
        cubeDescData = cubeDescData.replaceAll("[\r\n]", "");
        cubeDescData = cubeDescData.trim();
        String body = "{" + "\"cubeDescData\":" + "\"" + cubeDescData + "\"" +
                ",\"cubeName\" : \"cube_test\"" +
                ",\"project\" :  \"kylin_test\"" +
                "}";
        return excute(para, method, body);
    }

    public static String createModel() {
        String method = "POST";
        String para = "/models";
        String modelDescData = "{\"name\": \"model_test\", \"description\": \"\",\"fact_table\": \"DEFAULT.KYLIN_SALES\",\"lookups\": [{\"table\": " +
                "\"DEFAULT.KYLIN_CAL_DT\",\"alias\": \"KYLIN_CAL_DT\",\"joinTable\": \"KYLIN_SALES\",\"kind\": \"LOOKUP\",\"join\": " +
                "{\"type\": \"inner\",\"primary_key\": [\"KYLIN_CAL_DT.CAL_DT\"],\"foreign_key\": [" +
                "\"KYLIN_SALES.PART_DT\"],\"isCompatible\": [true],\"pk_type\": [\"date\"]," +
                "\"fk_type\": [\"date\"]}}],\"filter_condition\": \"\",\"dimensions\": [{\"table\": \"KYLIN_SALES\"," +
                "\"columns\": [\"TRANS_ID\",\"PART_DT\",\"LSTG_FORMAT_NAME\"]},{\"table\": \"KYLIN_CAL_DT\"," +
                "\"columns\": [\"YEAR_BEG_DT\",\"QTR_BEG_DT\",\"CAL_DT\"]}],\"metrics\": [],\"partition_desc\": { " +
                " \"partition_type\": \"APPEND\",\"partition_date_format\": \"yyyy-MM-dd\"},\"last_modified\": 0}";
        modelDescData = modelDescData.replaceAll("\"", "\\\\\"");
        modelDescData = modelDescData.replaceAll("[\r\n]", " ");
        modelDescData = modelDescData.trim();
        String body = "{" + "\"modelDescData\":" + "\"" + modelDescData + "\"" +
                ",\"modelName\" : \"model_test\"" +
                ",\"project\" :  \"kylin_test\"" +
                "}";
        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 QURNSU46S1lMSU4=");
            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();
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值