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访问

Kylin如何进行JDBC方式访问或者调用的更多相关文章

  1. 2017.11.12 web中JDBC 方式访问数据库的技术

    JavaWeb------ 第四章 JDBC数据库访问技术 在JavaWeb应用程序中数据库访问是通过Java数据库连接(JavaDateBase Connectivity简称JDBC)数据库的链接一 ...

  2. Impala通过JDBC方式访问

    不多说,直接上干货! • 配置: – impala.driver=org.apache.hive.jdbc.HiveDriver – impala.url=jdbc:hive2://node2:210 ...

  3. 使用jdbc的方式访问kylin cube的数据

    使用jdbc的方式访问kylin cube的数据 引用kylin相关的jar包 <dependency> <groupId>org.apache.kylin</group ...

  4. Spring04-SpringEL&Spring JDBC数据访问

    一. SpringEL入门 Spring动态语言(简称SpEL) 是一个支持运行时查询和操作对象图的强大的动态语言,语法类似于EL表达式,具有诸如显示方法和基本字符串模板函数等特性. 1. 准备工作 ...

  5. jdbc数据访问技术

    jdbc数据访问技术 1.JDBC如何做事务处理? Con.setAutoCommit(false) Con.commit(); Con.rollback(); 2.写出几个在Jdbc中常用的接口 p ...

  6. 使用原生JDBC方式对数据库进行操作

    使用原生JDBC方式对数据库进行操作,包括六个步骤: 1.加载JDBC驱动程序 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM.可以通过java.lang.Class类的静态方法forNa ...

  7. SharePoint—用REST方式访问列表

    REST的定义与作用 在SharePoint 2010中,基本上有如下几种数据访问方式: 服务器端对象模型 LINQ to SharePoint Web Service 客户端对象模型 ADO.NET ...

  8. salesforce 零基础学习(三十三)通过REST方式访问外部数据以及JAVA通过rest方式访问salesforce

    本篇参考Trail教程: https://developer.salesforce.com/trailhead/force_com_dev_intermediate/apex_integration_ ...

  9. URL方式访问Hadoop的内容

    * 1.设置url支持hadoop,FsUrlStreamHandlerFactory      * 2.创建URL对象,指定访问的HDFS路径      * 3.openStream获取输入流对象, ...

随机推荐

  1. JS_高阶函数(sort)

    //javaScript sort()排序算法 //sort()方法默认把所有的元素转换成String再排序,字符串是根据ASCII进行排序的,所以会出现“10”排在“2”前面,或是小写字母“a”排在 ...

  2. CSS_级联和继承

    2016-11-06 <CSS入门经典>第七章 1.在HTML中使用CSS样式表的三种方式: (1)内联的样式表. eg:<em style="background-whi ...

  3. 牛客网-C++

    2017/8/18 程序运行结束时才释放:静态变量在内存的静态存储区,静态数据一直占有着该存储单元直到程序结束:一般局部变量在函数调用结束后释放变量占用的存储单元,而静态局部变量不释放. 静态全局变量 ...

  4. JAVA自学作业03

    JAVA自学作业03 1.请用最有效率的方式计算2*8的结果 使用位运算是最有效率的. System.out.println(2<<3); 2.请交换两整数的值 int x = 10; i ...

  5. IDEA手工添加webapp目录

    自己手工建目录,是没法识别的,在自己手工建的webapp文件夹上右键菜单,Make Directory As也没有相应的选项 解决方案是 File->Project Structure

  6. Siamese网络

    1.       对比损失函数(Contrastive Loss function) 孪生架构的目的不是对输入图像进行分类,而是区分它们.因此,分类损失函数(如交叉熵)不是最合适的选择,这种架构更适合 ...

  7. CentOS ISO版本区别

    CentOS6 CentOS7 介绍 区别 bin-DVD.iso bin-DVD.iso 系统标准安装包 bin DVD本身包含了软件,不需要依赖于网络经行安装. bin-DVD2.iso Ever ...

  8. MDD Cup 2017 小记

    http://blog.csdn.net/zhangzhengyuan123123/article/details/78971298

  9. mybatis 批量添加

    <insert id="addTrackBatch" parameterType="java.util.List"> INSERT INTO t_t ...

  10. Windows 7安装Tensorflow

    以前是在Cent OS中运行Tensorflow,,经常需要切换操作系统,很不方便,于是决定在Windows 7下安装Tensorflow. 过程还是挺复杂的,需要安装的包括:Visual Studi ...