序言
咱们在 Apache Calcite 动态数据治理框架介绍 介绍了 calcite 的基本功能,本文一起来看一下如何实现一个 csv 的 sql 查问。
入门例子
依赖
<?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>org.example</groupId>
<artifactId>calcite-learn</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>calcite-learn-basic</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<calcite.version>1.20.0</calcite.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-core</artifactId>
<version>${calcite.version}</version>
</dependency>
<dependency>
<groupId>org.apache.calcite</groupId>
<artifactId>calcite-example-csv</artifactId>
<version>${calcite.version}</version>
</dependency>
<!-- Add other dependencies, e.g., database driver -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
测试 csv
创立文件夹:
D:\github\calcite-learn\calcite-learn-basic\src\main\resources\csv
上面防对应的测试 csv 文件:
- depts.csv
EMPNO:long,NAME:string,DEPTNO:int,GENDER:string,CITY:string,EMPID:int,AGE:int,SLACKER:boolean,MANAGER:boolean,JOINEDAT:date
100,"Fred",10,,,30,25,true,false,"1996-08-03"
110,"Eric",20,"M","San Francisco",3,80,,false,"2001-01-01"
110,"John",40,"M","Vancouver",2,,false,true,"2002-05-03"
120,"Wilma",20,"F",,1,5,,true,"2005-09-07"
130,"Alice",40,"F","Vancouver",2,,false,true,"2007-01-01"
测试类
package com.github.houbb.calcite.learn.basic;
import org.apache.calcite.adapter.csv.CsvSchema;
import org.apache.calcite.adapter.csv.CsvTable;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.schema.SchemaPlus;
import java.io.File;
import java.sql.*;
import java.util.Properties;
public class CsvDemo {public static void main(String[] args) throws Exception {
// 0. 获取 csv 文件的门路,留神获取到文件所在下层门路就能够了
String path = "D:\\github\\calcite-learn\\calcite-learn-basic\\src\\main\\resources\\csv\\";
// 1. 构建 CsvSchema 对象,在 Calcite 中,不同数据源对应不同 Schema,比方 CsvSchema、DruidSchema、ElasticsearchSchema 等
CsvSchema csvSchema = new CsvSchema(new File(path), CsvTable.Flavor.SCANNABLE);
// 2. 构建 Connection
// 2.1 设置连贯参数
Properties info = new Properties();
// 不辨别 sql 大小写
info.setProperty("caseSensitive", "false");
// 2.2 获取规范的 JDBC Connection
Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
// 2.3 获取 Calcite 封装的 Connection
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
// 3. 构建 RootSchema,在 Calcite 中,RootSchema 是所有数据源 schema 的 parent,多个不同数据源 schema 能够挂在同一个 RootSchema 下
// 以实现查问不同数据源的目标
SchemaPlus rootSchema = calciteConnection.getRootSchema();
// 4. 将不同数据源 schema 挂载到 RootSchema,这里增加 CsvSchema
rootSchema.add("csv", csvSchema);
// 5. 执行 SQL 查问,通过 SQL 形式拜访 csv 文件
String sql = "select * from csv.depts";
Statement statement = calciteConnection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
// 6. 遍历打印查问后果集
printResultSet(resultSet);
}
public static void printResultSet(ResultSet resultSet) throws SQLException {
// 获取 ResultSet 元数据
ResultSetMetaData metaData = resultSet.getMetaData();
// 获取列数
int columnCount = metaData.getColumnCount();
System.out.println("Number of columns:" + columnCount);
// 遍历 ResultSet 并打印后果
while (resultSet.next()) {
// 遍历每一列并打印
for (int i = 1; i <= columnCount; i++) {String columnName = metaData.getColumnName(i);
String columnValue = resultSet.getString(i);
System.out.println(columnName + ":" + columnValue);
}
System.out.println(); // 换行}
}
}
测试成果
Number of columns: 10
EMPNO: 100
NAME: Fred
DEPTNO: 10
GENDER:
CITY:
EMPID: 30
AGE: 25
SLACKER: true
MANAGER: false
JOINEDAT: 1996-08-03
EMPNO: 110
NAME: Eric
DEPTNO: 20
GENDER: M
CITY: San Francisco
EMPID: 3
AGE: 80
SLACKER: null
MANAGER: false
JOINEDAT: 2001-01-01
EMPNO: 110
NAME: John
DEPTNO: 40
GENDER: M
CITY: Vancouver
EMPID: 2
AGE: null
SLACKER: false
MANAGER: true
JOINEDAT: 2002-05-03
EMPNO: 120
NAME: Wilma
DEPTNO: 20
GENDER: F
CITY:
EMPID: 1
AGE: 5
SLACKER: null
MANAGER: true
JOINEDAT: 2005-09-07
EMPNO: 130
NAME: Alice
DEPTNO: 40
GENDER: F
CITY: Vancouver
EMPID: 2
AGE: null
SLACKER: false
MANAGER: true
JOINEDAT: 2007-01-01
参考资料
Apache Calcite 疾速入门指南
Apache Calcite 精简入门与学习领导
本文由博客一文多发平台 OpenWrite 公布!