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

希望对对家有帮助

相关推荐
冷琴199614 分钟前
基于java+springboot的酒店预定网站、酒店客房管理系统
java·开发语言·spring boot
九圣残炎37 分钟前
【springboot】简易模块化开发项目整合Redis
spring boot·redis·后端
.生产的驴1 小时前
Electron Vue框架环境搭建 Vue3环境搭建
java·前端·vue.js·spring boot·后端·electron·ecmascript
爱学的小涛1 小时前
【NIO基础】基于 NIO 中的组件实现对文件的操作(文件编程),FileChannel 详解
java·开发语言·笔记·后端·nio
爱学的小涛1 小时前
【NIO基础】NIO(非阻塞 I/O)和 IO(传统 I/O)的区别,以及 NIO 的三大组件详解
java·开发语言·笔记·后端·nio
北极无雪1 小时前
Spring源码学习:SpringMVC(4)DispatcherServlet请求入口分析
java·开发语言·后端·学习·spring
琴智冰1 小时前
SpringBoot
java·数据库·spring boot
爱码少年1 小时前
springboot工程中使用tcp协议
spring boot·后端·tcp/ip
《源码好优多》2 小时前
基于SpringBoot+Vue+Uniapp的植物园管理小程序系统(2024最新,源码+文档+远程部署+讲解视频等)
vue.js·spring boot·uni-app
计算机学姐2 小时前
基于微信小程序的调查问卷管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis