Calcite-[6]-配置查询mysql

配置calcite查询本地mysql中dbtest_1库下student
参考:tutorial

model json如下

{
  version: '1.0',
  defaultSchema: 'dbtest_1',
  schemas: [
    {
      name: 'dbtest_1',
      type: 'custom',
      factory: 'org.apache.calcite.adapter.jdbc.JdbcSchema$Factory',
      operand: {
        jdbcDriver: 'com.mysql.jdbc.Driver',
        jdbcUrl: 'jdbc:mysql://localhost:3306/dbtest_1',
        jdbcUser: 'xxx',
        jdbcPassword: 'xxx'
      }
    }
  ]
}

测试代码

package com.learn.mysql;

import org.apache.calcite.jdbc.CalciteConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Description:
 * @Date:Create in 下午5:38 2018/9/15
 * @Modified By:
 */
public class QueryMysql {
  public static void main(String[] args) throws SQLException, ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver");
    Properties info = new Properties();
    info.put("model",
      "inline:"
        + "{\n"
        + "  version: '1.0',\n"
        + "  defaultSchema: 'dbtest_1',\n"
        + "  schemas: [\n"
        + "    {\n"
        + "      name: 'dbtest_1',\n"
        + "      type: 'custom',\n"
        + "      factory: 'org.apache.calcite.adapter.jdbc.JdbcSchema$Factory',\n"
        + "      operand: {\n"
        + "        jdbcDriver: 'com.mysql.jdbc.Driver',\n"
        + "        jdbcUrl:'jdbc:mysql://localhost:3306/dbtest_1',\n"
        + "        jdbcUser: 'xxx',\n"
        + "        jdbcPassword: 'xxx'\n"
        + "      }\n"
        + "    }\n"
        + "  ]\n"
        + "}");

    Connection connection =
      DriverManager.getConnection("jdbc:calcite:", info);
    // must print "directory ... not found" to stdout, but not fail
    Statement statement = connection.createStatement();
    CalciteConnection calciteConnection =
      connection.unwrap(CalciteConnection.class);
    ResultSet resultSet =
      statement.executeQuery("select * from \"dbtest_1\".\"student\"");

    ResultSet tables =
      connection.getMetaData().getTables(null, null, null, null);

    final StringBuilder buf = new StringBuilder();
    while (resultSet.next()) {
      int n = resultSet.getMetaData().getColumnCount();
      for (int i = 1; i <= n; i++) {
        buf.append(i > 1 ? "; " : "")
          .append(resultSet.getMetaData().getColumnLabel(i))
          .append("=")
          .append(resultSet.getObject(i));
      }
      System.out.println(buf.toString());
      buf.setLength(0);
    }
    resultSet.close();
    statement.close();
    connection.close();
  }
}

结果

id=1; age=18; name=李四
id=2; age=18; name=张三
id=4; age=12; name=studentA

依赖

 <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.calcite</groupId>
            <artifactId>calcite-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.calcite.avatica</groupId>
                    <artifactId>avatica-core</artifactId>
                </exclusion>
            </exclusions>
            <version>1.12.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.calcite.avatica/avatica -->
        <dependency>
            <groupId>org.apache.calcite.avatica</groupId>
            <artifactId>avatica</artifactId>
            <version>1.9.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>
    </dependencies>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值