[Kylin] 1.Rest API和java获取model_Desc的例子

1.KylinHttpHelper代码

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

import org.apache.commons.codec.binary.Base64;


public class KylinHttpHelper {
    
    private static String encoding;
    private static final String baseURL = "http://128.196.116.75:7070/kylin/api";
    public static String login(String user,String passwd){
  	  	String auth = user + ":" + passwd;
  	  	@SuppressWarnings("restriction")
		String code = new sun.misc.BASE64Encoder() {
            @Override protected int bytesPerLine() {
                return 9999;
            }
        }.encode(new String(auth).getBytes()) + "'";
  	  	System.out.println("CODE:" + code);
  	  	
        String method = "POST";
        String para = "/user/authentication";
        byte[] key = (user+":"+passwd).getBytes();
        encoding = code;
        return  excute(para,method,null);
    }
    
    
    public static String query(String body){
        String  method = "POST";
        String para = "/query";
        
        return excute(para,method,body);
    }
    
    public  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();
    }
    

    
}

其中,sun.misc.BASE64Encoder找不到jar包的解决方法,打开Windows -> Preferences -> Java -> Compiler,参考下图:

 
2.http方式获取cube_desc

package main;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.kylin.cube.model.CubeDesc;
import org.apache.kylin.cube.model.DimensionDesc;
import org.apache.kylin.jdbc.shaded.com.fasterxml.jackson.core.type.TypeReference;
import org.apache.kylin.metadata.model.DataModelDesc;
import org.apache.kylin.metadata.model.JoinDesc;
import org.apache.kylin.metadata.model.LookupDesc;

import main.KylinHttpHelper;

import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;


public class KylinMain {
    private static String newLine = "\r\n";
    

    
	public static void main(String[] args){
		KylinHttpHelper kHB = new KylinHttpHelper();
		KylinHttpHelper.login("ADMIN", "KYLIN");
		/**
		 * GET Cube_Desc: kylin_sales_cube
		 */
		String cubdesc_array = KylinHttpHelper.excute("/cube_desc/kylin_sales_cube?", "GET", null);
		String cubdesc_str=cubdesc_array.substring(1, cubdesc_array.length()-1);
		System.out.println(cubdesc_array);
		System.out.println(cubdesc_str);
		
		JsonParser parse =new JsonParser();  //创建json解析器
		
		try {
			JsonObject json_Cube = new JsonObject();
			
            json_Cube = (JsonObject) parse.parse(cubdesc_str);
            
            System.out.println("version:"+json_Cube.get("version").getAsString());  //将json数据转为为int型的数据
            System.out.println("name:"+json_Cube.get("name").getAsString());     //将json数据转为为String型的数据
            
            String version = json_Cube.get("version").getAsString();	//Kylin_Version
            String cube_Name = json_Cube.get("name").getAsString();	//Cube_Name
            String model_name = json_Cube.get("model_name").getAsString();	//Cube_Version
            //String signature = json.get("signature").getAsString();	//Cube_signature
            //String notify_list = json.get("notify_list").getAsString();	//Cube_notify_list
            String partition_date_start = json_Cube.get("partition_date_start").getAsString();	//Cube_partition_date_start
            String partition_date_end = json_Cube.get("partition_date_end").getAsString();	//Cube_partition_date_end
            //String auto_merge_time_ranges = json.get("auto_merge_time_ranges").getAsString();	//Cube_auto_merge_time_ranges
            String engine_type = json_Cube.get("engine_type").getAsString();	//Cube_engine_type
            String storage_type = json_Cube.get("storage_type").getAsString();	//Cube_storage_type
            
            
    		/**
    		 * GET Model_Desc: kylin_sales_model
    		 */
    		String modelDesc_str = KylinHttpHelper.excute("/model/" + model_name + "?", "GET", null);
    		System.out.println(modelDesc_str);
			JsonObject json_Model = new JsonObject();
			json_Model = (JsonObject) parse.parse(modelDesc_str);
			
			String fact_table_str = json_Model.get("fact_table").getAsString();
			//System.out.println("#####Fact_Table_Str:" + fact_table_str);
			
			
			/***
			 * Model: Get lookups_talbe
			 */
            JsonArray Model_lookups = json_Model.get("lookups").getAsJsonArray();	//dimensions
            
            JsonArray Cube_dimensions_jsArray = json_Cube.get("dimensions").getAsJsonArray();
            System.out.println("%%%%%:" + Cube_dimensions_jsArray.size());


      
        } catch (JsonIOException e) {
            e.printStackTrace();
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
		
		
		CubeDesc cubedesc = null;
		
	}
	
    

	
	
}
分类上一篇:     分类下一篇:

Trackbacks for this post

  1. [Kylin] 2.java方式调用api进行增量build | BigData

Leave a Reply