最近在做大数据方面的开发, 学习研究了一段时间的kylin系统, 对于前端开发需要使用 RESTful API ,但是官网并没有提供详细的Java  API. 经过几天的看文档,最终写出了 Java 的API,不敢私藏,特分享与大家.

 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; /**
*
* @author HennSun
* www.shareideas.net
* @Reference
* http://kylin.apache.org/docs15/howto/howto_use_restapi.html#authentication
*
*/
public class KylinHttpBasic { private static String encoding;
private static final String baseURL = "http://10.1.50.123:7070/kylin/api";
public static String login(String user,String passwd){
String method = "POST";
String para = "/user/authentication";
byte[] key = (user+":"+passwd).getBytes();
encoding = new sun.misc.BASE64Encoder().encode(key);
return excute(para,method,null);
} public static String listQueryableTables(String projectName){ String method = "GET";
String para = "/tables_and_columns?project="+projectName; return excute(para,method,null); } /**
*
* @param offset required int Offset used by pagination
* @param limit required int Cubes per page.
* @param cubeName optional string Keyword for cube names. To find cubes whose name contains this keyword.
* @param projectName optional string Project name.
* @return
*/
public static String listCubes(int offset,
int limit,
String cubeName,
String projectName ){
String method = "GET";
String para = "/cubes?offset="+offset
+"&limit="+limit
+"&cubeName="+cubeName
+"&projectName="+projectName;
return excute(para,method,null);
} /**
*
* @param cubeName Cube name.
* @return
*/
public static String getCubeDes(String cubeName){
String method = "GET";
String para = "/cube_desc/"+cubeName;
return excute(para,method,null); } /**
*
* @param cubeName
* @return
*/
public static String getCube(String cubeName){
String method = "GET";
String para = "/cubes/"+cubeName;
return excute(para,method,null); } /**
*
* @param modelName Data model name, by default it should be the same with cube name.
* @return
*/
public static String getDataModel(String modelName){
String method = "GET";
String para = "/model/"+modelName;
return excute(para,method,null); } /**
*
* @param cubeName cubeName Cube name.
* @return
*/
public static String enableCube(String cubeName){ String method = "PUT";
String para = "/cubes/"+cubeName+"/enable";
return excute(para,method,null); } /**
*
* @param cubeName Cube name.
* @return
*/
public static String disableCube(String cubeName){ String method = "PUT";
String para = "/cubes/"+cubeName+"/disable";
return excute(para,method,null); } /**
*
* @param cubeName Cube name.
* @return
*/
public static String purgeCube(String cubeName){ String method = "PUT";
String para = "/cubes/"+cubeName+"/purge";
return excute(para,method,null); } /**
*
* @param jobId Job id
* @return
*/
public static String resumeJob(String jobId){ String method = "PUT";
String para = "/jobs/"+jobId+"/resume";
return excute(para,method,null); } /**
* startTime - required long Start timestamp of data to build, e.g. 1388563200000 for 2014-1-1
* endTime - required long End timestamp of data to build
* buildType - required string Supported build type: ‘BUILD’, ‘MERGE’, ‘REFRESH’
* @param cubeName Cube name.
* @return
*/
public static String buildCube(String cubeName,String body){
String method = "PUT";
String para = "/cubes/"+cubeName+"/rebuild"; return excute(para,method,body);
} /**
*
* @param jobId Job id.
* @return
*/
public static String discardJob(String jobId){ String method = "PUT";
String para = "/jobs/"+jobId+"/cancel";
return excute(para,method,null); } /**
*
* @param jobId Job id.
* @return
*/
public static String getJobStatus(String jobId){ String method = "GET";
String para = "/jobs/"+jobId;
return excute(para,method,null); } /**
*
* @param jobId Job id.
* @param stepId Step id; the step id is composed by jobId with step sequence id;
* for example, the jobId is “fb479e54-837f-49a2-b457-651fc50be110”, its 3rd step id
* is “fb479e54-837f-49a2-b457-651fc50be110-3”,
* @return
*/
public static String getJobStepOutput(String jobId,String stepId){
String method = "GET";
String para = "/"+jobId+"/steps/"+stepId+"/output";
return excute(para,method,null);
} /**
*
* @param tableName table name to find.
* @return
*/
public static String getHiveTable(String tableName){
String method = "GET";
String para = "/tables/"+tableName;
return excute(para,method,null);
} /**
*
* @param tableName table name to find.
* @return
*/
public static String getHiveTableInfo(String tableName){
String method = "GET";
String para = "/tables/"+tableName+"/exd-map";
return excute(para,method,null);
} /**
*
* @param projectName will list all tables in the project.
* @param extOptional boolean set true to get extend info of table.
* @return
*/
public static String getHiveTables(String projectName,boolean extOptional){
String method = "GET";
String para = "/tables?project="+projectName+"&ext="+extOptional;
return excute(para,method,null);
} /**
*
* @param tables table names you want to load from hive, separated with comma.
* @param project the project which the tables will be loaded into.
* @return
*/
public static String loadHiveTables(String tables,String project){
String method = "POST";
String para = "/tables/"+tables+"/"+project;
return excute(para,method,null);
} /**
*
* @param type ‘METADATA’ or ‘CUBE’
* @param name Cache key, e.g the cube name.
* @param action ‘create’, ‘update’ or ‘drop’
* @return
*/
public static String wipeCache(String type,String name,String action){
String method = "POST";
String para = "/cache/"+type+"/"+name+"/"+action;
return excute(para,method,null);
} public static String query(String body){
String method = "POST";
String para = "/query"; 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();
}
}

参考:

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

Kylin Java RESTful API的更多相关文章

  1. 【转载】Java Restful API 文档生成工具 smart-doc

    谁说生成api文档就必须要定义注解? 谁说生成接口请求和返回示例必须要在线? 用代码去探路,不断尝试更多文档交付的可能性. 如果代码有生命,为什么不换种方式和它对话! 一.背景 没有背景.就自己做自己 ...

  2. kylin 使用RESTful API 请求

    目前根据Kylin的官方文档介绍,Kylin的认证是basic authentication,加密算法是Base64,在POST的header进行用户认证我使用的用户和密码是(格式:username: ...

  3. Java Fluent Restful API自动化测试框架

    这是一个Restful API自动化测试框架,这是一个能让你写出高可读性测试代码的测试框架! 项目目标 话说目前行业内,Restful API自动化测试框架已经不是稀罕物了,各个语言都有自己的实现机制 ...

  4. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)RESTful API实战笔记(接口设计及Java后端实现)

    写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...

  5. Java框架spring Boot学习笔记(九):一个简单的RESTful API

    RESTful API设计需求如下: User.java package com.springboot.test; public class User { private Long id; priva ...

  6. Java 调用Restful API接口的几种方式--HTTPS

    摘要:最近有一个需求,为客户提供一些Restful API 接口,QA使用postman进行测试,但是postman的测试接口与java调用的相似但并不相同,于是想自己写一个程序去测试Restful ...

  7. RESTful API实战笔记(接口设计及Java后端实现)

    写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...

  8. RESTful API后台系统架构设计(Java)

    最近设计和实现了一个JAVA的RESTful API的后台业务系统架构,主要基于Java平台.设计要求是: 性能:平均响应时间(RESTful API)小于2s(平均负载的情况下),并发访问200个以 ...

  9. elasticsearch组合多条件查询实现restful api以及java代码实现

    原文:http://blog.java1234.com/blog/articles/372.html elasticsearch组合多条件查询实现restful api以及java代码实现 实际开发中 ...

随机推荐

  1. jQuery外部框架浅析

    (function(window, undefined) {         var jQuery = ...         ...             window.jQuery = wind ...

  2. EasyUI中Grid标题居中方法(jquery实现方法)

    $(".panel-title").css("text-align", "center"); 一句话搞定,就是这么随意

  3. 问题:C++形参默认值为什么一定要放在最后?

    问题:C++形参默认值为什么一定要放在最后? 1.会出现歧义! 2.从汇编角度看是怎么回事?   待解答!!

  4. C++学习笔记34:泛型编程拓展3

    输入流迭代器 #include <iostream> #include <iterator> #include <algorithm> #include <v ...

  5. 常用的CSS Hack

    一.什么是CSS Hack? 不同的浏览器对CSS的解析结果是不同的,因此会导致相同的CSS输出的页面效果不同,这就需要CSS Hack来解决浏览器局部的兼容性问题.而这个针对不同的浏览器写不同的CS ...

  6. appserver安装常见的问题

    安装过程: 一般下载安装包直接按照步骤安装,不过一次安装好没问题的情况很少. 1.       下载安装包百度搜索appserv 或者到以下网址下载 http://www.appservnetwork ...

  7. 水平ListView类

    package com.hztbc.android.HorizontalListView; /* * HorizontalListView.java v1.5 * *  * The MIT Licen ...

  8. xamarin学习之页面布局

    在android应中,需要注意3个文件,他们分别是:Main.axml,String.xml和Activity.cs. 1.布局文件Main.axml ,该文件保存在Resouses/layout的目 ...

  9. js中属性和方法的类型和区别

    对象的属性:私有属性(var).类属性(静态属性).对象属性(this).原型属性(prototype). 对象的方法: 私有方法(funtion).类方法(静态方法).对象方法(this).原型方法 ...

  10. coderforces #387 Servers(模拟)

    Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...