0

I have a task to read a CSV file then do some logic and then create a JSON for that.

I am kind of stuck with required logic before creating JSON, I need to set Max PR VALUE against the SK as PR for all the same SK's.

My Requirement:

CSV:

SK,VR,ST,PR
1000,1000-Q1,10,187
1000,1000-Q2,20,925  // MAX PR against SK
1001,1001-Q1,10,112
1001,1001-Q2,30,120  // MAX PR against SK

Note: Max PR against SK will always be in the last row of its SK.

I have to read CSV here and need to write JSON data as below :

[
   {
      "SK": "1000",
      "VR": "1000-Q1",
      "ST": "10",
      "PR": "925"
   },
   {
      "SK": "1000",
      "VR": "1000-Q2",
      "ST": "20",
      "PR": "925"
   },
   {
      "SK": "1001",
      "VR": "1001-Q1",
      "ST": "10",
      "PR": "120"
   },
   {
      "SK": "1001",
      "VR": "1001-Q2",
      "ST": "30",
      "PR": "120"
   }
]

Edit:

Code

       File input = new File("input.csv");
       File output = new File("output.json");
       CsvSchema csvSchema = CsvSchema.builder().setUseHeader(true).build();
       CsvMapper csvMapper = new CsvMapper();

       // Read data from CSV file
       List<Object> readAll = csvMapper.readerFor(Map.class).with(csvSchema).readValues(input).readAll();

       ObjectMapper mapper = new ObjectMapper();

       // Write JSON formated data to output.json file
       mapper.writerWithDefaultPrettyPrinter().writeValue(output, readAll);

       // Write JSON formated data to stdout
       System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(readAll));
4

2 回答 2

2

一种方法是首先对每个SK的 CSV 记录进行分组

        String[] HEADERS = { "SK","VR","ST","PR"};

        Reader in = new FileReader("mycsvfile.csv");
        Iterable<CSVRecord> records = CSVFormat.DEFAULT
          .withHeader(HEADERS)
          .withFirstRecordAsHeader()
          .parse(in);

        // Group the records by  SK
     Map<String, List<CSVRecord>> recordListBySK =   StreamSupport
            .stream(records.spliterator(), false).
            collect(Collectors.groupingBy(record -> record.get("SK")));

那么这次你需要再添加一个 Mapping 来保持每个Sk的MAX-PR

 Map<String, Integer> skMaxMap =  recordListBySK
    .entrySet()
    .stream()
    .collect(Collectors
                .toMap( e -> e.getKey(),
                        e -> e.getValue()
                              .stream()
                              .mapToInt(v -> Integer.parseInt(v.get("PR")))
                              .max()
                              .getAsInt() 
                      )
            );

现在您只需像这样构建您的 json Sk列表:

 // Building the new sk (JSON ) objects
 List<NewSk> newSkList = new ArrayList<>();
 recordListBySK
    .values()
    .stream()
    .flatMap(v -> v.stream())
    .forEach(csvRecord -> {
         NewSk newSk = new NewSk(csvRecord.get("SK"),
                                csvRecord.get("VR"),
                                csvRecord.get("ST"),
                                skMaxMap.get(csvRecord.get("SK"))
                                );
         newSkList.add(newSk);
    });

如果您尝试将它们打印出来:

newSkList.forEach(sk -> {
         System.out.print(" "+sk.getSk());
         System.out.print(" "+sk.getVr());
         System.out.print(" "+sk.getSt());
         System.out.print(" "+sk.getPr());
         System.out.println(" ");
     });

你会得到这个

 1001 1001-Q1 10 120 
 1001 1001-Q2 30 120 
 1000 1000-Q1 10 925 
 1000 1000-Q2 20 925

不,您可以使用 JSON 对象映射器将列表写入 JSON 文件。希望能帮助到你

编辑:

public class NewSk {

    private String sk;
    private String vr;
    private String st;
    private String pr;

    public NewSk(String sk, String vr, String st, String pr) {
        this.sk = sk;
        this.vr = vr;
        this.st = st;
        this.pr = pr;
    }

    public String getSk() {
        return sk;
    }

    public void setSk(String sk) {
        this.sk = sk;
    }

    public String getVr() {
        return vr;
    }

    public void setVr(String vr) {
        this.vr = vr;
    }

    public String getSt() {
        return st;
    }

    public void setSt(String st) {
        this.st = st;
    }

    public String getPr() {
        return pr;
    }

    public void setPr(String pr) {
        this.pr = pr;
    }

}
于 2018-03-28T09:48:44.437 回答
0

首先,我制作了一个 pojo 来轻松操作数据:

public class Model {
    private final int sk;
    private final String vr;
    private final int st;
    private final int pr;

    public Model(int sk, String vr, int st, int pr) {
        this.sk = sk;
        this.vr = vr;
        this.st = st;
        this.pr = pr;
    }

    public int getSk() { return sk; }
    public String getVr() { return vr; }
    public int getSt() { return st; }
    public int getPr() { return pr; }

    @Override
    public String toString() {
        return "Model{" +
                "sk=" + sk +
                ", vr='" + vr + '\'' +
                ", st=" + st +
                ", pr=" + pr +
                '}';
    }
}

之后,使用 Java 8 流式传输,您可以进行所需的聊天:

List<Model> models = new ArrayList<>();
models.add(new Model(1000, "1000-Q1", 10, 187));
models.add(new Model(1000, "1000-Q2", 10, 925));
models.add(new Model(1001, "1001-Q1", 10, 112));
models.add(new Model(1001, "1001-Q2", 30, 120));

List<Model> collect = models.stream()
    .collect(Collectors.groupingBy(Model::getSk))
    .entrySet()
    .stream()
    .flatMap(
        v -> {
            int max = v.getValue()
                .stream()
                .map(m -> m.getPr())
                .reduce((a, b) -> Math.max(a, b))
                .get();
            return v.getValue().stream()
                .map(m -> new Model(m.getSk(), m.getVr(), m.getSt(), max));
        }
    )
    .collect(Collectors.toList());

System.out.println(collect);

结果:

[
    {sk=1000, vr='1000-Q1', st=10, pr=925}, 
    {sk=1000, vr='1000-Q2', st=10, pr=925}, 
    {sk=1001, vr='1001-Q1', st=10, pr=120}, 
    {sk=1001, vr='1001-Q2', st=30, pr=120}
]
于 2018-03-28T09:57:09.513 回答