calcite入门01-连接mysql

calcite入门01-连接mysql

apache calcite 可以连接多种数据源,mysql,redis,elasticsearch等等,并且封装了jdbc协议进行查询。另外它也易于扩展,可以查询自定义的数据源。

MAVEN依赖

        <!--calcite核心包-->
        <dependency>
            <groupId>org.apache.calcite</groupId>
            <artifactId>calcite-core</artifactId>
            <version>1.22.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.79</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>24.1-jre</version>
        </dependency>

        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.26</version>
        </dependency>

准备数据源

首先准备一个可以运行的Mysql服务器,其次,创建schema以及table,当然,也插入一些数据,这边准备了init.sql

create schema test;

use test;
create table student
(
    pk_id varchar(32)  not null
        primary key,
    name  varchar(128) null
);

insert into test.student (pk_id, name)
values  ('1', 'wang');

commit;

DEMO代码

/**
 * test connect mysql by calcite
 *
 * to run this test normally please run init.sql on your mysql server first
 *
 * schema: test
 * table: test.student
 */
public class Test01 {

    // single schema query
    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);

        // run sql query
        Statement statement = calciteConnection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from test.student");
        while (resultSet.next()) {
            System.out.println(resultSet.getObject(1) + "__" + resultSet.getObject(2));
        }

        statement.close();
        connection.close();
    }
}

  • 保证驱动包存在
  • 创建calcite的连接对象connection以及rootSchema
  • 创建mysql的datasource
  • 创建mysql的schema
  • 将mysql的schema添加到rootSchema中
  • 执行SQL
  • 打印SQL结果

github地址

calcite-mysql

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值