Item Reader and Writer Implementations
在本节中,我们将介绍前面各节中尚未讨论过的读者和编写器。
In this section, we will introduce you to readers and writers that have not already been discussed in the previous sections.
Decorators
在某些情况下,用户需要将专门的行为附加到预先存在的 ItemReader
。Spring Batch 提供了开箱即用的装饰器,可以为 ItemReader
和 ItemWriter
实现添加额外行为。
In some cases, a user needs specialized behavior to be appended to a pre-existing
ItemReader
. Spring Batch offers some out of the box decorators that can add
additional behavior to to your ItemReader
and ItemWriter
implementations.
Spring Batch 包括以下装饰器:
Spring Batch includes the following decorators:
SynchronizedItemStreamReader
当使用非线程安全的 ItemReader
时,Spring Batch 提供了 SynchronizedItemStreamReader
装饰器,该装饰器可用于使 ItemReader
线程安全。Spring Batch 提供了一个 SynchronizedItemStreamReaderBuilder
来构建 SynchronizedItemStreamReader
的实例。
When using an ItemReader
that is not thread safe, Spring Batch offers the
SynchronizedItemStreamReader
decorator, which can be used to make the ItemReader
thread safe. Spring Batch provides a SynchronizedItemStreamReaderBuilder
to construct
an instance of the SynchronizedItemStreamReader
.
例如,FlatFileItemReader
不是 线程安全的,不能在多线程步骤中使用。该读取器可以用 SynchronizedItemStreamReader
装饰,以便在多线程步骤中安全使用它。以下是如何装饰此类读取器的示例:
For example, the FlatFileItemReader
is not thread-safe and cannot be used in
a multi-threaded step. This reader can be decorated with a SynchronizedItemStreamReader
in order to use it safely in a multi-threaded step. Here is an example of how to decorate
such a reader:
@Bean
public SynchronizedItemStreamReader<Person> itemReader() {
FlatFileItemReader<Person> flatFileItemReader = new FlatFileItemReaderBuilder<Person>()
// set reader properties
.build();
return new SynchronizedItemStreamReaderBuilder<Person>()
.delegate(flatFileItemReader)
.build();
}
SingleItemPeekableItemReader
Spring Batch 包含一个为 ItemReader
添加偷看方法的装饰器。此偷看方法允许用户偷看下一项。重复调用 peek 会返回同一项,并且这是从 read
方法返回的下一项。Spring Batch 提供了一个 SingleItemPeekableItemReaderBuilder
来构建 SingleItemPeekableItemReader
的实例。
Spring Batch includes a decorator that adds a peek method to an ItemReader
. This peek
method lets the user peek one item ahead. Repeated calls to the peek returns the same
item, and this is the next item returned from the read
method. Spring Batch provides a
SingleItemPeekableItemReaderBuilder
to construct an instance of the
SingleItemPeekableItemReader
.
SingleItemPeekableItemReader 的 peek 方法不是线程安全的,因为它不可能在多个线程中遵守 peek。执行 peek 的线程中只有一个线程会在对 read 的下一次调用中获取该项。 |
SingleItemPeekableItemReader’s peek method is not thread-safe, because it would not be possible to honor the peek in multiple threads. Only one of the threads that peeked would get that item in the next call to read. |
SynchronizedItemStreamWriter
当使用非线程安全的 ItemWriter
时,Spring Batch 提供了 SynchronizedItemStreamWriter
装饰器,该装饰器可用于使 ItemWriter
线程安全。Spring Batch 提供了一个 SynchronizedItemStreamWriterBuilder
来构建 SynchronizedItemStreamWriter
的实例。
When using an ItemWriter
that is not thread safe, Spring Batch offers the
SynchronizedItemStreamWriter
decorator, which can be used to make the ItemWriter
thread safe. Spring Batch provides a SynchronizedItemStreamWriterBuilder
to construct
an instance of the SynchronizedItemStreamWriter
.
例如,FlatFileItemWriter
不是 线程安全的,不能在多线程步骤中使用。该写入器可以用 SynchronizedItemStreamWriter
装饰,以便在多线程步骤中安全使用它。以下是如何装饰此类写入器的示例:
For example, the FlatFileItemWriter
is not thread-safe and cannot be used in
a multi-threaded step. This writer can be decorated with a SynchronizedItemStreamWriter
in order to use it safely in a multi-threaded step. Here is an example of how to decorate
such a writer:
@Bean
public SynchronizedItemStreamWriter<Person> itemWriter() {
FlatFileItemWriter<Person> flatFileItemWriter = new FlatFileItemWriterBuilder<Person>()
// set writer properties
.build();
return new SynchronizedItemStreamWriterBuilder<Person>()
.delegate(flatFileItemWriter)
.build();
}
MultiResourceItemWriter
MultiResourceItemWriter
封装一个 ResourceAwareItemWriterItemStream
,并在当前资源中写入的项目计数超过 itemCountLimitPerResource
时创建一个新的输出资源。Spring Batch 提供了一个 MultiResourceItemWriterBuilder
来构建 MultiResourceItemWriter
的实例。
The MultiResourceItemWriter
wraps a ResourceAwareItemWriterItemStream
and creates a new
output resource when the count of items written in the current resource exceeds the
itemCountLimitPerResource
. Spring Batch provides a MultiResourceItemWriterBuilder
to
construct an instance of the MultiResourceItemWriter
.
ClassifierCompositeItemWriter
ClassifierCompositeItemWriter
基于通过提供的 Classifier
实现的路由模式,为每个项目调用一系列 ItemWriter
实现之一。如果所有委托都是线程安全的,则该实现是线程安全的。Spring Batch 提供了一个 ClassifierCompositeItemWriterBuilder
来构建 ClassifierCompositeItemWriter
的实例。
The ClassifierCompositeItemWriter
calls one of a collection of ItemWriter
implementations for each item, based on a router pattern implemented through the provided
Classifier
. The implementation is thread-safe if all delegates are thread-safe. Spring
Batch provides a ClassifierCompositeItemWriterBuilder
to construct an instance of the
ClassifierCompositeItemWriter
.
ClassifierCompositeItemProcessor
ClassifierCompositeItemProcessor
是一个 ItemProcessor
,它基于一个路由调用一个收集的 ItemProcessor
实现模式,该模式通过所提供的 Classifier
实现。Spring Batch 提供了一个 ClassifierCompositeItemProcessorBuilder
来构建 ClassifierCompositeItemProcessor
的实例。
The ClassifierCompositeItemProcessor
is an ItemProcessor
that calls one of a
collection of ItemProcessor
implementations, based on a router pattern implemented
through the provided Classifier
. Spring Batch provides a
ClassifierCompositeItemProcessorBuilder
to construct an instance of the
ClassifierCompositeItemProcessor
.
Messaging Readers And Writers
Spring Batch 为常用的消息系统提供以下读取器和写入器:
Spring Batch offers the following readers and writers for commonly used messaging systems:
AmqpItemReader
AmqpItemReader
是一个 ItemReader
,它使用 AmqpTemplate
从交换中接收或转换消息。Spring Batch 提供了一个 AmqpItemReaderBuilder
来构建 AmqpItemReader
的实例。
The AmqpItemReader
is an ItemReader
that uses an AmqpTemplate
to receive or convert
messages from an exchange. Spring Batch provides a AmqpItemReaderBuilder
to construct
an instance of the AmqpItemReader
.
AmqpItemWriter
AmqpItemWriter
是一个 ItemWriter
,它使用 AmqpTemplate
将消息发送到 AMQP 交换中。如果在所提供的 AmqpTemplate
中没有指定名称,则消息将发送到无名交换。Spring Batch 提供了一个 AmqpItemWriterBuilder
来构建 AmqpItemWriter
的实例。
The AmqpItemWriter
is an ItemWriter
that uses an AmqpTemplate
to send messages to
an AMQP exchange. Messages are sent to the nameless exchange if the name not specified in
the provided AmqpTemplate
. Spring Batch provides an AmqpItemWriterBuilder
to
construct an instance of the AmqpItemWriter
.
JmsItemReader
JmsItemReader
是用于 JMS 的 ItemReader
,它使用 JmsTemplate
。模板应该有一个默认目的地,用于为 read()
方法提供项目。Spring Batch 提供了一个 JmsItemReaderBuilder
来构建 JmsItemReader
的实例。
The JmsItemReader
is an ItemReader
for JMS that uses a JmsTemplate
. The template
should have a default destination, which is used to provide items for the read()
method. Spring Batch provides a JmsItemReaderBuilder
to construct an instance of the
JmsItemReader
.
JmsItemWriter
JmsItemWriter
是用于 JMS 的 ItemWriter
,它使用 JmsTemplate
。模板应该有一个默认目的地,用于在 write(List)
中发送项目。Spring Batch 提供了一个 JmsItemWriterBuilder
来构建 JmsItemWriter
的实例。
The JmsItemWriter
is an ItemWriter
for JMS that uses a JmsTemplate
. The template
should have a default destination, which is used to send items in write(List)
. Spring
Batch provides a JmsItemWriterBuilder
to construct an instance of the JmsItemWriter
.
KafkaItemReader
KafkaItemReader
是用于 Apache Kafka 主题的 ItemReader
。可以将其配置为从同一主题的多个分区中读取消息。它将消息偏移量存储在执行上下文中以支持重新启动功能。Spring Batch 提供了一个 KafkaItemReaderBuilder
来构建 KafkaItemReader
的实例。
The KafkaItemReader
is an ItemReader
for an Apache Kafka topic. It can be configured
to read messages from multiple partitions of the same topic. It stores message offsets
in the execution context to support restart capabilities. Spring Batch provides a
KafkaItemReaderBuilder
to construct an instance of the KafkaItemReader
.
KafkaItemWriter
KafkaItemWriter
是用于 Apache Kafka 的 ItemWriter
,它使用 KafkaTemplate
将事件发送到默认主题。Spring Batch 提供了一个 KafkaItemWriterBuilder
来构建 KafkaItemWriter
的实例。
The KafkaItemWriter
is an ItemWriter
for Apache Kafka that uses a KafkaTemplate
to
send events to a default topic. Spring Batch provides a KafkaItemWriterBuilder
to
construct an instance of the KafkaItemWriter
.
Database Readers
Spring Batch 提供以下数据库读取器:
Spring Batch offers the following database readers:
Neo4jItemReader
Neo4jItemReader
是一个 ItemReader
,它通过使用分页技术从图数据库 Neo4j 读取对象。Spring Batch 提供了一个 Neo4jItemReaderBuilder
来构建 Neo4jItemReader
的实例。
The Neo4jItemReader
is an ItemReader
that reads objects from the graph database Neo4j
by using a paging technique. Spring Batch provides a Neo4jItemReaderBuilder
to
construct an instance of the Neo4jItemReader
.
MongoItemReader
MongoItemReader
是一个 ItemReader
,它通过使用分页技术从 MongoDB 读取文档。Spring Batch 提供了一个 MongoItemReaderBuilder
来构建 MongoItemReader
的实例。
The MongoItemReader
is an ItemReader
that reads documents from MongoDB by using a
paging technique. Spring Batch provides a MongoItemReaderBuilder
to construct an
instance of the MongoItemReader
.
HibernateCursorItemReader
HibernateCursorItemReader
是一个 ItemStreamReader
,用于读取建立在 Hibernate 之上的数据库记录。它执行 HQL 查询,然后在初始化时,随着调用 read()
方法,迭代结果集,连续返回一个与当前行相对应的对象。Spring Batch 提供了一个 HibernateCursorItemReaderBuilder
来构建 HibernateCursorItemReader
的实例。
The HibernateCursorItemReader
is an ItemStreamReader
for reading database records
built on top of Hibernate. It executes the HQL query and then, when initialized, iterates
over the result set as the read()
method is called, successively returning an object
corresponding to the current row. Spring Batch provides a
HibernateCursorItemReaderBuilder
to construct an instance of the
HibernateCursorItemReader
.
HibernatePagingItemReader
HibernatePagingItemReader
是一个 ItemReader
,用于读取建立在 Hibernate 之上的数据库记录,一次仅读取固定数量的项目。Spring Batch 提供了一个 HibernatePagingItemReaderBuilder
来构建 HibernatePagingItemReader
的实例。
The HibernatePagingItemReader
is an ItemReader
for reading database records built on
top of Hibernate and reading only up to a fixed number of items at a time. Spring Batch
provides a HibernatePagingItemReaderBuilder
to construct an instance of the
HibernatePagingItemReader
.
RepositoryItemReader
RepositoryItemReader
是一个 ItemReader
,它通过使用 PagingAndSortingRepository
读取记录。Spring Batch 提供了一个 RepositoryItemReaderBuilder
来构建 RepositoryItemReader
的实例。
The RepositoryItemReader
is an ItemReader
that reads records by using a
PagingAndSortingRepository
. Spring Batch provides a RepositoryItemReaderBuilder
to
construct an instance of the RepositoryItemReader
.
Database Writers
Spring Batch 提供以下数据库写入器:
Spring Batch offers the following database writers:
Neo4jItemWriter
Neo4jItemWriter
是一个 ItemWriter
实现,它写入 Neo4j 数据库。Spring Batch 提供了一个 Neo4jItemWriterBuilder
来构建 Neo4jItemWriter
的实例。
The Neo4jItemWriter
is an ItemWriter
implementation that writes to a Neo4j database.
Spring Batch provides a Neo4jItemWriterBuilder
to construct an instance of the
Neo4jItemWriter
.
MongoItemWriter
MongoItemWriter
是一个 ItemWriter
实现,它使用 Spring Data 的 MongoOperations
的一个实现写入 MongoDB 存储。Spring Batch 提供了一个 MongoItemWriterBuilder
来构建 MongoItemWriter
的实例。
The MongoItemWriter
is an ItemWriter
implementation that writes to a MongoDB store
using an implementation of Spring Data’s MongoOperations
. Spring Batch provides a
MongoItemWriterBuilder
to construct an instance of the MongoItemWriter
.
RepositoryItemWriter
RepositoryItemWriter
是 Spring Data 的 CrudRepository
的一个 ItemWriter
封装。Spring Batch 提供了一个 RepositoryItemWriterBuilder
来构建 RepositoryItemWriter
的实例。
The RepositoryItemWriter
is an ItemWriter
wrapper for a CrudRepository
from Spring
Data. Spring Batch provides a RepositoryItemWriterBuilder
to construct an instance of
the RepositoryItemWriter
.
HibernateItemWriter
HibernateItemWriter
是一个 ItemWriter
,它使用 Hibernate 会话来保存或更新不属于当前 Hibernate 会话的实体。Spring Batch 提供了一个 HibernateItemWriterBuilder
来构建 HibernateItemWriter
的实例。
The HibernateItemWriter
is an ItemWriter
that uses a Hibernate session to save or
update entities that are not part of the current Hibernate session. Spring Batch provides
a HibernateItemWriterBuilder
to construct an instance of the HibernateItemWriter
.
JdbcBatchItemWriter
JdbcBatchItemWriter
是一个 ItemWriter
,它使用来自 NamedParameterJdbcTemplate
的批处理功能为所提供的所有项目执行批处理语句。Spring Batch 提供了一个 JdbcBatchItemWriterBuilder
来构建 JdbcBatchItemWriter
的实例。
The JdbcBatchItemWriter
is an ItemWriter
that uses the batching features from
NamedParameterJdbcTemplate
to execute a batch of statements for all items provided.
Spring Batch provides a JdbcBatchItemWriterBuilder
to construct an instance of the
JdbcBatchItemWriter
.
JpaItemWriter
JpaItemWriter
是一个`ItemWriter`,它使用 JPA EntityManagerFactory
将任何不属于持久性上下文的一部分的实体合并。Spring Batch 提供一个 JpaItemWriterBuilder
,用来构建 JpaItemWriter
实例。
The JpaItemWriter
is an ItemWriter
that uses a JPA EntityManagerFactory
to merge
any entities that are not part of the persistence context. Spring Batch provides a
JpaItemWriterBuilder
to construct an instance of the JpaItemWriter
.
Specialized Readers
Spring Batch 提供以下专门读取器:
Spring Batch offers the following specialized readers:
LdifReader
LdifReader
从 Resource
中读取 LDIF(LDAP 数据交换格式)记录,对其进行解析,并为每次执行的 read
返回一个 LdapAttribute
对象。Spring Batch 提供一个 LdifReaderBuilder
,用于构建 LdifReader
实例。
The LdifReader
reads LDIF (LDAP Data Interchange Format) records from a Resource
,
parses them, and returns a LdapAttribute
object for each read
executed. Spring Batch
provides a LdifReaderBuilder
to construct an instance of the LdifReader
.
MappingLdifReader
MappingLdifReader
从 Resource
中读取 LDIF(LDAP 数据交换格式)记录,对其进行解析,然后将每个 LDIF 记录映射到一个 POJO(简单的旧 Java 对象)。每次读取都会返回一个 POJO。Spring Batch 提供了一个 MappingLdifReaderBuilder
,用于构建 MappingLdifReader
实例。
The MappingLdifReader
reads LDIF (LDAP Data Interchange Format) records from a
Resource
, parses them then maps each LDIF record to a POJO (Plain Old Java Object).
Each read returns a POJO. Spring Batch provides a MappingLdifReaderBuilder
to construct
an instance of the MappingLdifReader
.
AvroItemReader
AvroItemReader
从资源中读取序列化的 Avro 数据;每次读取均返回指定 Java 类或 Avro 模式类型的实例;可以选择将读取器配置为输入是否嵌入 Avro 模式;Spring Batch 提供一个 AvroItemReaderBuilder
,用于构建 AvroItemReader
实例。
The AvroItemReader
reads serialized Avro data from a Resource.
Each read returns an instance of the type specified by a Java class or Avro Schema.
The reader may be optionally configured for input that embeds an Avro schema or not.
Spring Batch provides an AvroItemReaderBuilder
to construct an instance of the AvroItemReader
.
Specialized Writers
Spring Batch 提供以下专门写入器:
Spring Batch offers the following specialized writers:
SimpleMailMessageItemWriter
SimpleMailMessageItemWriter
是一个 ItemWriter
,它可以发送邮件;它将邮件的实际发送委派给 MailSender
实例。Spring Batch 提供了一个 SimpleMailMessageItemWriterBuilder
,用于构建 SimpleMailMessageItemWriter
实例。
The SimpleMailMessageItemWriter
is an ItemWriter
that can send mail messages. It
delegates the actual sending of messages to an instance of MailSender
. Spring Batch
provides a SimpleMailMessageItemWriterBuilder
to construct an instance of the
SimpleMailMessageItemWriter
.
AvroItemWriter
AvroItemWrite
根据给定的类型或模式将 Java 对象序列化到可写资源;可以选择将写入器配置为在输出中嵌入一个 Avro 模式,也可以不嵌入;Spring Batch 提供了一个 AvroItemWriterBuilder
,用于构建 AvroItemWriter
实例。
The AvroItemWrite
serializes Java objects to a WriteableResource according to the given type or Schema.
The writer may be optionally configured to embed an Avro schema in the output or not.
Spring Batch provides an AvroItemWriterBuilder
to construct an instance of the AvroItemWriter
.
Specialized Processors
Spring Batch 提供以下专门处理器:
Spring Batch offers the following specialized processors:
ScriptItemProcessor
ScriptItemProcessor
是一个 ItemProcessor
,它将当前要处理的项目传递给所提供的脚本,并且脚本的结果由处理器返回;Spring Batch 提供了一个 ScriptItemProcessorBuilder
,用于构建 ScriptItemProcessor
的实例。
The ScriptItemProcessor
is an ItemProcessor
that passes the current item to process
to the provided script and the result of the script is returned by the processor. Spring
Batch provides a ScriptItemProcessorBuilder
to construct an instance of the
ScriptItemProcessor
.