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\t"+ "\t" + "total_selled" + "\t" +"sellers");  
                    
         while(resultSet.next()) {  
                   String col1 = resultSet.getString(1);  
                   String col2 = resultSet.getString(2);  
                   String col3 = resultSet.getString(3);  
                   System.out.println(col1+ "\t" + col2 + "\t" + 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+ "\t" + col2 + "\t" + 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+ "\t" + col2 + "\t" + 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訪問

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM