官方文档 http://kylin.apache.org/docs23/tutorial/jdbc.html
1、pom.xml
代码语言:javascript复制<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.kylin/kylin-jdbc -->
<dependency>
<groupId>org.apache.kylin</groupId>
<artifactId>kylin-jdbc</artifactId>
<version>2.3.1</version>
</dependency>
...
</dependencies>
2、Statement
代码语言:javascript复制package com.cntaiping.tpa.kylin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class KylinDemo1 {
public static void main(String[] args) throws Exception{
String conStr = "jdbc:kylin://10.17.12.160:7070/learn_kylin";
Class.forName("org.apache.kylin.jdbc.Driver");
Connection conn = DriverManager.getConnection(conStr, "ADMIN", "KYLIN");
Statement state= conn.createStatement();
String sql="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";
ResultSet rs=state.executeQuery(sql);
System.out.println("part_dtt" "t" "total_selled" "t" "sellers");
while(rs.next()) {
System.out.println(rs.getString(1) "t" rs.getString(2) "t" rs.getString(3));
}
}
}
运行结果
代码语言:javascript复制part_dt total_selled sellers
2011-12-31 466.9037 12
2012-01-01 970.2347 17
2012-01-02 917.4138 14
2012-01-03 553.0541 10
2012-01-04 732.9007 18
3、PreparedStatement
代码语言:javascript复制package com.cntaiping.tpa.kylin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class KylinDemo2 {
public static void main(String[] args) throws Exception{
String conStr = "jdbc:kylin://10.17.12.160:7070/learn_kylin";
Class.forName("org.apache.kylin.jdbc.Driver");
Connection conn = DriverManager.getConnection(conStr, "ADMIN", "KYLIN");
String sql="select * from KYLIN_CATEGORY_GROUPINGS where LEAF_CATEG_ID = ?";
PreparedStatement state = conn.prepareStatement(sql);
state.setLong(1,10058);
ResultSet rs= state.executeQuery();
while(rs.next()) {
System.out.println(rs.getString(1) "t" rs.getString(2) "t" rs.getString(3));
}
}
}