UNIVOCITY-PARSERS to stop the csv object to bean as soon as an error occurs

I use UNIVOCITY-PARSERS to convert strings in CSV files to Java objects.

during file processing, if it encounters any problem with any of the columns in a row, then it analyzes the stop process on that row and throws an exception. But I need something that will continue until the end of the file, just skipping the error line. But I did not have any utility classes in api.

MY Bean Class

public class ItemcodeBean {

@Trim
@NullString(nulls = { " ", "" }) 
@Parsed(field = "ItemCode")
 private String itemCode;

@Trim 
@NullString(nulls = { " ", "" })
@Parsed(field = "PartNumber") 
private String partNumber;

@Trim 
@NullString(nulls = { " ", "" }) 
@Parsed(field = "ModelNumber") 
private String modelNumber;

}

My main class

public class TestClass {

private  BeanListProcessor<ItemcodeBean>
        rowProcessor = null;
private CsvParser parser = null;
public static void main(String[] args) {
    TestClass testClass = new TestClass();
    testClass.init();
    try{
        ItemcodeBean itemcodeBean;
        while ((itemcodeBean = testClass.getRowData()) != null){
            System.out.println(itemcodeBean.toString());
        }
    }catch (Throwable ex){
        System.out.println(ex.getLocalizedMessage());
    }

}

private BeanListProcessor<ItemcodeBean> init() {
    // BeanListProcessor converts each parsed row to an instance of a given class, then stores each instance into a list.
          this.rowProcessor =
            new BeanListProcessor<ItemcodeBean>(ItemcodeBean.class);

    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.setProcessor(rowProcessor);
    parserSettings.setHeaderExtractionEnabled(true);
    // skip leading whitespaces
    parserSettings.setIgnoreLeadingWhitespaces(true);

    //skip trailing whitespaces
    parserSettings.setIgnoreTrailingWhitespaces(true);
    //skip empty lines
    parserSettings.setSkipEmptyLines(true);

    File file = new File("C:\\Users\\abhishyam.c\\Downloads\\Itemcode_Template.csv");
    this.parser = new CsvParser(parserSettings);
    //parser.parse(file);
    parser.beginParsing(file);
    return rowProcessor;
}

private ItemcodeBean getRowData() throws Throwable {
    String[] row;
    try {
        while ((row = parser.parseNext()) != null){
            return rowProcessor.createBean(row, parser.getContext());
        }
    }catch (DataProcessingException e){
        throw new DataProcessingException(e.getColumnName(),e);
    }
   // parser.stopParsing();
    return null;
}

}

+4
source share
1 answer

Just use an error handler, and it will continue if you yourself do not throw an exception:

    //Let set a RowProcessorErrorHandler to log the error. The parser will keep running.
    settings.setProcessorErrorHandler(new RowProcessorErrorHandler() {
        @Override
        public void handleError(DataProcessingException error, Object[] inputRow, ParsingContext context) {
            println(out, "Error processing row: " + Arrays.toString(inputRow));
            println(out, "Error details: column '" + error.getColumnName() + "' (index " + error.getColumnIndex() + ") has value '" + inputRow[error.getColumnIndex()] + "'");
        }
    });

UPDATE: RetryableErrorHandler. , 2.3.0, setDefaultValue(), , keepRecord, .

:

settings.setProcessorErrorHandler(new RetryableErrorHandler<ParsingContext>() {
    @Override
    public void handleError(DataProcessingException error, Object[] inputRow, ParsingContext context) {
        //if there an error in the first column, assign 50 and proceed with the record.
        if(error.getColumnIndex() == 0){ 
            setDefaultValue(50);
        } else { //else keep the record anyway. Null will be used instead.
            keepRecord();
        }
    }
});

, error.getColumnIndex() -1, , . .

+2

Source: https://habr.com/ru/post/1649663/


All Articles