使用calcite为对象List封装SQL接口
2016-05-19 21:30
471 查看
编写示例程序如下,注意这是一个scala程序:
import java.sql.DriverManager
import org.apache.calcite.jdbc.CalciteConnection
import org.apache.calcite.adapter.java.ReflectiveSchema
import java.util.Properties
object CalciteTest {
def main(args: Array[String]) = {
Class.forName("org.apache.calcite.jdbc.Driver");
val info = new Properties();
info.setProperty("lex", "JAVA");
val connection = DriverManager.getConnection("jdbc:calcite:", info);
val calciteConnection =
connection.asInstanceOf[CalciteConnection];
val rootSchema = calciteConnection.getRootSchema();
val hrs = new ReflectiveSchema(new JavaHrSchema())
rootSchema.add("hr", hrs);
val statement = calciteConnection.createStatement();
val resultSet = statement.executeQuery(
"""select * from hr.emps as e
join hr.depts as d on e.deptno = d.deptno""");
while (resultSet.next()) {
(1 to resultSet.getMetaData.getColumnCount).foreach(x => print(resultSet.getObject(x) + "\t"));
println("");
}
resultSet.close();
statement.close();
connection.close();
}
}
其中的JavaHrSchema如下,它包含2个字段emps和deps,分别模拟2张表:public class JavaHrSchema
{
public static class Employee
{
public final int empid;
public final String name;
public final int deptno;
public Employee(int empid, String name, int deptno)
{
this.empid = empid;
this.name = name;
this.deptno = deptno;
}
}
public static class Department
{
public final String name;
public final int deptno;
public Department(int deptno, String name)
{
this.name = name;
this.deptno = deptno;
}
}
public final Employee[] emps = { new Employee(100, "bluejoe", 1),
new Employee(200, "wzs", 2), new Employee(150, "wxz", 1) };
public final Department[] depts = { new Department(1, "dev"),
new Department(2, "market") };
}
运行结果如下:
100 bluejoe 1 dev 1
200 wzs 2 market 2
150 wxz 1 dev 1
这么臃肿的java代码确实有点丑陋,原本想把JavaHrSchema写成如下scala代码:class HrSchema {
val emps: Array[Employee] = Array(new Employee(100, "bluejoe", 1),
new Employee(200, "wzs", 2),
new Employee(150, "wxz", 1));
val depts: Array[Department] = Array(new Department(1, "dev"),
new Department(2, "market"));
}
class Employee(val empid: Int, val name: String, val deptno: Int) {}
class Department(val deptno: Int, val name: String) {}
结果出错了,原因是ReflectiveSchema读取不到HrSchema里面的public字段,因为scala自动把它变成了private。。。
import java.sql.DriverManager
import org.apache.calcite.jdbc.CalciteConnection
import org.apache.calcite.adapter.java.ReflectiveSchema
import java.util.Properties
object CalciteTest {
def main(args: Array[String]) = {
Class.forName("org.apache.calcite.jdbc.Driver");
val info = new Properties();
info.setProperty("lex", "JAVA");
val connection = DriverManager.getConnection("jdbc:calcite:", info);
val calciteConnection =
connection.asInstanceOf[CalciteConnection];
val rootSchema = calciteConnection.getRootSchema();
val hrs = new ReflectiveSchema(new JavaHrSchema())
rootSchema.add("hr", hrs);
val statement = calciteConnection.createStatement();
val resultSet = statement.executeQuery(
"""select * from hr.emps as e
join hr.depts as d on e.deptno = d.deptno""");
while (resultSet.next()) {
(1 to resultSet.getMetaData.getColumnCount).foreach(x => print(resultSet.getObject(x) + "\t"));
println("");
}
resultSet.close();
statement.close();
connection.close();
}
}
其中的JavaHrSchema如下,它包含2个字段emps和deps,分别模拟2张表:public class JavaHrSchema
{
public static class Employee
{
public final int empid;
public final String name;
public final int deptno;
public Employee(int empid, String name, int deptno)
{
this.empid = empid;
this.name = name;
this.deptno = deptno;
}
}
public static class Department
{
public final String name;
public final int deptno;
public Department(int deptno, String name)
{
this.name = name;
this.deptno = deptno;
}
}
public final Employee[] emps = { new Employee(100, "bluejoe", 1),
new Employee(200, "wzs", 2), new Employee(150, "wxz", 1) };
public final Department[] depts = { new Department(1, "dev"),
new Department(2, "market") };
}
运行结果如下:
100 bluejoe 1 dev 1
200 wzs 2 market 2
150 wxz 1 dev 1
这么臃肿的java代码确实有点丑陋,原本想把JavaHrSchema写成如下scala代码:class HrSchema {
val emps: Array[Employee] = Array(new Employee(100, "bluejoe", 1),
new Employee(200, "wzs", 2),
new Employee(150, "wxz", 1));
val depts: Array[Department] = Array(new Department(1, "dev"),
new Department(2, "market"));
}
class Employee(val empid: Int, val name: String, val deptno: Int) {}
class Department(val deptno: Int, val name: String) {}
结果出错了,原因是ReflectiveSchema读取不到HrSchema里面的public字段,因为scala自动把它变成了private。。。
相关文章推荐
- MySQL5.7.12新密码登录方式及密码策略
- oracle拼接字段和group by的使用
- Windows下SQLMAP的安装图解
- NoSQL数据库Redis使用命令简介
- MySQL入门--修改mysql提示符的两种方法
- 记mysql 之 delete where 一张很大(20G)的表
- 详解 MySQL 5.7 新的权限与安全问题
- 【知识积累】使用Navicat连接Oracle数据库遇到的问题
- redis学习之java类对redis库操作的简单封装
- mysql入门--采用命令行进行登陆与退出
- sql server学习
- SQL FORMAT() 函数实例
- MySQL修改列名称或列的数据类型
- redis数据库
- 数据库去重的一种做法(借助临时表)
- MySQL数据库入门--软件下载与安装、开启服务、停止服务
- MYSQL 函数大全
- oracle sql学习三
- MySQL添加用户、删除用户与授权
- js,jsp里将数据库Date类型获取出来后格式化显示于界面