目前根据Kylin的官方文档介绍,Kylin的认证是basic authentication,加密算法是Base64。在POST的header进行用户认证:
执行:
curl -c cookiefile.txt -X POST -H "Authorization: Basic QURNSU46S1lMSU4="-H 'Content-Type: application/json' http://GPMASTER:7070/kylin/api/user/authentication
注:
ADMIN:KYLIN使用Base64编码后结果为:
QURNSU46S1lMSU4=
返回结果:
{"userDetails":{"password":null,"username":"ADMIN","authorities":[{"authority":"ROLE_ADMIN"},{"authority":"ROLE_ANALYST"},{"authority":"ROLE_MODELER"}],"accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true,"enabled":true}}
注:
-c:后面接的是cookie写入的文件
-H: Custom header topass to server
-X POST: Specify request command to use
cat cookiefile.txt
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Editat your own risk.
#HttpOnly_GPMASTER FALSE /kylin/ FALSE 0 JSESSIONID 52052E82B421E753D484031F534D63F4
在认证完成之后,可以复用cookie文件(不再需要重新认证),向Kylin发送GET或POST请求,比如,查询cube的信息:
curl -b cookiefile.txt -H 'Content-Type:application/json' http://GPMASTER:7070/kylin/api/cubes/kylin_sales_cube
若要向Kylin发送sql query,则POST请求中的data应遵从json规范(mysql.json):
{
"sql":"select * from KYLIN_SALES",
"offset":0,
"limit":500,
"acceptPartial":false,
"project":"learn_kylin"
}
其中,offset为sql中相对记录首行的偏移量,limit为限制记录条数;二者在后台处理时都会拼接到sql中去。发送sql query的curl命令:
curl -b cookiefile.txt -X POST -H'Content-Type: application/json' -d '{"sql":"select part_dt,sum(price) as total_selled, count(distinct seller_id) as sellers fromkylin_sales group by part_dt", "offset":0, "limit":500,"acceptPartial":false, "project":"learn_kylin"}' http://GPMASTER:7070/kylin/api/query
使用json格式的文件来执行获取数据:
curl -b cookiefile.txt -X POST -H'Content-Type: application/json' -d @mysql.json http://GPMASTER:7070/kylin/api/query
附录1:
1. Base64编码说明
Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式。 如果剩下的字符不足3个字节,则用0填充,输出字符使用'=',因此编码后输出的文本末尾可能会出现1或2个'='。
为了保证所输出的编码位可读字符,Base64制定了一个编码表,以便进行统一转换。编码表的大小为2^6=64,这也是Base64名称的由来。
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一。
2. Base64编码表
码值 |
字符 |
|
码值 |
字符 |
|
码值 |
字符 |
|
码值 |
字符 |
0 |
A |
16 |
Q |
32 |
g |
48 |
w |
|||
1 |
B |
17 |
R |
33 |
h |
49 |
x |
|||
2 |
C |
18 |
S |
34 |
i |
50 |
y |
|||
3 |
D |
19 |
T |
35 |
j |
51 |
z |
|||
4 |
E |
20 |
U |
36 |
k |
52 |
0 |
|||
5 |
F |
21 |
V |
37 |
l |
53 |
1 |
|||
6 |
G |
22 |
W |
38 |
m |
54 |
2 |
|||
7 |
H |
23 |
X |
39 |
n |
55 |
3 |
|||
8 |
I |
24 |
Y |
40 |
o |
56 |
4 |
|||
9 |
J |
25 |
Z |
41 |
p |
57 |
5 |
|||
10 |
K |
26 |
a |
42 |
q |
58 |
6 |
|||
11 |
L |
27 |
b |
43 |
r |
59 |
7 |
|||
12 |
M |
28 |
c |
44 |
s |
60 |
8 |
|||
13 |
N |
29 |
d |
45 |
t |
61 |
9 |
|||
14 |
O |
30 |
e |
46 |
u |
62 |
+ |
|||
15 |
P |
31 |
f |
47 |
v |
63 |
/ |
附录2:
使用Java代码方式实现Base64的编码和解码:
package com.pipeapple.kylin;
import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
public class Base64Demo {
public static void main(String[] args){
String encode_str = "ADMIN:KYLIN";
String decode_str = "QURNSU46S1lMSU4=";
try{
// 编码
byte[] encodeBase64 = Base64.encodeBase64(encode_str.getBytes("UTF-8"));
System.out.println("ENCODE RESULT: " +new String(encodeBase64));
// 解码
byte[] decodeBase64 = Base64.decodeBase64(decode_str.getBytes("UTF-8"));
System.out.println("DECODE RESULT: " +new String(decodeBase64));
} catch(UnsupportedEncodingException e){
e.printStackTrace();
}
}
}
运行结果为:
ENCODE RESULT: QURNSU46S1lMSU4=
DECODE RESULT: ADMIN:KYLIN