我的输入文件夹中有2个json文件。我需要读取每个json,处理它(假设我的处理器将Json值更改为大写)并将处理后的json写入输出文件夹。所以基本上对于每个json输入文件,我需要将相应的已处理JSON写入输出文件夹。我怎么能做到这一点?
下面是我输入的json文件:雇员-list01.json
[{"firstName":"aaa", "lastName":"bbb"},
{"firstName":"ccc", "lastName":"ddd"}]
雇员-list02.json
[{"firstName":"eee", "lastName":"fff"},
{"firstName":"ggg", "lastName":"hhh"}]
这是我的工作配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd">
<import resource="../config/applicationContext.xml" />
<bean id="multiResourceItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="classpath:json/employee-list*.json" />
<property name="delegate" ref="jsonFileReader" />
</bean>
<bean id="jsonFileReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="recordSeparatorPolicy" ref="recordPolicy" />
<property name="lineMapper" ref="lineMapper" />
</bean>
<bean id="recordPolicy"
class="com.spring.batch.learnings.ComplexJsonRecordSeparatorPolicy" />
<bean id="lineMapper"
class="com.spring.batch.learnings.EmployeeListLineMapper">
<property name="delegate" ref="lineMapperType" />
</bean>
<bean id="lineMapperType"
class="org.springframework.batch.item.file.mapping.JsonLineMapper" />
<bean id="multiResourceItemWriter"
class="org.springframework.batch.item.file.MultiResourceItemWriter">
<property name="resource" value="file:json/employee-list.json" />
<property name="itemCountLimitPerResource" value="2"/>
<property name="saveState" value="true" />
<property name="delegate" ref="jsonFileWriter"/>
</bean>
<bean id="jsonFileWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="headerCallback" ref="headerCallback" />
<property name="footerCallback" ref="footerCallback" />
<property name="lineAggregator" ref="lineAggregator" />
</bean>
<bean id="headerCallback"
class="com.spring.batch.learnings.EmployeeHeaderFooterCallBack">
</bean>
<bean id="footerCallback"
class="com.spring.batch.learnings.EmployeeHeaderFooterCallBack">
</bean>
<bean id="lineAggregator"
class="com.spring.batch.learnings.EmployeeJsonItemAggregator">
</bean>
<bean id="itemProcessor"
class="com.spring.batch.learnings.EmployeeListProcessor" />
<bean id="batchJobListener" class="com.spring.batch.learnings.BatchJobListener" />
<batch:job id="mySimpleJob">
<batch:step id="step1">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="multiResourceItemReader" writer="multiResourceItemWriter"
processor="itemProcessor" commit-interval="1" />
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="batchJobListener" />
</batch:listeners>
</batch:job>
我的EmployeeJsonItemAggregator如下所示
public class EmployeeJsonItemAggregator<Employee> implements LineAggregator<Employee> {
private boolean isFirstObject = true;
@Override
public String aggregate(final Employee aEmployee) {
if (isFirstObject) {
isFirstObject = false;
return JsonUtils.convertObjectToJsonString(aEmployee);
}
return "," + JsonUtils.convertObjectToJsonString(aEmployee);
}
}
JsonUtils:
public class JsonUtils {
public static <T> String convertObjectToJsonString(T object) {
ObjectMapper objectMapper = new ObjectMapper();
ObjectWriter objectWriter = objectMapper.writer().withDefaultPrettyPrinter();
String jsonString = null;
try {
jsonString = objectWriter.writeValueAsString(object);
} catch (JsonProcessingException myException) {
// TODO Auto-generated catch block
myException.printStackTrace();
}
return jsonString;
}
}
EmployeeHeaderFooterCallBack:
public class EmployeeHeaderFooterCallBack implements FlatFileHeaderCallback, FlatFileFooterCallback {
@Override
public void writeHeader(Writer writer) throws IOException {
writer.write("[");
}
@Override
public void writeFooter(Writer writer) throws IOException {
writer.write("]");
}
}
我能够成功读取两个JSON。此外,我能够看到已处理的JSON被写入2个单独的输出JSON文件。但我的第二个JSON输出文件正在写一个额外的&#34;,&#34;标题之后&#34; [&#34;是在开头写的。
我的输出JSON如下所示:
雇员-list.json.1
[
{
"firstName" : "AAA",
"lastName" : "BBB"
}
,{
"firstName" : "CCC",
"lastName" : "DDD"
}
]
employee-list.json.2(你可以看到额外的&#34;,&#34;&#34; [&#34; here)我怎样才能避免这种情况?
[
,{
"firstName" : "EEE",
"lastName" : "FFF"
}
,{
"firstName" : "GGG",
"lastName" : "HHH"
}
]
另外我认为通过上述方法,批处理实际上是同时读取两个输入文件。我希望我的批处理读取输入文件,处理并写入它,然后读取下一个输入文件进行处理和写入。我怎样才能做到这一点?
输出文件必须与输入文件同名。如何配置输出以使其从输入文件中获取输出文件名?