Search in sources :

Example 1 with CsvFields

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) CsvComplexType(io.atlasmap.csv.v2.CsvComplexType) CsvFields(io.atlasmap.csv.v2.CsvFields) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AtlasException(io.atlasmap.api.AtlasException) Document(io.atlasmap.v2.Document) CsvFields(io.atlasmap.csv.v2.CsvFields) Fields(io.atlasmap.v2.Fields) CsvField(io.atlasmap.csv.v2.CsvField) CSVParser(org.apache.commons.csv.CSVParser) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord)

Aggregations

AtlasException (io.atlasmap.api.AtlasException)1 CsvComplexType (io.atlasmap.csv.v2.CsvComplexType)1 CsvField (io.atlasmap.csv.v2.CsvField)1 CsvFields (io.atlasmap.csv.v2.CsvFields)1 Document (io.atlasmap.v2.Document)1 Fields (io.atlasmap.v2.Fields)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVParser (org.apache.commons.csv.CSVParser)1 CSVRecord (org.apache.commons.csv.CSVRecord)1