因项目数据放到了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();
        }
    }

希望对对家有帮助

03-19 07:15