- Calcite原理和代码讲解(一) https://blog.csdn.net/qq_35494772/article/details/118887267
- quickstart:Apache Calcite精简入门与学习指导 https://blog.51cto.com/xpleaf/2639844
- quickstart:多源数据的关联 csv和mem数据类型 https://cloud.tencent.com/developer/article/1622438
- Apache Calcite: A Foundational Framework for Optimized Query Processing Over Heterogeneous Data Sources https://dl.acm.org/doi/abs/10.1145/3183713.3190662 | 翻译 https://xie.infoq.cn/article/f6f2fee3aa86cd3882cf5240a
- Apache Calcite 学习 (一) https://www.cnblogs.com/wcgstudy/p/11795886.html
Calcite的主要功能我们上面其实已经提到了,主要有以下功能:
- SQL解析:通过JavaCC将SQL解析成未经校验的AST语法树
- SQL校验:校验分两部分,一种为无状态的校验,即验证SQL语句是否符合规范;一种为有状态的即通过与元数据结合验证SQL中的Schema、Field、Function是否存在。
- SQL查询优化:对上个步骤的输出(RelNode)进行优化,得到优化后的物理执行计划
- SQL生成:将物理执行计划生成为在特定平台/引擎的可执行程序,如生成符合Mysql or Oracle等不同平台规则的SQL查询语句等
- 数据连接与执行:通过各个执行平台执行查询,得到输出结果。
// 初始化配置
SqlParser.ConfigBuilder configBuilder = SqlParser.configBuilder();
configBuilder.setUnquotedCasing(Casing.UNCHANGED);
//Sql解析:解析Sql语句,通过JavaCC解析成AST语法树,表现为SqlNode
SqlParser sqlParser = SqlParser.create(sql, configBuilder.build());
SqlNode sqlNode = sqlParser.parseQuery();
//Sql校验:结合元数据信息验证Sql是否符合规范
Planner planner = Frameworks.getPlanner(config);
SqlNode node = planner.validate(sqlNode);
//Sql查询优化:将SqlNode转换为LogicalPlan,表现为RelNode
RelRoot relRoot = planner.rel(node);
RelNode project = relRoot.project();
//指定优化规则
final HepProgram program = new HepProgramBuilder()
.addRuleInstance(SubQueryRemoveRule.PROJECT)
.addRuleInstance(SubQueryRemoveRule.FILTER)
.addRuleInstance(SubQueryRemoveRule.JOIN)
.build();
//生成优化后的RelNode
HepPlanner prePlanner = new HepPlanner(program);
prePlanner.setRoot(project);
RelNode relNode = prePlanner.findBestExp();
//ToDo 执行查询
//创建Calcite Connection对象
Class.forName("org.apache.calcite.jdbc.Driver");
Properties info = new Properties();
info.setProperty("lex", "JAVA");
Connection connection =
DriverManager.getConnection("jdbc:calcite:", info);
CalciteConnection calciteConnection =
connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();
//创建Mysql的数据源schema
Class.forName("com.mysql.jdbc.Driver");
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl("jdbc:mysql://localhost");
dataSource.setUsername("username");
dataSource.setPassword("password");
Schema schema = JdbcSchema.create(rootSchema, "hr", dataSource,
null, "name");
rootSchema.add("hr", schema);
//执行查询
Statement statement = calciteConnection.createStatement();
ResultSet resultSet = statement.executeQuery(
"select d.deptno, min(e.empid)\n"
+ "from hr.emps as e\n"
+ "join hr.depts as d\n"
+ " on e.deptno = d.deptno\n"
+ "group by d.deptno\n"
+ "having count(*) > 1");
print(resultSet);
resultSet.close();
statement.close();
connection.close();
Calcite提供了多种方式添加数据源,如通过“inline:”的字符串方式以及通过json或yaml文件的方式。同时,Calcite抽象出了功能齐全的接口,可以方便的将CSV文件抽象成数据表进行查询。这部分内容可以通过官方的示例了解一下!