kylin -- 使用JDBC连接操作Kylin
程序员文章站
2024-03-22 16:14:22
...
(数据见资源包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();
}
}
结果显示:
下一篇: 53. Maximum Subarray