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

希望对对家有帮助

相关推荐
喵了个Code12 分钟前
Spring Boot 3 + Spring Security + OAuth2 + Gateway企业级认证授权平台实现
后端
开心猴爷17 分钟前
除了 Perfdog,如何在 Windows 环境中完成 iOS App 的性能测试工作
后端
桦说编程1 小时前
简单方法实现子任务耗时统计
java·后端·监控
BD_Marathon1 小时前
配置文件分类
spring boot
M***Z2101 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback
盖世英雄酱581361 小时前
物品超领取损失1万事故复盘(一)
java·后端
凌览1 小时前
别再死磕 Nginx!http-proxy-middleware 低配置起飞
前端·后端
拾玖不会code1 小时前
简单分表场景下的业务发散思考:分表如何保证丝滑?
后端
CryptoRzz1 小时前
印度尼西亚(IDX)股票数据对接开发
java·后端·websocket·web3·区块链
f***28142 小时前
Springboot中使用Elasticsearch(部署+使用+讲解 最完整)
spring boot·elasticsearch·jenkins