|
@@ -20,74 +20,60 @@ import java.sql.DriverManager;
|
|
|
import java.sql.ResultSet;
|
|
|
import java.sql.Statement;
|
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
|
|
|
public class JsonToCalciteExample {
|
|
|
|
|
|
- public static SchemaPlus jsonTotable(String aa) throws Exception {
|
|
|
+ public static SchemaPlus jsonTotable(String schemaName, String jsonDate) {
|
|
|
// 1. JSON 数据
|
|
|
- String jsonData = """
|
|
|
- {
|
|
|
- "orders": [
|
|
|
- { "order_id": 1, "user_id": 101, "amount": 250.0 },
|
|
|
- { "order_id": 2, "user_id": 102, "amount": 300.0 }
|
|
|
- ],
|
|
|
- "users": [
|
|
|
- { "user_id": 101, "name": "Alice" },
|
|
|
- { "user_id": 102, "name": "Bob" }
|
|
|
- ]
|
|
|
- }
|
|
|
- """;
|
|
|
|
|
|
- // 2. 使用 Jackson 解析 JSON 数据
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
- JsonNode rootNode = objectMapper.readTree(aa);
|
|
|
-
|
|
|
- // 3. 创建 Calcite Schema
|
|
|
- Connection connection = DriverManager.getConnection("jdbc:calcite:");
|
|
|
- CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
|
|
|
-
|
|
|
- SchemaPlus rootSchema = calciteConnection.getRootSchema();
|
|
|
- rootSchema.add("credite",new AbstractSchema());
|
|
|
-
|
|
|
- // 注册 JSON 数据为表
|
|
|
- rootNode.fields().forEachRemaining(entry -> {
|
|
|
- String tableName = entry.getKey();
|
|
|
- JsonNode tableData = entry.getValue();
|
|
|
- rootSchema.add(tableName, new JsonScannableTable(Sources.of(tableData.toString())));
|
|
|
- });
|
|
|
-
|
|
|
- // 4. 执行 SQL 查询
|
|
|
- Statement statement = connection.createStatement();
|
|
|
- String sql = """
|
|
|
- SELECT "input"."method"
|
|
|
- FROM "input"
|
|
|
+ try{
|
|
|
+ // 2. 使用 Jackson 解析 JSON 数据
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ JsonNode rootNode = objectMapper.readTree(jsonDate);
|
|
|
+
|
|
|
+ // 3. 创建 Calcite Schema
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:calcite:");
|
|
|
+ CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
|
|
|
+
|
|
|
+ SchemaPlus rootSchema = calciteConnection.getRootSchema();
|
|
|
+
|
|
|
+ SchemaPlus schema = rootSchema.add(schemaName,new AbstractSchema());
|
|
|
+
|
|
|
+ // 注册 JSON 数据为表
|
|
|
+ rootNode.fields().forEachRemaining(entry -> {
|
|
|
+ String tableName = entry.getKey();
|
|
|
+ JsonNode tableData = entry.getValue();
|
|
|
+ schema.add(tableName, new JsonScannableTable(Sources.of(tableData.toString())));
|
|
|
+ });
|
|
|
+ // 4. 执行 SQL 查询
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ String sql = """
|
|
|
+ SELECT "test"."input"."method" FROM "test"."input"
|
|
|
""";
|
|
|
|
|
|
- ResultSet resultSet = statement.executeQuery(sql);
|
|
|
-
|
|
|
- // 打印查询结果
|
|
|
- System.out.println("Query Results:");
|
|
|
- while (resultSet.next()) {
|
|
|
- /*System.out.printf("Order ID: %d, Amount: %.2f, Name: %s%n",
|
|
|
+ ResultSet resultSet = statement.executeQuery(sql);
|
|
|
+ while (resultSet.next()){
|
|
|
+ String method=resultSet.getString("method");
|
|
|
+ System.out.println(method);
|
|
|
+ }
|
|
|
+ // 打印查询结果
|
|
|
+ /*while (resultSet.next()) {
|
|
|
+ System.out.printf("Order ID: %d, Amount: %.2f, Name: %s%n",
|
|
|
resultSet.getInt("user_id"),
|
|
|
resultSet.getDouble("amount"),
|
|
|
- resultSet.getString("name"))*/;
|
|
|
- System.out.println(resultSet.getString("method"));
|
|
|
+ resultSet.getString("name"));
|
|
|
+
|
|
|
+ }*/
|
|
|
|
|
|
+ // 关闭连接
|
|
|
+ resultSet.close();
|
|
|
+ statement.close();
|
|
|
+ connection.close();
|
|
|
+ return rootSchema;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
|
|
|
- // 关闭连接
|
|
|
- resultSet.close();
|
|
|
- statement.close();
|
|
|
- connection.close();
|
|
|
- //解析完成后开启新线程 保存数据至数据库
|
|
|
- ExecutorService executor= Executors.newSingleThreadExecutor();
|
|
|
- executor.execute(()-> System.out.println("ExecutorService is runing..."));
|
|
|
- //保存至数据库
|
|
|
- executor.shutdown();//关闭线程池
|
|
|
- return rootSchema;
|
|
|
}
|
|
|
|
|
|
// 自定义表类实现
|