简体   繁体   中英

How to catch exceptions in sql loader

I'm using sql loader to load large amount of data from csv into oracle database. I write a java program running sql loader with restful. Here is the program

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

I use the postman to test the API: /getCsv. However, if the upload is successful or not it still returns HTTP Status 200. Sometimes status is 200 but data from csv has not been imported into the database. How do I know if the csv data has been imported into the database? I have read that the sql loader has an option of log =. However, I would like if uploading the data from csv to the database, there will be a message displayed to the user and if not successful there is also such a message.I want catch event from sql loader. How do I know if my data has uploaded successfully or error? Please help. Thank you.

Generally, the best way is to use Oracle direct load API, but as far as I know it isn't available for Java.

When using SQL*Loader, you can parse log file to determine load result. For example, when loading if successful, log would look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM