本文介绍了带有Spring Batch的Json Array读取器文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含json数组的文件作为输入:

I have a file as an input which contain a json Array :

[ {
  ...,
  ...
  },
  {
  ...,
  ...
  },
  {
  ...,
  ...
  }
]

我想在不破坏spring批处理主体的情况下进行读取(与FlatFileReader或XmlReader相同)

I want to read it without breaking the spring batch principales (With the same way as FlatFileReader or XmlReader)

我没有找到任何方法来完成已经在spring-batch中实现的读者.

I didn't find any way to do it the readers already implemented in spring-batch .

实现此阅读器的最佳方法是什么?

What's the best way to implement this reader ?

预先感谢

推荐答案

假设您要对StaxEventItemReader建模,因为您想将JSON数组的每个项目都作为Spring Batch中的项目读取,这就是我要做的推荐:

Assuming you want to model the StaxEventItemReader in that you want to read each item of the JSON array as an item in Spring Batch, here's what I'd recommend:

  • RecordSeparatorPolicy-您需要实现自己的RecordSepartorPolicy,以指示您是否已阅读完整项目.您还可以使用RecordSeparatoerPolicy#postProcess清除需要处理的开始和结束[]以及逗号分隔符.
  • LineTokenizer-然后,您将要创建自己的用于解析JSON的LineTokenzier.我今天只是为一个项目工作,因此您可以将该代码作为开始(考虑一下未经测试的内容):

  • RecordSeparatorPolicy - You'll need to implement your own RecordSepartorPolicy that indicates if you've finished reading in the full item or not. You can also use the RecordSeparatoerPolicy#postProcess to cleanup the beginning and ending [] you'll need to deal with as well as the comma delimiters.
  • LineTokenizer - You'll then want to create your own LineTokenzier that parses JSON. I was just working on one today for a project so you can use that code as a start (consider it untested):

public class JsonLineTokenizer implements LineTokenizer {

    @Override
    public FieldSet tokenize(String line) {
        List<String> tokens = new ArrayList<>();

        try {
            HashMap<String,Object> result =
                    new ObjectMapper().readValue(line, HashMap.class);

            tokens.add((String) result.get("field1"));
            tokens.add((String) result.get("field2")));

        } catch (IOException e) {
            throw new RuntimeException("Unable to parse json: " + line);
        }

        return new DefaultFieldSet(tokens.toArray(new String[0]), {"field1", "field2"});
    }
}

这篇关于带有Spring Batch的Json Array读取器文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:04