CalciteUtil.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.sundata.internalevaluation.calc.util;
  2. import org.apache.calcite.adapter.file.JsonScannableTable;
  3. import org.apache.calcite.jdbc.CalciteConnection;
  4. import org.apache.calcite.schema.SchemaPlus;
  5. import org.apache.calcite.schema.impl.AbstractSchema;
  6. import org.apache.calcite.util.Sources;
  7. import java.sql.*;
  8. /**
  9. * Created by IntelliJ IDEA.
  10. *
  11. * @author JoeLazy
  12. * @date 2025-02-22 15:56:09
  13. * @description: json转虚拟数据库、表进行sql查询工具类
  14. */
  15. public class CalciteUtil {
  16. private Connection connection;
  17. private SchemaPlus rootSchema;
  18. private Statement statement;
  19. private ResultSet resultSet;
  20. private CalciteUtil() {
  21. }
  22. /**
  23. * 获取工具类对象实例
  24. * @return CalciteUtil
  25. * @throws SQLException
  26. */
  27. public static CalciteUtil getInstance() throws SQLException {
  28. CalciteUtil calciteUtil = new CalciteUtil();
  29. calciteUtil.createRootSchema();
  30. return calciteUtil;
  31. }
  32. /**
  33. * 创建跟根schema
  34. * @throws SQLException
  35. */
  36. private void createRootSchema() throws SQLException {
  37. connection = DriverManager.getConnection("jdbc:calcite:");
  38. CalciteConnection calcConnection = connection.unwrap(CalciteConnection.class);
  39. rootSchema = calcConnection.getRootSchema();
  40. }
  41. /**
  42. * 创建 域 (数据库)
  43. * @param dbName 数据库名
  44. * @return 数据库对应的 Schema
  45. */
  46. public SchemaPlus createDatabase(String dbName) {
  47. return rootSchema.add(dbName, new AbstractSchema());
  48. }
  49. /**
  50. * 创建表格
  51. * @param dbSchema 数据库对应的 Schema
  52. * @param tabName 数据表名
  53. * @param json listMap对应json [{a:a1,b:b1},{a:a2,b:b2}...]
  54. * @throws SQLException
  55. */
  56. public void createTable(SchemaPlus dbSchema, String tabName, String json) throws SQLException {
  57. if (dbSchema == null) {
  58. throw new RuntimeException("dbSchema 不能为空 !!");
  59. }
  60. if (rootSchema.getSubSchema(dbSchema.getName()) == null) {
  61. throw new RuntimeException("dbSchema不存在!!");
  62. } else {
  63. dbSchema.add(tabName, new JsonScannableTable(Sources.of(json)));
  64. }
  65. dbSchema.add(tabName, new JsonScannableTable(Sources.of(json)));
  66. }
  67. /**
  68. * 执行sql
  69. * @param sql sql语句
  70. * @return 查询结果(仅返回第一行第一列) 如果无数据返回 null
  71. * @throws SQLException
  72. */
  73. public Object doExecute(String sql) throws SQLException {
  74. if (statement == null) {
  75. statement = connection.createStatement();
  76. }
  77. resultSet = statement.executeQuery(sql);
  78. if (resultSet.next()) {
  79. return resultSet.getObject(1);
  80. }
  81. return null;
  82. }
  83. /**
  84. * 关闭资源
  85. * @throws SQLException
  86. */
  87. public void closeResource() throws SQLException {
  88. if (resultSet != null) {
  89. resultSet.close();
  90. }
  91. if (statement != null) {
  92. statement.close();
  93. }
  94. if (connection != null) {
  95. connection.close();
  96. }
  97. }
  98. public static void main(String[] args) throws SQLException {
  99. String subject = "[\n" +
  100. " {\n" +
  101. " \"subNo\": \"07cc9eca-4e66-4d1c-9dd1-fe4671c2e18b\",\n" +
  102. " \"subName\": \"Associate Degree in Health Science\"\n" +
  103. " },\n" +
  104. " {\n" +
  105. " \"subNo\": \"cda7b717-bbff-40fd-af90-7e22dcea8457\",\n" +
  106. " \"subName\": \"Associate Degree in Medicine\"\n" +
  107. " },\n" +
  108. " {\n" +
  109. " \"subNo\": \"9cf8534b-37c6-4365-8453-d1454e336992\",\n" +
  110. " \"subName\": \"Master of Biological Science\"\n" +
  111. " },\n" +
  112. " {\n" +
  113. " \"subNo\": \"b21f3d9b-670b-4819-8faa-dbb2324f522a\",\n" +
  114. " \"subName\": \"Bachelor of Criminology\"\n" +
  115. " },\n" +
  116. " {\n" +
  117. " \"subNo\": \"635c0a8a-3701-43f1-a1d6-d359465df3c6\",\n" +
  118. " \"subName\": \"Master of Teaching\"\n" +
  119. " },\n" +
  120. " {\n" +
  121. " \"subNo\": \"2aa9e2d1-5f0e-41c6-8bf5-970b08d47b67\",\n" +
  122. " \"subName\": \"Associate Degree in Creative Arts\"\n" +
  123. " }\n" +
  124. " ]";
  125. CalciteUtil instance = CalciteUtil.getInstance();
  126. SchemaPlus db1 = instance.createDatabase("db1");
  127. instance.createTable(db1, "tab1", subject);
  128. String sql = "";
  129. sql = """
  130. select "db1"."tab1"."subName" from "db1"."tab1" where "db1"."tab1"."subNo" = '635c0a8a-3701-43f1-a1d6-d359465df3c6'
  131. """;
  132. // 执行sql
  133. Object o = instance.doExecute(sql);
  134. System.out.println(o);
  135. // 关闭资源
  136. instance.closeResource();
  137. }
  138. }