最近在做大数据方面的开发, 学习研究了一段时间的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. blog已搬迁

    All blogs are moved to my currently-used site: http://jianlu.github.io/

  2. 小学生四则运算出题程序 无操作界面java版 简单的运用java中一些基本操作

    这是本学期java课中实验大纲里的第一个实验,这里简单做了一个无用户界面版本. import java.util.Random; import java.util.Scanner; public cl ...

  3. 10道javascript笔试题

    1.考察this JavaScript var length = 10; function fn() { console.log(this.length); } var obj = { length: ...

  4. 第三个Sprint冲刺第十天

    讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:做最后的工作

  5. 关于自定义Dialog的一次折腾

    <新手,自己的简单理解,勿喷,有更好建议欢迎提出>   1.在设置dialog布局上的时候 我需要做一个圆角的dialog,我像往常一样定义一个<shape>然后做为我的dia ...

  6. Java socket长连接代码实现

    服务器端程序: import java.io.*; import java.net.*; import java.util.*; public class ChatServer { boolean s ...

  7. 对C语言的知识与能力予以自评

    看到一个问卷不错,拟作为第三次作业的部分内容. 你对自己的未来有什么规划?做了哪些准备?我准备在将来成为一名合格的软件工作人员,我已经在平时有空的时间里着手代码的练习. 你认为什么是学习?学习有什么用 ...

  8. Tomcat编码配置解疑

    环境:tomcat6.0.23 jdk 1.6 相关参考: http://tomcat.apache.org/tomcat-7.0-doc/config/ajp.html http://wiki.ap ...

  9. 【转】FPGA内部小数计算

    FPGA内部计算小数  [转载] 谓定点小数,就是小数点的位置是固定的.我们是要用整数来表示定点小数,由于小数点的位置是固定的,所以就没有必要储存它(如果储存了小数点的位置,那就是浮点数了).既然没有 ...

  10. 偶然翻出很久很久以前写的一款sqlmap UI,有点年头了