Apache kylin 入门学习(4) kylin 查询api

编程入门 行业动态 更新时间:2024-10-08 10:58:28

之前我们成功创建了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 .apache.kylin.jdbc.Driver;public class KylinJdbc {public void connentJdbc()throws InstantiationException, IllegalAessException, ClassNotFoundException, SQLException {Driver driver = (Driver) Class.forName(".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_aount 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, IllegalAessException, 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 .apache.axis.encoding.Base64;
import .apache.http.HttpEntity;
import .apache.http.client.methods.CloseableHttpResponse;
import .apache.http.client.methods.HttpPost;
import .apache.http.entity.StringEntity;
import .apache.http.impl.client.CloseableHttpClient;
import .apache.http.impl.client.HttpClients;
import .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 = "*.*.*.*: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();}
}

更多推荐

入门,Apache,kylin,api

本文发布于:2023-05-28 00:46:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/308215.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:入门   Apache   kylin   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!