Files
ATS_Esper/src/main/java/CSVReader.java
2018-09-06 15:56:21 -07:00

41 lines
1.0 KiB
Java

import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CSVReader implements TickStreamReader {
final Logger log = LoggerFactory.getLogger(CSVReader.class);
File file;
public CSVReader(File file) {
this.file = file;
}
public void run(TickProcessor processor) {
CsvMapper mapper = new CsvMapper();
// allow deserializing Joda time classes
mapper.registerModule(new JodaModule());
CsvSchema schema = mapper.schemaFor(TrueFXTickEvent.class);
try {
MappingIterator<TrueFXTickEvent> it = mapper.readerFor(TrueFXTickEvent.class).with(schema).readValues(file);
while (it.hasNextValue()) {
TrueFXTickEvent value = it.nextValue();
processor.process(value);
}
} catch (IOException e) {
log.error("Reading CSV", e);
}
}
}