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();
        }
    }

希望对对家有帮助

相关推荐
秋难降1 分钟前
SQL 索引突然 “罢工”?快来看看为什么
数据库·后端·sql
上官浩仁1 小时前
springboot ioc 控制反转入门与实战
java·spring boot·spring
Access开发易登软件1 小时前
Access开发导出PDF的N种姿势,你get了吗?
后端·低代码·pdf·excel·vba·access·access开发
叫我阿柒啊1 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
中国胖子风清扬2 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
bobz9652 小时前
分析 docker.service 和 docker.socket 这两个服务各自的作用
后端
野犬寒鸦2 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode
phiilo3 小时前
golang 设置进程退出时kill所有子进程
后端
花花无缺3 小时前
python自动化-pytest-用例发现规则和要求
后端·python
程序员小假3 小时前
我们来说一说 Cglib 与 JDK 动态代理
后端