一、服务端
public static void main(String[] args) throws Exception{
HttpServer server = start(8765);
server.join();
}
public static HttpServer start(int port) 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/calcitetest/src/main/resources/DEPTS.csv',\n"
+ " flavor: 'scannable'\n"
+ " }\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "}");
Meta meta = new JdbcMeta("jdbc:calcite:", info);
Service service = new LocalService(meta);
HttpServer server = new HttpServer(port,new AvaticaJsonHandler(service));
server.start();
System.out.println("server start...");
return server;
}
二、客户端
public static void main(String[] args) throws Exception{
Class.forName("org.apache.calcite.avatica.remote.Driver");
Connection conn = DriverManager.getConnection("jdbc:avatica:remote:url=http://localhost:8765","username","password");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * from DEPTS");
while(rs.next()){
System.out.println(rs.getString(1));
}
rs.close();
statement.close();
conn.close();
}
三、服务执行
开启服务端,执行客户端,得到输出
10
20
30
四、项目依赖
org.apache.calcite:calcite-core:1.19.0
org.apache.calcite:calcite-example-csv:1.19.0
org.apache.calcite.avatica:avatica-server:1.13.0
com.alibaba:fastjson:1.2.54
com.google.guava:guava:16.0.1