Merge pull request #3767 from taosdata/feature/TD-1476
[TD-1476]<feature>: a demo for calcite
This commit is contained in:
commit
dcda284fdc
|
@ -0,0 +1,53 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.taosdata.example.calcite</groupId>
|
||||||
|
<artifactId>calciteDemo</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- slf4j -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-simple</artifactId>
|
||||||
|
<version>1.7.25</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- calcite -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.calcite</groupId>
|
||||||
|
<artifactId>calcite-core</artifactId>
|
||||||
|
<version>1.23.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-dbcp2</artifactId>
|
||||||
|
<version>2.7.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.calcite.avatica</groupId>
|
||||||
|
<artifactId>avatica-core</artifactId>
|
||||||
|
<version>1.17.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- mysql -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>5.1.47</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- tdengine -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.taosdata.jdbc</groupId>
|
||||||
|
<artifactId>taos-jdbcdriver</artifactId>
|
||||||
|
<version>2.0.7</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,67 @@
|
||||||
|
package com.taosdata.example.calcite;
|
||||||
|
|
||||||
|
import org.apache.calcite.adapter.jdbc.JdbcSchema;
|
||||||
|
import org.apache.calcite.jdbc.CalciteConnection;
|
||||||
|
import org.apache.calcite.schema.Schema;
|
||||||
|
import org.apache.calcite.schema.SchemaPlus;
|
||||||
|
import org.apache.calcite.sql.parser.SqlParseException;
|
||||||
|
import org.apache.commons.dbcp2.BasicDataSource;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class CalciteDemo {
|
||||||
|
|
||||||
|
private static String url_taos = "jdbc:TAOS://192.168.236.135:6030/test";
|
||||||
|
private static String url_mysql = "jdbc:mysql://master:3306/test?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
|
||||||
|
|
||||||
|
public static void main(String[] args) throws SqlParseException, ClassNotFoundException, SQLException {
|
||||||
|
Class.forName("org.apache.calcite.jdbc.Driver");
|
||||||
|
Properties info = new Properties();
|
||||||
|
info.setProperty("caseSensitive", "false");
|
||||||
|
|
||||||
|
Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
|
||||||
|
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
|
||||||
|
|
||||||
|
SchemaPlus rootSchema = calciteConnection.getRootSchema();
|
||||||
|
|
||||||
|
//这里hdb是在tdengine中创建的数据库名
|
||||||
|
Schema schema = mysqlTest(rootSchema);
|
||||||
|
// Schema schema = tdengineTest(rootSchema);
|
||||||
|
|
||||||
|
//创建新的schema自动映射到原来的hdb数据库
|
||||||
|
rootSchema.add("test", schema);
|
||||||
|
|
||||||
|
Statement stmt = calciteConnection.createStatement();
|
||||||
|
//查询schema test中的表,表名是tdengine中的表
|
||||||
|
ResultSet rs = stmt.executeQuery("select * from test.t");
|
||||||
|
ResultSetMetaData metaData = rs.getMetaData();
|
||||||
|
while (rs.next()) {
|
||||||
|
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||||
|
System.out.println(metaData.getColumnLabel(i) + " : " + rs.getString(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static Schema tdengineTest(SchemaPlus rootSchema) throws ClassNotFoundException {
|
||||||
|
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||||
|
BasicDataSource dataSource = new BasicDataSource();
|
||||||
|
dataSource.setUrl(url_taos);
|
||||||
|
dataSource.setUsername("root");
|
||||||
|
dataSource.setPassword("taosdata");
|
||||||
|
|
||||||
|
return JdbcSchema.create(rootSchema, "test", dataSource, "hdb", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Schema mysqlTest(SchemaPlus rootSchema) throws ClassNotFoundException {
|
||||||
|
Class.forName("com.mysql.jdbc.Driver");
|
||||||
|
BasicDataSource dataSource = new BasicDataSource();
|
||||||
|
dataSource.setUrl(url_mysql);
|
||||||
|
dataSource.setUsername("root");
|
||||||
|
dataSource.setPassword("123456");
|
||||||
|
|
||||||
|
//Schema schema = JdbcSchema.create(rootSchema, "test", dataSource, "hdb", null);
|
||||||
|
return JdbcSchema.create(rootSchema, "test", dataSource, "test", null);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
log4j.rootLogger=info,stdout
|
||||||
|
|
||||||
|
#console
|
||||||
|
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||||
|
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||||
|
log4j.appender.stdout.layout.ConversionPattern= [%d{yyyy-MM-dd HH:mm:ss a}]:%p %l%m%n
|
Loading…
Reference in New Issue