diff --git a/src/main/java/info/johtani/elasticsearch/plugin/ingest/csv/CsvProcessor.java b/src/main/java/info/johtani/elasticsearch/plugin/ingest/csv/CsvProcessor.java index 284974c..e8b57d0 100644 --- a/src/main/java/info/johtani/elasticsearch/plugin/ingest/csv/CsvProcessor.java +++ b/src/main/java/info/johtani/elasticsearch/plugin/ingest/csv/CsvProcessor.java @@ -38,6 +38,7 @@ public class CsvProcessor extends AbstractProcessor { private final String field; private final List columns; private final CsvParserSettings csvSettings; + private final CsvParser parser; public CsvProcessor(String tag, String field, List columns, char quoteChar, char separator) throws IOException { super(tag); @@ -46,6 +47,7 @@ public CsvProcessor(String tag, String field, List columns, char quoteCh csvSettings = new CsvParserSettings(); csvSettings.getFormat().setQuote(quoteChar); csvSettings.getFormat().setDelimiter(separator); + parser = new CsvParser(this.csvSettings); } @Override @@ -53,8 +55,7 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { String content = ingestDocument.getFieldValue(field, String.class); if (Strings.hasLength(content)) { - CsvParser parser = new CsvParser(this.csvSettings); - String[] values = parser.parseLine(content); + String[] values = parser.parseLine(content); if (values.length != this.columns.size()) { // TODO should be error? throw new IllegalArgumentException("field[" + this.field + "] size [" diff --git a/src/test/java/info/johtani/elasticsearch/plugin/ingest/csv/CsvProcessorTests.java b/src/test/java/info/johtani/elasticsearch/plugin/ingest/csv/CsvProcessorTests.java index b65ce35..198be3a 100644 --- a/src/test/java/info/johtani/elasticsearch/plugin/ingest/csv/CsvProcessorTests.java +++ b/src/test/java/info/johtani/elasticsearch/plugin/ingest/csv/CsvProcessorTests.java @@ -86,6 +86,27 @@ public void testEmptyField() throws Exception { assertThat(e.getMessage(), equalTo("field[source_field] is empty string.")); } + public void testManyTimes() throws Exception { + CsvProcessor processor = new CsvProcessor(randomAlphaOfLength(10), "source_field", defaultColumns, '\"', ','); + int times = 50000; + + logger.info("start"); + long startTime = System.currentTimeMillis(); + for (int i = 0; i < times; i++) { + Map document = new HashMap<>(); + document.put("source_field", "a_value, b_value"); + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document); + + + Map data = processor.execute(ingestDocument).getSourceAndMetadata(); + + assertThat(data, hasKey("a")); + assertThat(data.get("a"), is("a_value")); + assertThat(data, hasKey("b")); + assertThat(data.get("b"), is("b_value")); + } + logger.info("end. Loop is " + times + " times. Process Time is " + String.valueOf(System.currentTimeMillis() - startTime) + " ms"); + } }