Apache Commons CSVParser – 如何将列拆分为单独的键?

huangapple 未分类评论56阅读模式
英文:

Apache Commons CSVParser - how to split columns to separate keys?

问题

I'm trying to map my CSV file into the POJO class in Spring Boot, but still I get nulls from my parser. This is probably because of the fact that parser put all columns from my CSV files as one key (doesn't split them). That's why instead of finding first column header called "Pos_holder", it returns all the headers in one place, which is not understandable for it.

Here's the code of my CSV parser:

class CounterpartyResourceCsvParser {

CounterpartyResource mapToResource(Map<String, String> map, int rowNum, String userId) {
    CounterpartyResource counterpartyResource = new CounterpartyResource();
    counterpartyResource.setPositionHolderId(getOrThrow(map, "Pos_holder", rowNum, userId));
    counterpartyResource.setPositionHolderIdType(getOrThrow(map, "position_holder_id_type", rowNum, userId));
    counterpartyResource.setPositionHolderEmail(getOrThrow(map, "Email_pos_holder", rowNum, userId));
    counterpartyResource.setParentEntityId(getOrThrow(map, "Ultimate_parent", rowNum, userId));
    counterpartyResource.setParentEntityIdType(getOrThrow(map, "ultimate_parent_entity_id_type", rowNum, userId));
    counterpartyResource.setParentEntityEmail(getOrThrow(map, "Email_parent", rowNum, userId));
    counterpartyResource.setParentInvestmentStatus(getOrThrow(map, "Parent_col_inv_sta", rowNum, userId).toUpperCase());
    counterpartyResource.setFinancialInstitutionFlag(getOrThrow(map, "Fin_inst_flag", rowNum, userId).toUpperCase());
    counterpartyResource.setNdg(getOrThrow(map, "NDG", rowNum, userId));
    counterpartyResource.setUnderlyingClients(getOrThrow(map, "Underlying_clients", rowNum, userId).toUpperCase());
    counterpartyResource.setResponsiblePerson(getOrThrow(map, "Resp_person", rowNum, userId));
    counterpartyResource.setResponsibleDeputy(getOrThrow(map, "Resp_deputy", rowNum, userId));
    counterpartyResource.setResponsiblePersonEmail(getOrThrow(map, "Resp_email", rowNum, userId));
    counterpartyResource.setResponsibleDeputyEmail(getOrThrow(map, "Deputy_email", rowNum, userId));
    counterpartyResource.setComments(map.get("Comments"));
   
    return counterpartyResource;
}

private String getOrThrow(Map<String, String> map, String key, int rowNum, String userId) {
    return Optional.ofNullable(map.get(key)).orElseThrow(() -> new FileValidationException(key, userId, rowNum));
}

List<CounterpartyResource> csvToResources(InputStream inputStream, String encoding, String userId, boolean isMaksyFieldsAllowed) throws IOException {
    CSVFormat format = CSVFormat.EXCEL.withFirstRecordAsHeader();
    CSVParser parser = new CSVParser(new InputStreamReader(inputStream, encoding), format);
    List<CSVRecord> records = parser.getRecords();
    
    List<CounterpartyResource> resources = new ArrayList<>(records.size());
    for (int i = 0; i < records.size(); i++) {
        resources.add(mapToResource(records.get(i).toMap(), i, userId));
    }
    return resources;
}

}


Here's the POJO:

public class CounterpartyResource {

private Long id;
private String positionHolderId;
private String positionHolderIdType;
private String positionHolderEmail;
private String parentEntityId;
private String parentEntityIdType;
private String parentEntityEmail;
private String parentInvestmentStatus;
private String financialInstitutionFlag;
private String ndg;
private String underlyingClients;
private String responsiblePerson;
private String responsibleDeputy;
private String responsiblePersonEmail;
private String responsibleDeputyEmail;
private String entity;
private String insertionUser;
private String updateUser;
private Date insertionTime;
private Date updateTime;
private String updatedFieldSummary;
private Boolean updatedSuccessfully;
private String comments;
private String maksyRefClient;
private String maksyRefNdl;
private String maksyRefDepo;

public static CounterpartyResourceBuilder builder() {
    return new CounterpartyResourceBuilder();
}

public static class CounterpartyResourceBuilder {
    // ... (getter and setter methods are not translated)
}

}


The nulls which I mentioned before, appear in the following line:

return Optional.ofNullable(map.get(key)).orElseThrow(() -> new FileValidationException(key, userId, rowNum));


My question is: how to make the Apache parser see all the columns' headers separately? What should I add to my code to split this one long String, so I could achieve pairs like: "Pos_holder"-value; "position_holder_id_type"-value, "Email_pos_holder"-value, etc. ?

EDIT: Here's how my csv sample looks like:
[![enter image description here][1]][1]

Thanks in advance for your help!
Cheers, Mat
英文:

I'm trying to map my CSV file into the POJO class in Spring Boot, but still I get nulls from my parser. This is probably because of the fact that parser put all columns from my CSV files as one key (doesn't split them). That's why instead of finding first column header called "Pos_holder", it returns all the headers in one place, which is not understable for it.

Here's the code of my CSV parser:

class CounterpartyResourceCsvParser {

    CounterpartyResource mapToResource(Map&lt;String, String&gt; map, int rowNum, String userId) {
        CounterpartyResource counterpartyResource = new CounterpartyResource();
        counterpartyResource.setPositionHolderId(getOrThrow(map, &quot;Pos_holder&quot;, rowNum, userId));
        counterpartyResource.setPositionHolderIdType(getOrThrow(map,&quot;position_holder_id_type&quot;,rowNum,userId));
        counterpartyResource.setPositionHolderEmail(getOrThrow(map, &quot;Email_pos_holder&quot;, rowNum, userId));
        counterpartyResource.setParentEntityId(getOrThrow(map, &quot;Ultimate_parent&quot;, rowNum, userId));
        counterpartyResource.setParentEntityIdType(getOrThrow(map,&quot;ultimate_parent_entity_id_type&quot;,rowNum,userId));
        counterpartyResource.setParentEntityEmail(getOrThrow(map, &quot;Email_parent&quot;, rowNum, userId));
        counterpartyResource.setParentInvestmentStatus(getOrThrow(map, &quot;Parent_col_inv_sta&quot;, rowNum, userId).toUpperCase());
        counterpartyResource.setFinancialInstitutionFlag(getOrThrow(map, &quot;Fin_inst_flag&quot;, rowNum, userId).toUpperCase());
        counterpartyResource.setNdg(getOrThrow(map, &quot;NDG&quot;, rowNum, userId));
        counterpartyResource.setUnderlyingClients(getOrThrow(map, &quot;Underlying_clients&quot;, rowNum, userId).toUpperCase());
        counterpartyResource.setResponsiblePerson(getOrThrow(map, &quot;Resp_person&quot;, rowNum, userId));
        counterpartyResource.setResponsibleDeputy(getOrThrow(map, &quot;Resp_deputy&quot;, rowNum, userId));
        counterpartyResource.setResponsiblePersonEmail(getOrThrow(map, &quot;Resp_email&quot;, rowNum, userId));
        counterpartyResource.setResponsibleDeputyEmail(getOrThrow(map, &quot;Deputy_email&quot;, rowNum, userId));
        counterpartyResource.setComments(map.get(&quot;Comments&quot;));
       
        return counterpartyResource;
    }

    private String getOrThrow(Map&lt;String, String&gt; map, String key, int rowNum, String userId) {
        return Optional.ofNullable(map.get(key)).orElseThrow(() -&gt; new FileValidationException(key, userId, rowNum));
    }

    List&lt;CounterpartyResource&gt; csvToResources(InputStream inputStream, String encoding, String userId, boolean isMaksyFieldsAllowed) throws IOException {
        CSVFormat format = CSVFormat.EXCEL.withFirstRecordAsHeader();
        CSVParser parser = new CSVParser(new InputStreamReader(inputStream, encoding), format);
        List&lt;CSVRecord&gt; records = parser.getRecords();
        
        List&lt;CounterpartyResource&gt; resources = new ArrayList&lt;&gt;(records.size());
        for (int i = 0; i &lt; records.size(); i++) {
            resources.add(mapToResource(records.get(i).toMap(), i, userId));
        }
        return resources;
    }
}

Here's the POJO:

public class CounterpartyResource {

    private Long id;
    private String positionHolderId;
    private String positionHolderIdType;
    private String positionHolderEmail;
    private String parentEntityId;
    private String parentEntityIdType;
    private String parentEntityEmail;
    private String parentInvestmentStatus;
    private String financialInstitutionFlag;
    private String ndg;
    private String underlyingClients;
    private String responsiblePerson;
    private String responsibleDeputy;
    private String responsiblePersonEmail;
    private String responsibleDeputyEmail;
    private String entity;
    private String insertionUser;
    private String updateUser;
    private Date insertionTime;
    private Date updateTime;
    private String updatedFieldSummary;
    private Boolean updatedSuccessfully;
    private String comments;
    private String maksyRefClient;
    private String maksyRefNdl;
    private String maksyRefDepo;

    public static CounterpartyResourceBuilder builder() {
        return new CounterpartyResourceBuilder();
    }

    public static class CounterpartyResourceBuilder {
        private Long id;
        private String positionHolderId;
        private String positionHolderIdType;
        private String positionHolderEmail;
        private String parentEntityId;
        private String parentEntityIdType;
        private String parentEntityEmail;
        private String parentInvestmentStatus;
        private String financialInstitutionFlag;
        private String ndg;
        private String underlyingClients;
        private String responsiblePerson;
        private String responsibleDeputy;
        private String responsiblePersonEmail;
        private String responsibleDeputyEmail;
        private String entity;
        private String insertionUser;
        private String updateUser;
        private Date insertionTime;
        private Date updateTime;
        private String updatedFieldSummary;
        private Boolean updatedSuccessfully;
        private String comments;
        private String maksyRefClient;
        private String maksyRefNdl;
        private String maksyRefDepo;

        public CounterpartyResource build() {
            CounterpartyResource counterpartyResource = new CounterpartyResource();
            counterpartyResource.id = this.id;
            counterpartyResource.positionHolderId = this.positionHolderId;
            counterpartyResource.positionHolderIdType = this.positionHolderIdType;
            counterpartyResource.positionHolderEmail = this.positionHolderEmail;
            counterpartyResource.parentEntityId = this.parentEntityId;
            counterpartyResource.parentEntityIdType = this.parentEntityIdType;
            counterpartyResource.parentEntityEmail = this.parentEntityEmail;
            counterpartyResource.parentInvestmentStatus = this.parentInvestmentStatus;
            counterpartyResource.financialInstitutionFlag = this.financialInstitutionFlag;
            counterpartyResource.ndg = this.ndg;
            counterpartyResource.underlyingClients = this.underlyingClients;
            counterpartyResource.responsiblePerson = this.responsiblePerson;
            counterpartyResource.responsibleDeputy = this.responsibleDeputy;
            counterpartyResource.responsiblePersonEmail = this.responsiblePersonEmail;
            counterpartyResource.responsibleDeputyEmail = this.responsibleDeputyEmail;
            counterpartyResource.entity = this.entity;
            counterpartyResource.insertionUser = this.insertionUser;
            counterpartyResource.updateUser = this.updateUser;
            counterpartyResource.insertionTime = this.insertionTime;
            counterpartyResource.updateTime = this.updateTime;
            counterpartyResource.updatedFieldSummary = this.updatedFieldSummary;
            counterpartyResource.updatedSuccessfully = this.updatedSuccessfully;
            counterpartyResource.comments = this.comments;
            counterpartyResource.maksyRefClient = this.maksyRefClient;
            counterpartyResource.maksyRefNdl = this.maksyRefNdl;
            counterpartyResource.maksyRefDepo = this.maksyRefDepo;
            return counterpartyResource;
        }
    // getters &amp; setters
}

The nulls which I mentioned before, appear in the following line:

return Optional.ofNullable(map.get(key)).orElseThrow(() -&gt; new FileValidationException(key, userId, rowNum));

My question is: how to make the Apache parser see all the columns' headers separetely? What should I add to my code to split this one long String, so I could achieve pairs like: "Pos_holder"-value; "position_holder_id_type"-value, "Email_pos_holder"-value, etc. ?

EDIT: Here's how my csv sample looks like:
Apache Commons CSVParser – 如何将列拆分为单独的键?

Thanks in advance for your help!
Cheers, Mat

huangapple
  • 本文由 发表于 2020年7月27日 22:42:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/63117724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定