use of io.atlasmap.csv.v2.CsvFields in project atlasmap by atlasmap.
the class CsvFieldReader method readSchema.
/**
* Reads only the first row of the document.
*
* If firstRecordAsHeader is set to true it uses column names for field names, otherwise it uses an index
* starting from 0.
*
* @return {@link Document} built from CSV
* @throws AtlasException if it fails
*/
public Document readSchema() throws AtlasException {
CSVFormat csvFormat = csvConfig.newCsvFormat();
CSVParser parser;
try {
document.mark(Integer.MAX_VALUE);
parser = csvFormat.parse(new InputStreamReader(document));
} catch (IOException e) {
throw new AtlasException(e);
}
List<CsvField> fields = new ArrayList<>();
if (csvConfig.isFirstRecordAsHeader()) {
for (String headerName : parser.getHeaderNames()) {
CsvField field = new CsvField();
field.setName(headerName);
field.setPath("/<>/" + headerName);
field.setFieldType(FieldType.STRING);
fields.add(field);
}
} else {
CSVRecord record = parser.iterator().next();
for (int i = 0; i < record.size(); i++) {
CsvField field = new CsvField();
if (parser.getHeaderNames() != null && parser.getHeaderNames().size() > i) {
field.setName(parser.getHeaderNames().get(i));
} else {
field.setColumn(i);
field.setName(String.valueOf(i));
}
field.setPath("/<>/" + field.getName());
field.setFieldType(FieldType.STRING);
fields.add(field);
}
}
try {
document.reset();
} catch (IOException e) {
throw new AtlasException(e);
}
CsvFields csvFields = new CsvFields();
csvFields.getCsvField().addAll(fields);
CsvComplexType csvComplexType = new CsvComplexType();
csvComplexType.setFieldType(FieldType.COMPLEX);
csvComplexType.setCollectionType(CollectionType.LIST);
csvComplexType.setPath("/<>");
csvComplexType.setName("");
csvComplexType.setCsvFields(csvFields);
Fields documentFields = new Fields();
documentFields.getField().add(csvComplexType);
Document document = new Document();
document.setFields(documentFields);
return document;
}
Aggregations