1 importjava.io.BufferedReader;2 importjava.io.InputStream;3 importjava.io.InputStreamReader;4 importjava.io.OutputStream;5 importjava.net.HttpURLConnection;6 importjava.net.URL;7
8 importorg.apache.commons.codec.binary.Base64;9
10
11
12 /**
13 *14 *@authorHennSun15 * www.shareideas.net16 * @Reference17 *http://kylin.apache.org/docs15/howto/howto_use_restapi.html#authentication18 *19 */
20 public classKylinHttpBasic {21
22 private staticString encoding;23 private static final String baseURL = "http://10.1.50.123:7070/kylin/api";24 public staticString login(String user,String passwd){25 String method = "POST";26 String para = "/user/authentication";27 byte[] key = (user+":"+passwd).getBytes();28 encoding =new sun.misc.BASE64Encoder().encode(key);29 return excute(para,method,null);30 }31
32
33 public staticString 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 *@paramoffset required int Offset used by pagination46 *@paramlimit required int Cubes per page.47 *@paramcubeName optional string Keyword for cube names. To find cubes whose name contains this keyword.48 *@paramprojectName optional string Project name.49 *@return
50 */
51 public static String listCubes(intoffset,52 intlimit,53 String cubeName,54 String projectName ){55 String method = "GET";56 String para = "/cubes?offset="+offset57 +"&limit="+limit58 +"&cubeName="+cubeName59 +"&projectName="+projectName;60 return excute(para,method,null);61 }62
63 /**
64 *65 *@paramcubeName Cube name.66 *@return
67 */
68 public staticString 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 *@paramcubeName79 *@return
80 */
81 public staticString 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 *@parammodelName Data model name, by default it should be the same with cube name.93 *@return
94 */
95 public staticString getDataModel(String modelName){96 String method = "GET";97 String para = "/model/"+modelName;98 return excute(para,method,null);99
100 }101
102 /**
103 *104 *@paramcubeName cubeName Cube name.105 *@return
106 */
107 public staticString 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 *@paramcubeName Cube name.118 *@return
119 */
120 public staticString 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 *@paramcubeName Cube name.131 *@return
132 */
133 public staticString 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 *@paramjobId Job id145 *@return
146 */
147 public staticString 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-1158 * endTime - required long End timestamp of data to build159 * buildType - required string Supported build type: ‘BUILD’, ‘MERGE’, ‘REFRESH’160 *@paramcubeName Cube name.161 *@return
162 */
163 public staticString buildCube(String cubeName,String body){164 String method = "PUT";165 String para = "/cubes/"+cubeName+"/rebuild";166
167 returnexcute(para,method,body);168 }169
170
171 /**
172 *173 *@paramjobId Job id.174 *@return
175 */
176 public staticString 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 *@paramjobId Job id.187 *@return
188 */
189 public staticString 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 *@paramjobId Job id.200 *@paramstepId 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 id202 * is “fb479e54-837f-49a2-b457-651fc50be110-3”,203 *@return
204 */
205 public staticString 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 *@paramtableName table name to find.214 *@return
215 */
216 public staticString getHiveTable(String tableName){217 String method = "GET";218 String para = "/tables/"+tableName;219 return excute(para,method,null);220 }221
222 /**
223 *224 *@paramtableName table name to find.225 *@return
226 */
227 public staticString 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 *@paramprojectName will list all tables in the project.237 *@paramextOptional boolean set true to get extend info of table.238 *@return
239 */
240 public static String getHiveTables(String projectName,booleanextOptional){241 String method = "GET";242 String para = "/tables?project="+projectName+"&ext="+extOptional;243 return excute(para,method,null);244 }245
246
247 /**
248 *249 *@paramtables table names you want to load from hive, separated with comma.250 *@paramproject the project which the tables will be loaded into.251 *@return
252 */
253 public staticString 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 *@paramtype ‘METADATA’ or ‘CUBE’262 *@paramname Cache key, e.g the cube name.263 *@paramaction ‘create’, ‘update’ or ‘drop’264 *@return
265 */
266 public staticString 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 staticString query(String body){274 String method = "POST";275 String para = "/query";276
277 returnexcute(para,method,body);278 }279
280
281
282 private staticString excute(String para,String method,String body){283
284 StringBuilder out = newStringBuilder();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 (newInputStreamReader (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 returnout.toString();311 }312 }