如何在Spring Batch中读取复杂的JSON? [英] How to read a complex JSON in spring batch?

查看:354
本文介绍了如何在Spring Batch中读取复杂的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有一个复杂的JSON.我正在使用FlatFileItemReader读取它.如何使用自定义的ComplexJsonRecordSeparatorPolicy忽略最后一行]"?

[
  {"firstName":"Tom", "lastName":"Cruise"}, 
  {"firstName":"Bruce", "lastName":"Willis"},
  {"firstName":"Liam", "lastName":"Neeson"}
]

我的ComplexJsonRecordSeparatorPolicy如下所示.当我在第4行中有]"时,该类成功工作,但是,在第5行中仅向该行提供]"时,它将引发错误,因为我的后处理器会删除该行而不是忽略它. /p>

public class ComplexJsonRecordSeparatorPolicy extends JsonRecordSeparatorPolicy {

    @Override
    public boolean isEndOfRecord(String line) {

        return StringUtils.countOccurrencesOf(line, "{") == StringUtils.countOccurrencesOf(line, "}")
                && (line.trim().endsWith("}") || line.trim().endsWith(",") || line.trim().endsWith("]"));
    }

    @Override
    public String postProcess(String record) {
        if (record.startsWith("["))
            record = record.substring(1);
        if ((record.endsWith("]") || record.endsWith(",")))
            record = record.substring(0, record.length() - 1);
        return super.postProcess(record);
    }
}

解决方案

我为此创建了一个小示例.请看看,让我知道您的想法

https://github.com/bigzidane/spring-batch-jsonListItem-reader .

有关更多信息,我创建了一个Reader,它将JSON解析为一个列表,然后通过'classToBound'将每个条目映射到POJO,然后由每个遵循Spring Batch标准返回.

该示例将Json文件作为

[
    {
        "name": "zidane",
        "nation": "france"
    },
    {
        "name": "ronaldo",
        "nation": "brazil"
    },
    {
        "name": "marcelo",
        "nation": "brazil"
    }
]

作业配置

<job id="exampleJsonReaderJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="stepId">
            <tasklet>
                <chunk reader="exampleJsonReader" writer="exampleWriter" processor="exampleProcessor" commit-interval="5" />
            </tasklet>
        </step>
    </job>

读者

<bean id="exampleJsonReader" class="com.itservicesdepot.example.springbatch.jsonreader.reader.JsonFileListItemReader" scope="step">
        <property name="resource" value="classpath:soccers.json" />
        <property name="classToBound" value="com.itservicesdepot.example.springbatch.jsonreader.model.SoccerJsonEntry" />
    </bean>

处理器和写入器只是普通的.

在阅读器中,有一个属性"classToBound",即Pojo,它是在Json列表中具有项目的顶部地图.

I have a complex JSON below. I am reading it using FlatFileItemReader. How can I ignore the last line "]", with my customized ComplexJsonRecordSeparatorPolicy?

[
  {"firstName":"Tom", "lastName":"Cruise"}, 
  {"firstName":"Bruce", "lastName":"Willis"},
  {"firstName":"Liam", "lastName":"Neeson"}
]

My ComplexJsonRecordSeparatorPolicy looks like below. This class is successfully working, when I have "]" in line no 4 but, it throws an error when the line is supplied with only "]" in line no 5, as my post processor deletes the line instead of ignoring it.

public class ComplexJsonRecordSeparatorPolicy extends JsonRecordSeparatorPolicy {

    @Override
    public boolean isEndOfRecord(String line) {

        return StringUtils.countOccurrencesOf(line, "{") == StringUtils.countOccurrencesOf(line, "}")
                && (line.trim().endsWith("}") || line.trim().endsWith(",") || line.trim().endsWith("]"));
    }

    @Override
    public String postProcess(String record) {
        if (record.startsWith("["))
            record = record.substring(1);
        if ((record.endsWith("]") || record.endsWith(",")))
            record = record.substring(0, record.length() - 1);
        return super.postProcess(record);
    }
}

解决方案

I have created a small example for this one. Please take a look and let me know your thoughts

https://github.com/bigzidane/spring-batch-jsonListItem-reader.

For more information, I have created a Reader which is to parse JSON as a List and then map every entry to a POJO through 'classToBound' and then return each by each follow Spring Batch standard.

The example takes an Json file as

[
    {
        "name": "zidane",
        "nation": "france"
    },
    {
        "name": "ronaldo",
        "nation": "brazil"
    },
    {
        "name": "marcelo",
        "nation": "brazil"
    }
]

The job configuration

<job id="exampleJsonReaderJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="stepId">
            <tasklet>
                <chunk reader="exampleJsonReader" writer="exampleWriter" processor="exampleProcessor" commit-interval="5" />
            </tasklet>
        </step>
    </job>

The Reader

<bean id="exampleJsonReader" class="com.itservicesdepot.example.springbatch.jsonreader.reader.JsonFileListItemReader" scope="step">
        <property name="resource" value="classpath:soccers.json" />
        <property name="classToBound" value="com.itservicesdepot.example.springbatch.jsonreader.model.SoccerJsonEntry" />
    </bean>

The Processor and Writer are just normal ones.

In the reader, there is an property "classToBound", that is the Pojo which is top map with an item in Json list.

这篇关于如何在Spring Batch中读取复杂的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆