如何在sql loader中捕获异常

时间:2018-02-01 12:10:09

标签: java oracle rest csv

我使用sql loader将大量数据从csv加载到oracle数据库中。我用restful编写了一个运行sql loader的java程序。这是程序

FileUploadController

       @PostMapping("/getCsv")
        public ResponseEntity<?> getAll() {
           try {
               String path = context.getRealPath("/WEB-INF/uploaded");
               String fileName = "sqlldr_Employees.ctl ";

                String sqlldrCmd = "Sqlldr trung/tbtrung control = "+ path + "/" + fileName +"log=d:/bt.log skip=1" + " data=" + path + "/*.csv";
                System.out.println(sqlldrCmd.replace("\\", "/"));
                System.out.println("SQLLDR Started ....... ");
                Runtime rt = Runtime.getRuntime();
                Process proc = rt.exec(sqlldrCmd.replace("\\", "/"));
                System.out.println("SQLLDR Ended ........  ");
                return ResponseEntity.status(HttpStatus.OK).build();
              } catch (Exception e) {
                  return ResponseEntity.notFound().build();
            }
        }

我使用邮递员来测试API:/ getCsv。但是,如果上载成功与否,它仍会返回HTTP状态200.有时状态为200,但csv中的数据尚未导入数据库。我如何知道csv数据是否已导入数据库?我已经读过sql loader有一个log =选项。但是,我想如果将数据从csv上传到数据库,将会向用户显示一条消息,如果不成功,也会有这样的消息。我想从sql loader中获取catch事件。我如何知道我的数据是成功上传还是错误?请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

通常,最好的方法是使用Oracle直接加载API,但据我所知它不适用于Java。

使用SQL * Loader时,您可以解析日志文件以确定加载结果。例如,如果成功加载,log将如下所示:

Table T$LOADER:
  3 Rows successfully loaded.
  0 Rows not loaded due to data errors.
  0 Rows not loaded because all WHEN clauses were failed.
  0 Rows not loaded because all fields were null.


Space allocated for bind array:                  49536 bytes(64 rows)
Read   buffer bytes: 1048576

Total logical records skipped:          0
Total logical records read:             3
Total logical records rejected:         0
Total logical records discarded:        0

Run began on Tue Dec 19 10:54:48 2017
Run ended on Tue Dec 19 10:54:48 2017

Elapsed time was:     00:00:00.28
CPU time was:         00:00:00.07