一、利用calcite项目使用sql读取csv文件
进入calcite的example/csv module,在该module下新建main方法的函数,代码如下
public static void main(String[] args) throws Exception{
Properties info = new Properties();
info.put("model",
"inline:"
+ "{\n"
+ " version: '1.0',\n"
+ " defaultSchema: 'SALES',\n"
+ " schemas: [\n"
+ " {\n"
+ " name: 'SALES',\n"
+ " tables: [\n"
+ " {\n"
+ " name: 'DEPTS',\n"
+ " type: 'custom',\n"
+ " factory: 'org.apache.calcite.adapter.csv.CsvTableFactory',\n"
+ " operand: {\n"
+ " file: '/Users/xxx/project/calcite/example/csv/src/test/resources/sales/DEPTS.csv',\n"
+ " flavor: 'scannable'\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "}");
Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from DEPTS");
while (rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
statement.close();
connection.close();
}
其中file是自己本地文件路径,通过这种方式可以使用select方法访问DEPTS.csv文件,执行项目输出
10
20
30
至此,我们完成了calcite项目的基本使用方式