配置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>