(數據見資源包kylin)
要將數據以可視化方式展示出來,需要使用Kylin的JDBC方式連接執行SQL,獲取Kylin的執行結果
使用Kylin的JDBC與JDBC操作MySQL一致
jdbc url
- jdbc:kylin://node1:7070/tt01
- 用戶名密碼:ADMIN/KYLIN
需求
通過JDBC方式,查詢按照日期、區域、產品維度統計訂單總額/總數量結果
開發步驟
- 導入驅動依賴
<dependencies>
<!-- Kylin -->
<dependency>
<groupId>org.apache.kylin</groupId>
<artifactId>kylin-jdbc</artifactId>
<version>2.6.3</version>
</dependency>
</dependencies>
1、加載驅動
2、創建Connection連接對象
3、構建SQL語句
4、創建Statement對象,並執行executeQuery
5、打印結果
參考代碼
package com.kismet.demo01;
import java.sql.*;
/**
* @author kismet
* @date 2020-05-17 9:33
*/
public class tt01 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("org.apache.kylin.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:kylin://node01:7070/tt01", "ADMIN", "KYLIN");
Statement statement = connection.createStatement();
String sql = "select\n" +
" t1.date1,\n" +
" t2.regionname,\n" +
" productname,\n" +
" sum(t1.price) as total_money,\n" +
" sum(t1.amount) as total_amount\n" +
"from\n" +
" dw_sales t1\n" +
"inner join dim_region t2\n" +
"on t1.regionid = t2.regionid\n" +
"inner join dim_product t3\n" +
"on t1.productid = t3.productid\n" +
"group by\n" +
" t1.date1,\n" +
" t2.regionid,\n" +
" t2.regionname,\n" +
" t3.productid,\n" +
" t3.productname";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 4.1 獲取時間
String date1 = resultSet.getString("date1");
// 4.2 獲取區域名稱
String regionname = resultSet.getString("regionname");
// 4.3 獲取產品名稱
String productname = resultSet.getString("productname");
// 4.4 總金額
String total_money = resultSet.getString("total_money");
// 4.5 總數量
String total_amount = resultSet.getString("total_amount");
System.out.println(date1 + " " + regionname + " " + productname + " " + total_money + " " + total_amount);
}
connection.close();
}
}
結果顯示: