use of com.hazelcast.jet.pipeline.file.CsvFileFormat in project hazelcast by hazelcast.
the class CsvReadFileFnProvider method createReadFileFn.
@SuppressWarnings("unchecked")
@Nonnull
@Override
public <T> FunctionEx<Path, Stream<T>> createReadFileFn(@Nonnull FileFormat<T> format) {
CsvFileFormat<T> csvFileFormat = (CsvFileFormat<T>) format;
// Format is not Serializable
Class<?> formatClazz = csvFileFormat.clazz();
return path -> {
FileInputStream fis = new FileInputStream(path.toFile());
MappingIterator<T> iterator;
Function<T, T> projection = identity();
if (formatClazz == String[].class) {
ObjectReader reader = new CsvMapper().enable(Feature.WRAP_AS_ARRAY).readerFor(String[].class).with(CsvSchema.emptySchema().withSkipFirstDataRow(false));
iterator = reader.readValues(fis);
if (!iterator.hasNext()) {
throw new JetException("Header row missing in " + path);
}
String[] header = (String[]) iterator.next();
List<String> fieldNames = csvFileFormat.fieldNames();
if (fieldNames != null) {
projection = (Function<T, T>) createFieldProjection(header, fieldNames);
}
} else {
iterator = new CsvMapper().readerFor(formatClazz).withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).with(CsvSchema.emptySchema().withHeader()).readValues(fis);
}
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, ORDERED), false).map(projection).onClose(() -> uncheckRun(fis::close));
};
}
Aggregations