简体   繁体   中英

SpringBatch Read from unstructured csv

I would like to read from an unstructured CSV file. It means it will have different columns types every time. Please help.

Yes, Finally Myself found the solution and i would like to share with you. You can write a LineMapper and you can map unstructured header (dynamic- columns) with each line by the following code. Please Note i have read header while job scheduling and pass it as JobParameter.

      @Bean
  @StepScope
  public FlatFileItemReader<Customer> csvReader(@Value("#{jobParameters[filepath]}") String filepath,
      @Value("#{jobParameters[header]}") String header,
      @Value("#{jobParameters[campaignId]}") String campaignId,
      @Value("#{jobParameters[_id]}") String _id) {

    FlatFileItemReader<Customer> flatFileItemReader = new FlatFileItemReader<>();
    flatFileItemReader.setResource(new FileSystemResource(filepath));
    flatFileItemReader.setName("customer-csv-file-reader");
    flatFileItemReader.setLinesToSkip(1);
    flatFileItemReader.setLineMapper(lineMapper(header,campaignId,_id));
    return flatFileItemReader;
  }




     @Bean
        @StepScope
        public LineMapper<Customer> lineMapper(@Value("#{jobParameters[header]}") String header,
            @Value("#{jobParameters[campaignId]}") String campaignId,
            @Value("#{jobParameters[_id]}") String _id) {
          return new LineMapper<Customer>() {

            public String[] headers = header.split(",");
            @Override
            public Customer mapLine(String line, int linenumber) throws Exception {


                Customer item = new Customer();
                String[] p = line.split(",");

                Map<String, String> properties = IntStream.range(0, headers.length).boxed()
                    .collect(Collectors.toMap(i -> headers[i], i -> p[i]));

                item.setCampaignId(new ObjectId(campaignId));
                item.setInviteId(new ObjectId(_id));
                item.setProperties(properties);
                return item;

            }
          };


      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM