spring boot 运行mongodb sql

因项目数据放到了Mongodb 中,需在Mongodb 中查询数据,网上找了好久总算找到了实现方式

借用本地命令号读取命令行数据

主要是分2步:

1、下载

复制代码
sql-to-mongo-db-query-converter 

这个是一个3放插件 作用是将sql语句转成 Mongodb shell 语句

代码如下:

public static void main1(String[] args) throws Exception {
        try {
            String sqlQuery = "select sum(id),count(id) from test2 ;";
            ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "D:\\aj-report\\sql-to-mongo-db-query-converter-master\\target\\sql-to-mongo-db-query-converter-1.19-SNAPSHOT-standalone.jar", "-i");
            Process process = processBuilder.start();

            // 读取命令行输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

            // 将 SQL 查询语句写入到进程的标准输入流中
            process.getOutputStream().write(sqlQuery.getBytes());
            process.getOutputStream().close();

            // 打印原始的 SQL 查询语句
            System.out.println("Original SQL Query:");
            System.out.println(sqlQuery);

            // 打印转换后的 MongoDB 查询语句
            System.out.println("\nConverted MongoDB Query:");
            String line;
            StringBuffer lineString=new StringBuffer();
            while ((line = reader.readLine()) != null) {
                System.out.println(line); // 输出转换后的 MongoDB 查询语句
                lineString.append(line);
            }
            System.out.println("::::::"+lineString.toString());

            String searchStr = "******Mongo Query:*********";
            int index = lineString.indexOf(searchStr);

            if (index != -1) {
                String result = lineString.substring(index + searchStr.length());
                System.out.println("截取后sql->mongodb:"+result);
            } else {
                System.out.println("未找到指定的字符串");
            }




            // 打印错误信息
            System.out.println("\nError Output:");
            String errorLine;
            while ((errorLine = errorReader.readLine()) != null) {
                System.out.println(errorLine);
            }


//            System.out.println("::::::"+lineString.toString());


            // 检查命令行执行结果
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                System.err.println("Command execution failed with error code: " + exitCode);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

上述已把sql 转成 Mongodb 可执行语句

2、调用本地Mongodb 命令行

需注意 本地需安装

复制代码
mongosh

代码如下:

public static void main(String[] args) {
        try {


            // 构建命令
//            String[] command = { "mongosh", "--host", "remote_server_ip", "--port", "27017" };
//
//            // 执行命令
//            ProcessBuilder pb = new ProcessBuilder(command);


            ProcessBuilder processBuilder = new ProcessBuilder("D:/mongodb-win32-x86_64-windows-7.0.6/mongosh-2.2.0-win32-x64/bin/mongosh.exe");
            processBuilder.redirectErrorStream(true);
            Process process = processBuilder.start();

            OutputStream stdin = process.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

            // 发送查询命令
            writer.write("use test\n"); // 切换到指定数据库
            writer.write("db.test2.find()\n"); // 执行查询操作
            writer.write("exit\n"); // 退出 MongoDB Shell
            writer.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            int exitCode = process.waitFor();
            System.out.println("MongoDB Shell process exited with code " + exitCode);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

希望对对家有帮助

相关推荐
java—大象1 小时前
基于java+springboot+layui的流浪动物交流信息平台设计实现
java·开发语言·spring boot·layui·课程设计
ApiHug2 小时前
ApiSmart x Qwen2.5-Coder 开源旗舰编程模型媲美 GPT-4o, ApiSmart 实测!
人工智能·spring boot·spring·ai编程·apihug
魔道不误砍柴功2 小时前
探秘Spring Boot中的@Conditional注解
数据库·spring boot·oracle
杨哥带你写代码2 小时前
网上商城系统:Spring Boot框架的实现
java·spring boot·后端
camellias_2 小时前
SpringBoot(二十一)SpringBoot自定义CURL请求类
java·spring boot·后端
背水2 小时前
初识Spring
java·后端·spring
晴天飛 雪2 小时前
Spring Boot MySQL 分库分表
spring boot·后端·mysql
weixin_537590453 小时前
《Spring boot从入门到实战》第七章习题答案
数据库·spring boot·后端
AskHarries3 小时前
Spring Cloud Gateway快速入门Demo
java·后端·spring cloud