calcite入门02-连接多数据源

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: calcite入门02-连接多数据源

calcite入门02-连接多数据源

calcite可以连接多个数据源,多个库,并且将查询结果在内存中使用Linq进行计算合并。那么在01的基础上,我们尝试再新建一个库并且在一个SQL中查两个库的表。

SQL

在01的基础上,增量执行以下SQL

create schema test2;
use test2;
create table student2
(
    pk_id varchar(32)  not null
        primary key,
    name  varchar(128) null
);
insert into test2.student2 (pk_id, name)
values ('2', 'lu');
insert into test2.student2 (pk_id, name)
values ('1', 'wang');
commit;

那么正常SQL,肯定不支持test.student1与tese2.student2进行连表查询的。 那么如何支持以下SQL呢?

select *
from test.student
         left join test2.student2
                   on test.student.pk_id = test2.student2.pk_id

calcite demo代码

/**
 * test connect mysql by calcite
 *
 * to run this test normally please run init2.sql on your mysql server first
 *
 * schema: test & test2
 * table: test.student & test2.student2
 */
public class Test02 {
    // two schema query for mysql
    public static void main(String[] args) throws Exception {
        // check driver exist
        Class.forName("org.apache.calcite.jdbc.Driver");
        Class.forName("com.mysql.jdbc.Driver");
        // the properties for calcite connection
        Properties info = new Properties();
        info.setProperty("lex", "JAVA");
        info.setProperty("remarks","true");
        // SqlParserImpl can analysis sql dialect for sql parse
        info.setProperty("parserFactory","org.apache.calcite.sql.parser.impl.SqlParserImpl#FACTORY");
        // create calcite connection and schema
        Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
        CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
        System.out.println(calciteConnection.getProperties());
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        // code for mysql datasource
        MysqlDataSource dataSource = new MysqlDataSource();
        // please change host and port maybe like "jdbc:mysql://127.0.0.1:3306/test"
        dataSource.setUrl("jdbc:mysql://aaa.club/test");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        // mysql schema, the sub schema for rootSchema, "test" is a schema in mysql
        Schema schema = JdbcSchema.create(rootSchema, "test", dataSource, null, "test");
        rootSchema.add("test", schema);
        // code for mysql datasource2
        MysqlDataSource dataSource2 = new MysqlDataSource();
        dataSource2.setUrl("jdbc:mysql://aaa.club/test2");
        dataSource2.setUser("root");
        dataSource2.setPassword("123456");
        // mysql schema, the sub schema for rootSchema, "test2" is a schema in mysql
        Schema schema2 = JdbcSchema.create(rootSchema, "test2", dataSource2, null, "test2");
        rootSchema.add("test2", schema2);
        // run sql query
        Statement statement = calciteConnection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from test.student " +
                "left join test2.student2 on test.student.pk_id = test2.student2.pk_id");
        while (resultSet.next()) {
            System.out.println(resultSet.getObject(1) + "__" + resultSet.getObject(2));
        }
        statement.close();
        connection.close();
    }
}

与01相比,我们基本就是添加了一份与schema基本相同的schema2,仅仅库名不同~

那么暂且不去关心calcite如何进行合并的,上述代码就可以做到将SQL进行跨库连接查询了~

github地址

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
8月前
|
SQL 消息中间件 存储
Flink SQL 核心概念剖析与编程案例实战
本文使用了 Docker 镜像快速安装一些基础组件,zk 和 kafka,并通过案例的方式,剖析了 SQL 的概念与详细的使用方式
|
10月前
|
关系型数据库 MySQL Java
Flink的DataSource三部曲之三:自定义
实战多种自定义flink数据源
Flink的DataSource三部曲之三:自定义
|
10月前
|
消息中间件 JSON Java
Flink的DataSource三部曲之二:内置connector
来体验Flink内置connector提供的source能力
115 1
Flink的DataSource三部曲之二:内置connector
|
12月前
|
SQL Oracle 安全
如何使用JDBC操作数据库?一文带你吃透JDBC规范(一)
如何使用JDBC操作数据库?一文带你吃透JDBC规范(一)
238 1
|
12月前
|
SQL druid Java
如何使用JDBC操作数据库?一文带你吃透JDBC规范(二)
如何使用JDBC操作数据库?一文带你吃透JDBC规范(二)
85 0
EMQ
|
12月前
|
SQL 存储 物联网
eKuiper 源码解读:从一条 SQL 到流处理任务的旅程
在本篇文章中,我们以梳理关键代码节点的方式了解了 eKuiper 的 SQL 计算引擎中是如何解析、处理,并最终执行这条 SQL 得到相应的结果。对于整个计算引擎关键处理节点里,我们了解了每个环节的代码大致是如何运行的。
EMQ
120 0
eKuiper 源码解读:从一条 SQL 到流处理任务的旅程
|
SQL 存储 自然语言处理
看这篇就够了丨基于Calcite框架的SQL语法扩展探索
Calcite在大数据系统中有着广泛的运用, 比如Apache Flink, Apache Drill等都大量使用了Calcite,理解Calcite的原理可以说已经成为理解大数据系统中SQL访问层实现原理的必备条件之一。 本文就为大家详细介绍如何基于Calcite框架的SQL语法扩展探索使之更符合你的业务需求,以及扩展SQL在数栈产品的应用实践。对该技术感兴趣的同学点进文章阅读哦
1085 0
|
SQL 存储 算法
【ShardingSphere技术专题】「ShardingJDBC」(2) 进阶领略一下数据分片的核心概念
【ShardingSphere技术专题】「ShardingJDBC」(2) 进阶领略一下数据分片的核心概念
102 0
|
关系型数据库 Java MySQL