Kylin Java RESTful API
最近在做大数据方面的开发, 学习研究了一段时间的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的更多相关文章
- 【转载】Java Restful API 文档生成工具 smart-doc
谁说生成api文档就必须要定义注解? 谁说生成接口请求和返回示例必须要在线? 用代码去探路,不断尝试更多文档交付的可能性. 如果代码有生命,为什么不换种方式和它对话! 一.背景 没有背景.就自己做自己 ...
- kylin 使用RESTful API 请求
目前根据Kylin的官方文档介绍,Kylin的认证是basic authentication,加密算法是Base64,在POST的header进行用户认证我使用的用户和密码是(格式:username: ...
- Java Fluent Restful API自动化测试框架
这是一个Restful API自动化测试框架,这是一个能让你写出高可读性测试代码的测试框架! 项目目标 话说目前行业内,Restful API自动化测试框架已经不是稀罕物了,各个语言都有自己的实现机制 ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)RESTful API实战笔记(接口设计及Java后端实现)
写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...
- Java框架spring Boot学习笔记(九):一个简单的RESTful API
RESTful API设计需求如下: User.java package com.springboot.test; public class User { private Long id; priva ...
- Java 调用Restful API接口的几种方式--HTTPS
摘要:最近有一个需求,为客户提供一些Restful API 接口,QA使用postman进行测试,但是postman的测试接口与java调用的相似但并不相同,于是想自己写一个程序去测试Restful ...
- RESTful API实战笔记(接口设计及Java后端实现)
写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...
- RESTful API后台系统架构设计(Java)
最近设计和实现了一个JAVA的RESTful API的后台业务系统架构,主要基于Java平台.设计要求是: 性能:平均响应时间(RESTful API)小于2s(平均负载的情况下),并发访问200个以 ...
- elasticsearch组合多条件查询实现restful api以及java代码实现
原文:http://blog.java1234.com/blog/articles/372.html elasticsearch组合多条件查询实现restful api以及java代码实现 实际开发中 ...
随机推荐
- Mac小知识(不定时更新)
1.显示隐藏文件夹(在mac命令行中输入以下代码即可): 1)显示隐藏文件夹 defaults write com.apple.finder AppleShowAllFiles Yes &&a ...
- 配置apache apache服务器如何配置多站点
http://jingyan.baidu.com/article/5225f26b07605be6fa090890.html 让Apache在启动时能加载虚拟主机模块. 打开Apache安装目录下co ...
- C# BackgroundWorker的使用
文章摘自:http://www.cnblogs.com/tom-tong/archive/2012/02/22/2363965.html BackgroundWorker 可以用于启动后台线程. 主要 ...
- CPU指令集
cpu作为一台电脑中的核心,它的作用是无法替代的.而cpu本身只是在块硅晶片上所集成的超大规模的集成电路,集成的晶体管数量可达到上亿个,是由非常先进复杂的制造工艺制造出来的,拥有相当高的科技含量. C ...
- win7绕过开机密码攻略
访问windows机器,经常会因为忘记了开机密码而苦恼.当然你也可以选择重装,这样最简单粗暴.如果有重要数据保存在C盘之类的(个人严重推荐不要保存到C盘),那就不是重装能解决的问题了.2014年12月 ...
- devexpress xaf 开发中遇到的问题.
devexpress xaf 开发中遇到的问题很多久了就忘记了.每天都把开发内容记录下来,方便大家,方便自己
- POSTGRES与JDBC对照
POSTGRES与JDBC对照 未经验证,仅供参考.
- java读取properties文件工具
public class PropertiesUtil { public static String get(String filePath, String key) { String val = n ...
- JAVA文件下载功能问题解决日志
今天给报告系统做了个下载功能,遇到了挺多问题,通过查资料一一解决了. 1.首先遇到的问题是:java后台的输出流输出之后,没有任何报错,浏览器端不弹出保存文件的对话框,原本是ajax请求到后台的con ...
- Servlet页面登录的数据库验证程序(一)
一.基本思想是MVC模式,一个登录页面login.jsp,一个服务器处理程序Servlet.java,一个MySql数据库userinfo. 另外还有相关的数据封装类User和数据库连接类GetDat ...