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地址

calcite-mysql

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想使用 CalciteMySQL-Foodmart-Model.json 模型文件来查询 MySQL 数据库中的 foodmart 数据库,可以按照以下步骤操作: 1. 下载 MySQL-Foodmart-Model.json 模型文件,并将它保存到某个目录下。 2. 安装 Calcite JDBC 驱动程序。您可以从 Calcite 官方网站下载最新版本的 Calcite JDBC 驱动程序。 3. 创建一个 Java 项目,并添加 Calcite JDBC 驱动程序和 MySQL JDBC 驱动程序的依赖。 4. 在项目中创建一个名为 "calcite.properties" 的文件,并将以下内容添加到该文件中: ```properties model = { "version": "1.0", "defaultSchema": "foodmart", "schemas": [ { "type": "custom", "name": "foodmart", "factory": "org.apache.calcite.adapter.jdbc.JdbcSchema$Factory", "operand": { "jdbcDriver": "com.mysql.jdbc.Driver", "jdbcUrl": "jdbc:mysql://localhost:3306/foodmart", "jdbcUser": "root", "jdbcPassword": "password" } } ] } ``` 这个文件告诉 Calcite 如何连接MySQL 数据库,并使用 MySQL-Foodmart-Model.json 模型文件来查询 foodmart 数据库。 5. 在 Java 代码中创建一个 Calcite Connection,并使用该连接来执行 SQL 查询。例如: ```java Properties info = new Properties(); info.setProperty("model", "path/to/MySQL-Foodmart-Model.json"); Connection connection = DriverManager.getConnection("jdbc:calcite:", info); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM sales_fact_1997"); // 处理查询结果 ``` 这个代码片段会创建一个 Calcite 连接,并使用 MySQL-Foodmart-Model.json 模型文件来查询 foodmart 数据库中的 sales_fact_1997 表。 以上就是使用 CalciteMySQL-Foodmart-Model.json 模型文件来查询 MySQL 数据库中的 foodmart 数据库的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值