The FieldSet
在 Spring Batch 中处理平面文件时,无论其是输入还是输出,最重要的类之一是 FieldSet
。许多架构和库包含有助于您从文件中读取的抽象,但它们通常返回 String
或 String
对象数组。这实际上只让您完成了任务的一半。FieldSet
是 Spring Batch 的抽象,用于启用对文件资源中字段的绑定。它允许开发人员以与处理数据库输入相同的方式使用文件输入。FieldSet
在概念上类似于 JDBC ResultSet
。FieldSet
只需要一个参数:令牌的 String
数组。您还可以选择配置字段的名称,以便可以通过索引或名称访问字段,就像 ResultSet
中的模式一样,如下面的示例所示:
When working with flat files in Spring Batch, regardless of whether it is for input or
output, one of the most important classes is the FieldSet
. Many architectures and
libraries contain abstractions for helping you read in from a file, but they usually
return a String
or an array of String
objects. This really only gets you halfway
there. A FieldSet
is Spring Batch’s abstraction for enabling the binding of fields from
a file resource. It allows developers to work with file input in much the same way as
they would work with database input. A FieldSet
is conceptually similar to a JDBC
ResultSet
. A FieldSet
requires only one argument: a String
array of tokens.
Optionally, you can also configure the names of the fields so that the fields may be
accessed either by index or name as patterned after ResultSet
, as shown in the following
example:
String[] tokens = new String[]{"foo", "1", "true"};
FieldSet fs = new DefaultFieldSet(tokens);
String name = fs.readString(0);
int value = fs.readInt(1);
boolean booleanValue = fs.readBoolean(2);
FieldSet
接口上有更多选项,例如 Date
、long、BigDecimal
等。FieldSet
的最大优势在于它可以提供平面文件输入的一致性解析。它不会以可能出乎意料的方式在每个批处理作业中进行不同的解析,而是可以保持一致,无论是在处理格式异常导致的错误,还是在进行简单数据转换时。
There are many more options on the FieldSet
interface, such as Date
, long,
BigDecimal
, and so on. The biggest advantage of the FieldSet
is that it provides
consistent parsing of flat file input. Rather than each batch job parsing differently in
potentially unexpected ways, it can be consistent, both when handling errors caused by a
format exception, or when doing simple data conversions.