之前我們成功建立了kylin的cube,并且可以使用web ui查詢.但在真實的生産過程中,使用的是api調用,是以,我們将kylin api進行學習.
官方api文檔:這裡
本文代碼:這裡
lylin有兩種連接配接方法
jdbc
這種方法與mysql,hive相似.
package kylin;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.kylin.jdbc.Driver;
public class KylinJdbc {
public void connentJdbc()
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();
Properties info = new Properties();
info.put("user", "ADMIN");
info.put("password", "KYLIN");
Connection conn = driver.connect("jdbc:kylin://*.*.*.*:7070/learn_kylin", info);
Statement state = conn.createStatement();
System.out.println("1111");
ResultSet resultSet = state.executeQuery("select * from kylin_account limit 1000");
System.out.println("1111");
while (resultSet.next()) {
// System.out.println("1111");
String i = resultSet.getString(3);
System.out.println(i);
// System.out.println("1111");
}
}
public static void main(String[] args)
throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
KylinJdbc ky = new KylinJdbc();
System.out.println("1111111");
ky.connentJdbc();
}
}
post
通過post發送請求,傳回json.
package kylin;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import net.sf.json.JSONObject;
import org.apache.axis.encoding.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class kylinapi {
private String encoding = "UTF-8";
static String ACCOUNT = "ADMIN";
static String PWD = "KYLIN";
/**
* 使用httpcline 進行post通路
*
* @throws IOException
*/
public void requestByPostMethod() throws IOException {
CloseableHttpClient httpClient = this.getHttpClient();
try {
//建立post方式請求對象
String url = "http://*.*.*.*:7070/kylin/api/query";
HttpPost httpPost = new HttpPost(url);
String sql = "select count(*) from KYLIN_SALES ;";
// 接收參數json清單 (kylin 隻接受json格式資料)
JSONObject jsonParam = new JSONObject();
jsonParam.put("sql", sql);
jsonParam.put("limit", "20");
jsonParam.put("project", "lkproject");
StringEntity sentity = new StringEntity(jsonParam.toString(), encoding);// 解決中文亂碼問題
sentity.setContentEncoding(encoding);
sentity.setContentType("application/json");
httpPost.setEntity(sentity);
//設定header資訊
//指定封包頭【Content-type】、【User-Agent】
httpPost.setHeader("Content-type", "application/json;charset=utf-8");
httpPost.setHeader("Authorization", this.authCode());
System.out.println("POST 請求...." + httpPost.getURI());
//執行請求
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
HttpEntity entity = httpResponse.getEntity();
if (null != entity) {
//按指定編碼轉換結果實體為String類型
String body = EntityUtils.toString(entity, encoding);
JSONObject obj = JSONObject.fromObject(body);
System.out.println(body);
System.out.println(obj.get("results"));
}
} finally {
httpResponse.close();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
this.closeHttpClient(httpClient);
}
}
/**
* kylin 是base64加密的,通路時候需要加上加密碼
*
* @return
*/
private String authCode() {
String auth = ACCOUNT + ":" + PWD;
String code = "Basic " + new String(new Base64().encode(auth.getBytes()));
return code;
}
/**
* 建立httpclient對象
*
* @return
*/
private CloseableHttpClient getHttpClient() {
return HttpClients.createDefault();
}
/**
* 關閉連結
*
* @param client
* @throws IOException
*/
private void closeHttpClient(CloseableHttpClient client) throws IOException {
if (client != null) {
client.close();
}
}
public static void main(String[] args) throws IOException {
kylinapi ky = new kylinapi();
ky.requestByPostMethod();
}
}