kylin java查询

来源:互联网 发布:磁选机选矿的数据分析 编辑:程序博客网 时间:2024/06/08 08:46

kylin 提供了java查询的api接口,具体有什么接口 参见官网:http://kylin.apache.org/docs16/

这里介绍一下查询接口

1、使用httpClient post进行数据查询

  1. package kylin;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import net.sf.json.JSONObject;
  5. import org.apache.axis.encoding.Base64;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.client.methods.CloseableHttpResponse;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClients;
  12. import org.apache.http.util.EntityUtils;
  13. public class kylinPost {
  14. private String encoding = "UTF-8";
  15. static String ACCOUNT = "ADMIN";
  16. static String PWD = "KYLIN";
  17. /**
  18. * 使用httpcline 进行post访问
  19. * @throws IOException
  20. */
  21. public void requestByPostMethod() throws IOException{
  22. CloseableHttpClient httpClient = this.getHttpClient();
  23. try {
  24. //创建post方式请求对象
  25. String url ="http://192.168.0.222:7070/kylin/api/query";
  26. HttpPost httpPost = new HttpPost(url);
  27. String sql = "select a.part_dt ,sum(a.price) as sum_price ,max(a.price) as max_price,count(*) as cnt from kylin_sales a "
  28. + " inner join kylin_cal_dt b on a.part_dt = b.cal_dt "
  29. + " inner join kylin_category_groupings c on a.lstg_site_id = c.site_id and a.leaf_categ_id = c.leaf_categ_id "
  30. + " group by a.part_dt ;";
  31. // 接收参数json列表 (kylin 只接受json格式数据)
  32. JSONObject jsonParam = new JSONObject();
  33. jsonParam.put("sql", sql);
  34. jsonParam.put("limit", "20");
  35. jsonParam.put("project","learn_kylin");
  36. StringEntity sentity = new StringEntity(jsonParam.toString(),encoding);//解决中文乱码问题
  37. sentity.setContentEncoding(encoding);
  38. sentity.setContentType("application/json");
  39. httpPost.setEntity(sentity);
  40. //设置header信息
  41. //指定报文头【Content-type】、【User-Agent
  42. httpPost.setHeader("Content-type", "application/json;charset=utf-8");
  43. httpPost.setHeader("Authorization", this.authCode());
  44. System.out.println("POST 请求...." + httpPost.getURI());
  45. //执行请求
  46. CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
  47. try{
  48. HttpEntity entity = httpResponse.getEntity();
  49. if (null != entity){
  50. //按指定编码转换结果实体为String类型
  51. String body = EntityUtils.toString(entity, encoding);
  52. JSONObject obj = JSONObject.fromObject(body);
  53. System.out.println(body);
  54. System.out.println(obj.get("results"));
  55. }
  56. } finally{
  57. httpResponse.close();
  58. }
  59. } catch( UnsupportedEncodingException e){
  60. e.printStackTrace();
  61. }
  62. catch (IOException e) {
  63. e.printStackTrace();
  64. }finally{
  65. this.closeHttpClient(httpClient);
  66. }
  67. }
  68. /**
  69. * kylin base64加密的,访问时候需要加上加密码
  70. * @return
  71. */
  72. private String authCode(){
  73. String auth = ACCOUNT + ":" + PWD;
  74. String code = "Basic "+new String(new Base64().encode(auth.getBytes()));
  75. return code;
  76. }
  77. /**
  78. * 创建httpclient对象
  79. * @return
  80. */
  81. private CloseableHttpClient getHttpClient(){
  82. return HttpClients.createDefault();
  83. }
  84. /**
  85. * 关闭链接
  86. * @param client
  87. * @throws IOException
  88. */
  89. private void closeHttpClient(CloseableHttpClient client) throws IOException{
  90. if (client != null){
  91. client.close();
  92. }
  93. }
  94. public static void main(String[] args) throws IOException{
  95. kylinPost ky = new kylinPost();
  96. ky.requestByPostMethod();
  97. }
  98. }

2、使用jdbc进行查询

  1. package kylin;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Properties;
  8. import org.apache.kylin.jdbc.Driver;
  9. public class KylinJdbc {
  10. public void connentJdbc() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
  11. Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();
  12. Properties info = new Properties();
  13. info.put("user", "ADMIN");
  14. info.put("password", "KYLIN");
  15. Connection conn = driver.connect("jdbc:kylin://192.168.0.222:7070/learn_kylin", info);
  16. Statement state = conn.createStatement();
  17. ResultSet resultSet = state.executeQuery("select * from kylin_sales limit 10 ");
  18. while (resultSet.next()) {
  19. resultSet.getString(1);
  20. System.out.println(resultSet.getString(1));
  21. }
  22. }
  23. public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
  24. KylinJdbc ky = new KylinJdbc();
  25. ky.connentJdbc();
  26. }
  27. }
原创粉丝点击