使用JDBC方式读取Kylin结果
一、准备依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<dependencies>
<dependency>
<groupId>org.apache.kylin</groupId>
<artifactId>kylin-jdbc</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</project>
二、查询数据
import java.sql.*;
public class KylinTest {
public static void main(String[] args) throws sqlException {
//获取连接
Connection connection = DriverManager.getConnection("jdbc:kylin://hadoop102:7070/gmall","ADMIN","KYLIN");
//准备sql
String sql = "select PROVINCE_NAME ,REGION_NAME,ACTIVITY_NAME,ACTIVITY_TYPE ,\n" +
" sum(ORIGINAL_TOTAL_AMOUNT) sum_org,max(BENEFIT_REDUCE_AMOUNT ) max_ben\n" +
"from DWD_FACT_ORDER_INFO t1\n" +
"left join \n" +
"DWD_DIM_BASE_PROVINCE t2 on t1.PROVINCE_ID = t2.ID \n" +
"left join\n" +
"DWD_DIM_ACTIVITY_INFO_VIEW t3 on t1.ACTIVITY_ID = t3.id\n" +
"group by PROVINCE_NAME ,REGION_NAME,ACTIVITY_NAME,ACTIVITY_TYPE ";
//预编译sql
PreparedStatement ps = connection.prepareStatement(sql);
//执行sql
ResultSet resultSet = ps.executeQuery();
//遍历结果(迭代器使用haveNext的方式读取数据)
while (resultSet.next()){
System.out.println(resultSet.getString("PROVINCE_NAME") + " " +
resultSet.getString("REGION_NAME") + " " +
resultSet.getString("ACTIVITY_NAME") + " " +
resultSet.getString("ACTIVITY_TYPE") + " " +
resultSet.getBigDecimal("sum_org") + " " +
resultSet.getBigDecimal("max_ben"));
}
//关闭资源
resultSet.close();
connection.close();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。