|
@@ -1,5 +1,6 @@
|
|
|
package com.sundata.internalevaluation.conf;
|
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.sundata.internalevaluation.calc.calcUnit.IndexCalcUnit;
|
|
@@ -18,83 +19,52 @@ import org.apache.calcite.sql.type.SqlTypeName;
|
|
|
import org.apache.calcite.util.Pair;
|
|
|
import org.apache.calcite.util.Sources;
|
|
|
|
|
|
-import java.sql.Connection;
|
|
|
-import java.sql.DriverManager;
|
|
|
+import java.sql.*;
|
|
|
import java.util.*;
|
|
|
|
|
|
public class JsonToCalciteExample {
|
|
|
|
|
|
- public Object jsonTotable(String schemaName, String jsonDate, String sql) {
|
|
|
+ public Object jsonTotable(String jsonDate, String sql) {
|
|
|
|
|
|
Object resValue = null;
|
|
|
try{
|
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
JsonNode rootNode = objectMapper.readTree(jsonDate);
|
|
|
-
|
|
|
|
|
|
Connection connection = DriverManager.getConnection("jdbc:calcite:");
|
|
|
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
|
|
|
-
|
|
|
SchemaPlus rootSchema = calciteConnection.getRootSchema();
|
|
|
|
|
|
- SchemaPlus schema = rootSchema.add(schemaName,new AbstractSchema());
|
|
|
+
|
|
|
|
|
|
|
|
|
rootNode.fields().forEachRemaining(entry -> {
|
|
|
- String tableName = entry.getKey();
|
|
|
- JsonNode tableData = entry.getValue();
|
|
|
- schema.add(tableName, new JsonScannableTable(Sources.of(tableData.toString())));
|
|
|
+ String schename =entry.getKey();
|
|
|
+ String schenameData="";
|
|
|
+ JsonNode schenameData1=null;
|
|
|
+ try {
|
|
|
+ schenameData = String.valueOf(entry.getValue());
|
|
|
+ schenameData1 =objectMapper.readTree(schenameData);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ SchemaPlus schema = rootSchema.add(schename,new AbstractSchema());
|
|
|
+ schenameData1.fields().forEachRemaining(entrynext -> {
|
|
|
+ String tableName =entrynext.getKey();
|
|
|
+ JsonNode tableData =entrynext.getValue();
|
|
|
+ schema.add(tableName, new JsonScannableTable(Sources.of(tableData.toString())));
|
|
|
+ });
|
|
|
});
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- System.out.printf("Order ID: %d, Amount: %.2f, Name: %s%n",
|
|
|
- resultSet.getInt("user_id"),
|
|
|
- resultSet.getDouble("amount"),
|
|
|
- resultSet.getString("name"));
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- statement.close();*/
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- IndexCalcUnit model = new IndexCalcUnit(schemaName,"",Map.of(),new IndexConfigModel());
|
|
|
- resValue = model.getConnectionTable(calciteConnection,sql);
|
|
|
+
|
|
|
+
|
|
|
+ resValue = getConnectionTable(calciteConnection,sql);
|
|
|
|
|
|
connection.close();
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
- finally {
|
|
|
- return resValue;
|
|
|
- }
|
|
|
-
|
|
|
+ return resValue;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -140,4 +110,36 @@ public class JsonToCalciteExample {
|
|
|
return Linq4j.asEnumerable(result);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ *
|
|
|
+ * @param calciteConnection
|
|
|
+ * @param sql
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Object getConnectionTable (CalciteConnection calciteConnection,String sql) {
|
|
|
+
|
|
|
+ SchemaPlus rootSchemafter = calciteConnection.getRootSchema();
|
|
|
+
|
|
|
+ SchemaPlus childrenSchema=rootSchemafter.getSubSchema(calciteConnection.getRootSchema().getName());
|
|
|
+ Object childrebnameobj=rootSchemafter.getSubSchemaNames();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Object value = null;
|
|
|
+ try {
|
|
|
+ Statement statement = calciteConnection.createStatement();
|
|
|
+ ResultSet resultSet = statement.executeQuery(sql);
|
|
|
+ if (resultSet.next()){
|
|
|
+ value = resultSet.getObject(1);
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
}
|