Kylin如何进行JDBC方式访问或者调用

       Kylin提供了标准的ODBC和JDBC接口,能够和传统BI工具进行很好的集成。分析师们可以用他们最熟悉的工具来享受Kylin带来的快速。我们也可以对它进行定制开发报表等,把kylin当做数据库服务器就行了。

首先我们来看一下连接Kylin的URL格式为:

jdbc:kylin://<hostname>:<port>/<kylin_project_name>

注:

如果“ssl”为true话,那么上面的端口号应该为Kylin服务的HTTPS端口号。

 kylin_project_name必须指定,并且在Kylin服务中存在。

运行环境需要把安装文件的那个lib目录拷贝到自己的项目引用下

下面介绍几种方式访问Kylin数据:

第一种方法:使用Statement方式查询

第二种方法:使用PreparedStatement方式查询

第三种方法:获取查询结果集元数据

package org.apache.kylin.jdbc;

import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
import java.sql.Connection;  
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;  
import java.sql.Statement;  

/** 
* @author : 
* @date   : 2017年4月17日 
* @version 1.0 
* @parameter  
*/
public class QueryKylinST {

    
    @Test
    public void testStatementWithMockData() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        // 加载Kylin的JDBC驱动程序  
        Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();  
   
         // 配置登录Kylin的用户名和密码  
         Properties info= new Properties();  
         info.put("user","ADMIN");  
         info.put("password","KYLIN");  
   
         // 连接Kylin服务  
         Connection conn= driver.connect("jdbc:kylin://10.8.217.66:7070/learn_kylin",info);  
         Statement state= conn.createStatement();  
         ResultSet resultSet =state.executeQuery("select part_dt, sum(price) as total_selled,count(distinct seller_id) as sellers " +  
                                     "from kylin_sales group by part_dt order by part_dt limit 5");  
   
         System.out.println("part_dt	"+ "	" + "total_selled" + "	" +"sellers");  
                    
         while(resultSet.next()) {  
                   String col1 = resultSet.getString(1);  
                   String col2 = resultSet.getString(2);  
                   String col3 = resultSet.getString(3);  
                   System.out.println(col1+ "	" + col2 + "	" + col3);  
         }  
    }
    
    @Test
    public  void testanylist() throws Exception {  
        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://10.8.217.66:7070/learn_kylin",info);  
  
        PreparedStatement state = conn.prepareStatement("select * from KYLIN_CATEGORY_GROUPINGS where LEAF_CATEG_ID = ?");  
        state.setLong(1,10058);  
                   
        ResultSet resultSet = state.executeQuery();  
        while (resultSet.next()) {  
                  String col1 = resultSet.getString(1);  
                  String col2 = resultSet.getString(2);  
                  String col3 = resultSet.getString(3);  
                  System.out.println(col1+ "	" + col2 + "	" + col3);  
            }  
     }  
    
    @Test
    public  void testmetadatalist() throws Exception {  
        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://10.8.217.66:7070/learn_kylin", info);
        Statement state = conn.createStatement();
        ResultSet resultSet = state.executeQuery("select * from kylin_sales");

        //第三个是表名称,一般情况下如果要获取所有的表的话,可以直接设置为null,如果设置为特定的表名称,则返回该表的具体信息。
        ResultSet tables = conn.getMetaData().getTables(null, null, null, new String[]{"TABLE"});
        while (tables.next()) {
//            for (int i = 0; i < 10; i++) {
//                System.out.println(tables.getString(i + 1));               
//            }
            String col1 = tables.getString(1);  //表类别
            String col2 = tables.getString(2);  //表模式
            String col3 = tables.getString(3);  //表名称
            System.out.println("表信息:"+col1+ "	" + col2 + "	" + col3);  
        }
     }  
}

结果如下:由于单元测试,先后顺序不一致,注意忽视

 参考资料

http://kylin.apache.org/docs20/howto/howto_jdbc.html
http://www.cnblogs.com/en-heng/p/5420269.html Python Post方式
http://blog.csdn.net/linlinv3/article/details/53893144 Java 使用httpClient 进行post访问

原文地址:https://www.cnblogs.com/mistor/p/6724977.html