Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring batch Spring批读取多记录文件_Spring Batch - Fatal编程技术网

Spring batch Spring批读取多记录文件

Spring batch Spring批读取多记录文件,spring-batch,Spring Batch,这是我在这里的第一篇帖子。如果我没有遵守团体规则,我会提前道歉 基本上,我陷入了一个与Spring批处理相关的问题 我需要读取和处理包含多条记录的文件。如下所示: 标题 一批 卡斯特 反式 卡斯特 选择 反式 卡斯特 选择 反式 卡斯特 选择 反式 卡斯特 选择 反式 批页脚 客户页脚 TRANFOOTER您可能可以使用FlatFileItemReader来读取您发布的每一行,但我想您的行中有更多信息,所以使用标记器来分隔每行的值。而不是在ItemProcessor中检查第一个令牌是CUST还

这是我在这里的第一篇帖子。如果我没有遵守团体规则,我会提前道歉

基本上,我陷入了一个与Spring批处理相关的问题

我需要读取和处理包含多条记录的文件。如下所示:

标题 一批 卡斯特 反式 卡斯特 选择 反式 卡斯特 选择 反式 卡斯特 选择 反式 卡斯特 选择 反式 批页脚 客户页脚 TRANFOOTER您可能可以使用FlatFileItemReader来读取您发布的每一行,但我想您的行中有更多信息,所以使用标记器来分隔每行的值。而不是在ItemProcessor中检查第一个令牌是CUST还是TRANS,它是否是writer的返回值,如果不是,则返回null,这是过滤项的spring批处理机制

您可以在spring批处理中找到关于跳过的解释

您可能可以使用FlatFileItemReader来读取您发布的每一行,但我猜您的行中有更多信息,因此使用标记器来分隔每一行的值。而不是在ItemProcessor中检查第一个令牌是CUST还是TRANS,它是否是writer的返回值,如果不是,则返回null,这是过滤项的spring批处理机制

您可以在spring批处理中找到关于跳过的解释

使用PatternMatchingCompositeLineMapper标记行并映射到正确的域bean以用于CUST和TRANS行。 对于除CUST和TRANS之外的行,编写一个泛型FieldSetMapper,用于将行映射到泛型bean,我们称之为GenericBeanToSkip。。 使用进程CUST和TRANS-transformed bean,但跳过GenericBean跳过从ItemProcessor.process返回null。

使用PatternMatchingCompositeLineMapper标记行,并映射到正确的域bean以用于CUST和TRANS-line。 对于除CUST和TRANS之外的行,编写一个泛型FieldSetMapper,用于将行映射到泛型bean,我们称之为GenericBeanToSkip。。
使用进程CUST和TRANS-transformed bean,但跳过GenericBean跳过从ItemProcessor.process返回null。

Spring batch具有读取复杂文件的能力。唯一的问题是我们必须编写自己的读取器来处理复杂的文件。任何具有特定模式的文件,我们都可以通过SpringBatch读取

这是与您的文件类似的文件格式

CUST,Warren,Q,Darrow,8272 4th Street,New York,IL,76091
TRANS,1165965,2011-01-22 00:13:29,51.43
CUST,Ann,V,Gates,9247 Infinite Loop Drive,Hollywood,NE,37612
CUST,Erica,I,Jobs,8875 Farnam Street,Aurora,IL,36314
TRANS,8116369,2011-01-21 20:40:52,-14.83
TRANS,8116369,2011-01-21 15:50:17,-45.45
TRANS,8116369,2011-01-21 16:52:46,-74.6
TRANS,8116369,2011-01-22 13:51:05,48.55
TRANS,8116369,2011-01-21 16:51:59,98.53
自定义文件读取器

    import Java.util.ArrayList;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
public class CustomerFileReader implements ItemStreamReader<Object> {
private Object curItem = null;
private ItemStreamReader<Object> delegate;
public Object read() throws Exception {
if(curItem == null) {
curItem = (Customer) delegate.read();
}
Customer item = (Customer) curItem;
curItem = null;
if(item != null) {
item.setTransactions(new ArrayList<Transaction>());
while(peek() instanceof Transaction) {
curItem = null;
}
}
return item;
}
public Object peek() throws Exception, UnexpectedInputException,
ParseException {
if (curItem == null) {
curItem = delegate.read();
}
return curItem;
}
public void setDelegate(ItemStreamReader<Object> delegate) {
this.delegate = delegate;
}
public void close() throws ItemStreamException {
delegate.close();
}
public void open(ExecutionContext arg0) throws ItemStreamException {
delegate.open(arg0);
}
public void update(ExecutionContext arg0) throws ItemStreamException {
delegate.update(arg0);
}
}
CustomerFieldSetMapper

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public class CustomerFieldSetMapper implements FieldSetMapper<Customer> {
public Customer mapFieldSet(FieldSet fieldSet) throws BindException {
Customer customer = new Customer();
customer.setAddress(fieldSet.readString("addressNumber") +
" " + fieldSet.readString("street"));
customer.setCity(fieldSet.readString("city"));
customer.setFirstName(fieldSet.readString("firstName"));
customer.setLastName(fieldSet.readString("lastName"));
customer.setMiddleInitial(fieldSet.readString("middleInitial"));
customer.setState(fieldSet.readString("state"));
customer.setZip(fieldSet.readString("zip"));
return customer;
}
}

SpringBatch具有读取复杂文件的能力。唯一的问题是我们必须编写自己的读取器来处理复杂的文件。任何具有特定模式的文件,我们都可以通过SpringBatch读取

这是与您的文件类似的文件格式

CUST,Warren,Q,Darrow,8272 4th Street,New York,IL,76091
TRANS,1165965,2011-01-22 00:13:29,51.43
CUST,Ann,V,Gates,9247 Infinite Loop Drive,Hollywood,NE,37612
CUST,Erica,I,Jobs,8875 Farnam Street,Aurora,IL,36314
TRANS,8116369,2011-01-21 20:40:52,-14.83
TRANS,8116369,2011-01-21 15:50:17,-45.45
TRANS,8116369,2011-01-21 16:52:46,-74.6
TRANS,8116369,2011-01-22 13:51:05,48.55
TRANS,8116369,2011-01-21 16:51:59,98.53
自定义文件读取器

    import Java.util.ArrayList;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
public class CustomerFileReader implements ItemStreamReader<Object> {
private Object curItem = null;
private ItemStreamReader<Object> delegate;
public Object read() throws Exception {
if(curItem == null) {
curItem = (Customer) delegate.read();
}
Customer item = (Customer) curItem;
curItem = null;
if(item != null) {
item.setTransactions(new ArrayList<Transaction>());
while(peek() instanceof Transaction) {
curItem = null;
}
}
return item;
}
public Object peek() throws Exception, UnexpectedInputException,
ParseException {
if (curItem == null) {
curItem = delegate.read();
}
return curItem;
}
public void setDelegate(ItemStreamReader<Object> delegate) {
this.delegate = delegate;
}
public void close() throws ItemStreamException {
delegate.close();
}
public void open(ExecutionContext arg0) throws ItemStreamException {
delegate.open(arg0);
}
public void update(ExecutionContext arg0) throws ItemStreamException {
delegate.update(arg0);
}
}
CustomerFieldSetMapper

import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public class CustomerFieldSetMapper implements FieldSetMapper<Customer> {
public Customer mapFieldSet(FieldSet fieldSet) throws BindException {
Customer customer = new Customer();
customer.setAddress(fieldSet.readString("addressNumber") +
" " + fieldSet.readString("street"));
customer.setCity(fieldSet.readString("city"));
customer.setFirstName(fieldSet.readString("firstName"));
customer.setLastName(fieldSet.readString("lastName"));
customer.setMiddleInitial(fieldSet.readString("middleInitial"));
customer.setState(fieldSet.readString("state"));
customer.setZip(fieldSet.readString("zip"));
return customer;
}
}
CompositeItemProcessor用于将多个ItemProcessor“链接”在一起。因此,CUST ItemProcessor的输出将作为TRANS ItemProcessor的输入。CompositeItemProcessor用于将多个ItemProcessor“链接”在一起。因此,CUST ItemProcessor的输出将作为TRANS ItemProcessor的输入。