Kylin Java RESTful API

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

  1 import java.io.BufferedReader;
  2 import java.io.InputStream;
  3 import java.io.InputStreamReader;
  4 import java.io.OutputStream;
  5 import java.net.HttpURLConnection;
  6 import java.net.URL;
  7
  8 import org.apache.commons.codec.binary.Base64;
  9
 10
 11
 12 /**
 13  *
 14  * @author HennSun
 15  * www.shareideas.net
 16  * @Reference
 17  * http://kylin.apache.org/docs15/howto/howto_use_restapi.html#authentication
 18  *
 19  */
 20 public class KylinHttpBasic {
 21
 22     private static String encoding;
 23     private static final String baseURL = "http://10.1.50.123:7070/kylin/api";
 24     public static String login(String user,String passwd){
 25         String method = "POST";
 26         String para = "/user/authentication";
 27         byte[] key = (user+":"+passwd).getBytes();
 28         encoding = Base64.encodeBase64String(key);
 29         return  excute(para,method,null);
 30     }
 31
 32
 33     public static String listQueryableTables(String projectName){
 34
 35         String method = "GET";
 36         String para = "/tables_and_columns?project="+projectName;
 37
 38         return  excute(para,method,null);
 39
 40     }
 41
 42
 43     /**
 44      *
 45      * @param offset required int Offset used by pagination
 46      * @param limit required int Cubes per page.
 47      * @param cubeName optional string Keyword for cube names. To find cubes whose name contains this keyword.
 48      * @param projectName optional string Project name.
 49      * @return
 50      */
 51     public static String listCubes(int offset,
 52                                    int limit,
 53                                    String cubeName,
 54                                    String projectName ){
 55         String method = "GET";
 56         String para = "/cubes?offset="+offset
 57                             +"&limit="+limit
 58                             +"&cubeName="+cubeName
 59                             +"&projectName="+projectName;
 60         return excute(para,method,null);
 61     }
 62
 63     /**
 64      *
 65      * @param cubeName  Cube name.
 66      * @return
 67      */
 68     public static String getCubeDes(String cubeName){
 69         String method = "GET";
 70         String para = "/cube_desc/"+cubeName;
 71         return excute(para,method,null);
 72
 73     }
 74
 75
 76     /**
 77      *
 78      * @param cubeName
 79      * @return
 80      */
 81     public static String getCube(String cubeName){
 82         String method = "GET";
 83         String para = "/cubes/"+cubeName;
 84         return excute(para,method,null);
 85
 86     }
 87
 88
 89
 90     /**
 91      *
 92      * @param modelName Data model name, by default it should be the same with cube name.
 93      * @return
 94      */
 95     public static String getDataModel(String modelName){
 96         String method = "GET";
 97         String para = "/model/"+modelName;
 98         return excute(para,method,null);
 99
100     }
101
102     /**
103      *
104      * @param cubeName cubeName Cube name.
105      * @return
106      */
107     public static String enableCube(String cubeName){
108
109         String method = "PUT";
110         String para = "/cubes/"+cubeName+"/enable";
111         return excute(para,method,null);
112
113     }
114
115     /**
116      *
117      * @param cubeName Cube name.
118      * @return
119      */
120     public static String disableCube(String cubeName){
121
122         String method = "PUT";
123         String para = "/cubes/"+cubeName+"/disable";
124         return excute(para,method,null);
125
126     }
127
128     /**
129      *
130      * @param cubeName Cube name.
131      * @return
132      */
133     public static String purgeCube(String cubeName){
134
135         String method = "PUT";
136         String para = "/cubes/"+cubeName+"/purge";
137         return excute(para,method,null);
138
139     }
140
141
142     /**
143      *
144      * @param jobId Job id
145      * @return
146      */
147     public static String resumeJob(String jobId){
148
149         String method = "PUT";
150         String para = "/jobs/"+jobId+"/resume";
151         return excute(para,method,null);
152
153     }
154
155
156     /**
157      * startTime - required long Start timestamp of data to build, e.g. 1388563200000 for 2014-1-1
158      * endTime - required long End timestamp of data to build
159      * buildType - required string Supported build type: ‘BUILD’, ‘MERGE’, ‘REFRESH’
160      * @param cubeName  Cube name.
161      * @return
162      */
163     public static String buildCube(String cubeName,String body){
164         String method = "PUT";
165         String para = "/cubes/"+cubeName+"/rebuild";
166
167         return excute(para,method,body);
168     }
169
170
171     /**
172      *
173      * @param jobId  Job id.
174      * @return
175      */
176     public static String discardJob(String jobId){
177
178         String method = "PUT";
179         String para = "/jobs/"+jobId+"/cancel";
180         return excute(para,method,null);
181
182     }
183
184     /**
185      *
186      * @param jobId  Job id.
187      * @return
188      */
189     public static String getJobStatus(String jobId){
190
191         String method = "GET";
192         String para = "/jobs/"+jobId;
193         return excute(para,method,null);
194
195     }
196
197     /**
198      *
199      * @param jobId Job id.
200      * @param stepId  Step id; the step id is composed by jobId with step sequence id;
201      * for example, the jobId is “fb479e54-837f-49a2-b457-651fc50be110”, its 3rd step id
202      * is “fb479e54-837f-49a2-b457-651fc50be110-3”,
203      * @return
204      */
205     public static String getJobStepOutput(String jobId,String stepId){
206         String method = "GET";
207         String para = "/"+jobId+"/steps/"+stepId+"/output";
208         return excute(para,method,null);
209     }
210
211     /**
212      *
213      * @param tableName table name to find.
214      * @return
215      */
216     public static String getHiveTable(String tableName){
217         String method = "GET";
218         String para = "/tables/"+tableName;
219         return excute(para,method,null);
220     }
221
222     /**
223      *
224      * @param tableName  table name to find.
225      * @return
226      */
227     public static String getHiveTableInfo(String tableName){
228         String method = "GET";
229         String para = "/tables/"+tableName+"/exd-map";
230         return excute(para,method,null);
231     }
232
233
234     /**
235      *
236      * @param projectName will list all tables in the project.
237      * @param extOptional boolean set true to get extend info of table.
238      * @return
239      */
240     public static String getHiveTables(String projectName,boolean extOptional){
241         String method = "GET";
242         String para = "/tables?project="+projectName+"&ext="+extOptional;
243         return excute(para,method,null);
244     }
245
246
247     /**
248      *
249      * @param tables  table names you want to load from hive, separated with comma.
250      * @param project the project which the tables will be loaded into.
251      * @return
252      */
253     public static String loadHiveTables(String tables,String project){
254         String method = "POST";
255         String para = "/tables/"+tables+"/"+project;
256         return excute(para,method,null);
257     }
258
259     /**
260      *
261      * @param type ‘METADATA’ or ‘CUBE’
262      * @param name  Cache key, e.g the cube name.
263      * @param action ‘create’, ‘update’ or ‘drop’
264      * @return
265      */
266     public static String wipeCache(String type,String name,String action){
267         String method = "POST";
268         String para = "/cache/"+type+"/"+name+"/"+action;
269         return excute(para,method,null);
270     }
271
272
273     public static String query(String body){
274         String  method = "POST";
275         String para = "/query";
276
277         return excute(para,method,body);
278     }
279
280
281
282     private  static String excute(String para,String method,String body){
283
284         StringBuilder out = new StringBuilder();
285         try {
286             URL url = new URL(baseURL+para);
287             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
288             connection.setRequestMethod(method);
289             connection.setDoOutput(true);
290             connection.setRequestProperty  ("Authorization", "Basic " + encoding);
291             connection.setRequestProperty("Content-Type","application/json");
292             if(body !=null){
293                 byte[] outputInBytes = body.getBytes("UTF-8");
294                 OutputStream os = connection.getOutputStream();
295                 os.write(outputInBytes);
296                 os.close();
297             }
298             InputStream content = (InputStream)connection.getInputStream();
299             BufferedReader in  = new BufferedReader (new InputStreamReader (content));
300             String line;
301             while ((line = in.readLine()) != null) {
302                 out.append(line);
303             }
304             in.close();
305             connection.disconnect();
306
307         } catch(Exception e) {
308             e.printStackTrace();
309         }
310         return out.toString();
311     }
312 }

参考:

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

时间: 2024-10-06 11:21:16

Kylin Java RESTful API的相关文章

java restful api

开发RESTful API的需求范围很广,可选择的开发框架的范围也很广.保持多样性是繁荣生态环境的基础.像Java就有支持JAX-RS规范的Jersey.RESTEasy.Restlet.Apache CXF,和不支持JAX-RS规范的Spring MVC等等很多框架.这些框架目前都做的不错.我对框架的选择没有倾向性.RESTful API设计的最佳实践应该是通用的,而不是必须依赖某种特定的开发框架.

kylin 使用RESTful API 请求

目前根据Kylin的官方文档介绍,Kylin的认证是basic authentication,加密算法是Base64,在POST的header进行用户认证我使用的用户和密码是(格式:username:password) :ADMIN:KYLIN 使用Base64编码后结果为:QURNSU46S1lMSU4=1.在Linux环境下     curl -c cookiefile.txt -X POST -H "Authorization: Basic QURNSU46S1lMSU4="-H

java版spring cloud+spring boot 社交电子商务平台(九)使用Swagger2构建强大的RESTful API文档(1)

由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这些终端会共用很多底层业务逻辑,因此我们会抽象出这样一层来同时服务于多个移动端或者Web前端. 这样一来,我们的RESTful API就有可能要面对多个开发人员或多个开发团队:IOS开发.Android开发或是Web开发等.为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTf

java版spring cloud+spring boot+redis社交电子商务平台(十)使用Swagger2构建强大的RESTful API文档(2)

添加文档内容在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容.如下所示,我们通过@ApiOperation注解来给API增加说明.通过@ApiImplicitParams.@ApiImplicitParam注解来给参数增加说明. @RestController @RequestMapping(value="/users") // 通过这里配置使下面的映射都在/user

3.Spring Boot中使用Swagger2构建强大的RESTful API文档

原文:http://www.jianshu.com/p/8033ef83a8ed 由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这些终端会共用很多底层业务逻辑,因此我们会抽象出这样一层来同时服务于多个移动端或者Web前端. 这样一来,我们的RESTful API就有可能要面对多个开发人员或多个开发团队:IOS开发.Android开发或是Web开发

spring-boot实战【04】:Spring Boot构建RESTful API

@Controller:修饰class,用来创建处理http请求的对象@RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式.@RequestMapping:配置url映射 下面我们尝试使用Spring MVC来实现一组对User对象操作的RESTful API,配合注释详细说明在S

RESTful API 设计最佳实践(转)

摘要:目前互联网上充斥着大量的关于RESTful API(为了方便,以后API和RESTful API 一个意思)如何设计的文章,然而却没有一个”万能“的设计标准:如何鉴权?API格式如何?你的API是否应该加入版本信息? 背景 目前互联网上充斥着大量的关于RESTful API(为了方便,以后API和RESTful API 一个意思)如何设计的文章,然而却没有一个”万能“的设计标准:如何鉴权?API格式如何?你的API是否应该加入版本信息?当你开始写一个app的时候,特别是后端模型部分已经写完

Spring Boot中使用Swagger2生成RESTful API文档(转)

效果如下图所示: 添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <versi

Spring MVC中使用 Swagger2 构建Restful API

1.maven依赖 <!-- 构建Restful API --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>io.spr