Skip to content

Commit 38f3a2e

Browse files
authored
Merge pull request #182 from andreacioni/spring-batch-s3
New spring-batch-s3 module
2 parents b390b3b + 28cf752 commit 38f3a2e

38 files changed

+3049
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Spring Batch S3
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- '.github/workflows/spring-batch-s3.yml'
7+
- 'spring-batch-s3/**'
8+
push:
9+
paths:
10+
- '.github/workflows/spring-batch-s3.yml'
11+
- 'spring-batch-s3/**'
12+
13+
env:
14+
MAVEN_ARGS: -B -V -ntp -e -Djansi.passthrough=true -Dstyle.color=always
15+
16+
jobs:
17+
build:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- name: Set up Java
22+
uses: actions/setup-java@v4
23+
with:
24+
distribution: 'temurin'
25+
java-version: '25'
26+
cache: 'maven'
27+
- name: Build with Maven
28+
run: ./mvnw $MAVEN_ARGS verify javadoc:javadoc
29+
working-directory: spring-batch-s3

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This project is part of the [Spring organization][] on GitHub.
1313
| [`spring-batch-excel`](spring-batch-excel) | Support for [Microsoft Excel] | [@mdeinum](https://github.com/mdeinum) | [![Maven Central](https://img.shields.io/maven-central/v/org.springframework.batch.extensions/spring-batch-excel?label)](https://central.sonatype.com/artifact/org.springframework.batch.extensions/spring-batch-excel) | [![Spring Batch Excel](https://github.com/spring-projects/spring-batch-extensions/actions/workflows/spring-batch-excel.yml/badge.svg)](https://github.com/spring-projects/spring-batch-extensions/actions/workflows/spring-batch-excel.yml?query=branch%3Amain) |
1414
| [`spring-batch-neo4j`](spring-batch-neo4j) | Support for [Neo4j] | [@michael-simons](https://github.com/michael-simons) | [![Maven Central](https://img.shields.io/maven-central/v/org.springframework.batch.extensions/spring-batch-neo4j?label)](https://central.sonatype.com/artifact/org.springframework.batch.extensions/spring-batch-neo4j) | [![Spring Batch Neo4j](https://github.com/spring-projects/spring-batch-extensions/actions/workflows/spring-batch-neo4j.yml/badge.svg)](https://github.com/spring-projects/spring-batch-extensions/actions/workflows/spring-batch-neo4j.yml?query=branch%3Amain) |
1515
| [`spring-batch-notion`](spring-batch-notion) | Support for [Notion] | [@scordio](https://github.com/scordio) | [![Maven Central](https://img.shields.io/maven-central/v/org.springframework.batch.extensions/spring-batch-notion?label)](https://central.sonatype.com/artifact/org.springframework.batch.extensions/spring-batch-notion) | [![Spring Batch Notion](https://github.com/spring-projects/spring-batch-extensions/actions/workflows/spring-batch-notion.yml/badge.svg?branch=main)](https://github.com/spring-projects/spring-batch-extensions/actions/workflows/spring-batch-notion.yml?query=branch%3Amain) |
16+
| [`spring-batch-s3`](spring-batch-s3) | Support for [Amazon S3] | [@andreacioni](https://github.com/andreacioni) | [![Maven Central](https://img.shields.io/maven-central/v/org.springframework.batch.extensions/spring-batch-s3?label)](https://central.sonatype.com/artifact/org.springframework.batch.extensions/spring-batch-s3) | [![Spring Batch S3](https://github.com/spring-projects/spring-batch-extensions/actions/workflows/spring-batch-s3.yml/badge.svg?branch=main)](https://github.com/spring-projects/spring-batch-extensions/actions/workflows/spring-batch-s3.yml?query=branch%3Amain) |
1617

1718
## Getting support
1819

@@ -100,6 +101,7 @@ noted differently for individual extension Modules, but this should be the rare
100101

101102
**We look forward to your contributions!!**
102103

104+
[Amazon S3]: https://aws.amazon.com/s3/
103105
[Apache License]: https://www.apache.org/licenses/LICENSE-2.0
104106
[Contributor Guidelines]: CONTRIBUTING.md
105107
[Elasticsearch]: https://www.elastic.co

spring-batch-s3/.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
# Unix-style newlines with a newline ending every file
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
9+
# Maven POM code convention
10+
[pom.xml]
11+
indent_size = 2
12+
indent_style = space
13+
max_line_length = 205

spring-batch-s3/.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
### Maven ###
2+
target/
3+
.mvn/wrapper/maven-wrapper.jar
4+
.flattened-pom.xml
5+
6+
### STS ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
15+
### IntelliJ IDEA ###
16+
.idea
17+
*.iws
18+
*.iml
19+
*.ipr
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
build/
28+
!**/src/main/**/build/
29+
!**/src/test/**/build/
30+
31+
### VS Code ###
32+
.vscode/

spring-batch-s3/.mvn/jvm.config

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
2+
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
3+
--add-exports jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
4+
--add-exports jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
5+
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
6+
--add-exports jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
7+
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
8+
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
9+
--add-opens jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
10+
--add-opens jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
wrapperVersion=3.3.4
2+
distributionType=only-script
3+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip

spring-batch-s3/README.adoc

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
= spring-batch-s3
2+
:toc:
3+
:icons: font
4+
:source-highlighter: highlightjs
5+
6+
https://spring.io/projects/spring-batch[Spring Batch] extension for https://aws.amazon.com/it/s3/[Amazon S3] which contains `S3ItemReader` and `S3ItemWriter` implementations for reading from and writing to S3 buckets, including support for multipart uploads. Other S3-compatible technologies (like as Google Cloud Storage, MinIO, etc.) may be supported, but they are not guaranteed to work.
7+
8+
*Note*: these writers are based on the *AWS SDK V2*.
9+
10+
== Installation
11+
12+
To use the `spring-batch-s3` extension, you need to add the following dependency to your Maven or Gradle project:
13+
14+
=== Maven
15+
16+
[source,xml]
17+
----
18+
<dependency>
19+
<groupId>org.springframework.batch.extensions</groupId>
20+
<artifactId>spring-batch-s3</artifactId>
21+
<version>${spring-batch-extensions.version}</version>
22+
</dependency>
23+
----
24+
25+
=== Gradle
26+
27+
[source,groovy]
28+
----
29+
implementation 'org.springframework.batch.extensions:spring-batch-s3:${springBatchExtensionsVersion}'
30+
----
31+
32+
== Known limitations
33+
34+
* The `S3ItemReader` and `S3ItemWriter` are designed to work with the synchronous AWS S3 client (`S3Client`). They do not support the asynchronous client (`S3AsyncClient`) at this time.
35+
36+
== Pre-requisites
37+
38+
In order to set up these components you need to provide some additional beans in your Spring Batch configuration:
39+
40+
* An `S3Client` bean to interact with AWS S3.
41+
* In case you want to use the `S3ItemReader`: an instance of `S3Deserializer` for the data you want to read.
42+
* In case you want to use the `S3ItemWriter`: an instance of `S3Serializer` for the data you want to write.
43+
44+
There are two examples of implementation for both `S3Serializer` and `S3Deserializer` provided in this project:
45+
46+
* `S3StringSerializer`: take a `String` as input and writes it to S3 as a UTF-8 encoded byte array. The write functions add a line termination character at the end of each string.
47+
* `S3StringDeserializer`: takes a UTF-8 encoded byte array from S3 and converts it to a `String`. The implementation of this deserializer is *stateful* because lines may arrive in different chunks.
48+
49+
More details in the JavaDocs of the classes.
50+
51+
=== Configuration of the `S3Client`
52+
53+
To use the `S3ItemReader` and `S3ItemWriter`, you need to configure the AWS S3 client by providing a `S3Client`. Checkout the https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/http-configuration-apache.html[AWS SDK for Java] documentation for more details on how to configure this.
54+
55+
=== Configure `S3Serializer`
56+
57+
`S3StringSerializer` is a simple implementation of `S3Serializer` that takes a `String` as input and writes it to S3 as a UTF-8 encoded byte array. You are encouraged to implement your own serializer if you need to handle different data types or formats.
58+
59+
==== Java Config
60+
61+
[source,java]
62+
----
63+
@Bean
64+
S3Serializer<String> s3Serializer() {
65+
return new S3StringSerializer();
66+
}
67+
----
68+
69+
=== Configure `S3Deserializer`
70+
71+
Similarly, `S3StringDeserializer` is a simple implementation of `S3Deserializer` that takes a UTF-8 encoded byte array from S3 and converts it to a `String`. You can implement your own deserializer if you need to handle different data types or formats.
72+
73+
In case you don't want to implement your serializer checkout the "Alternatives readers" section below.
74+
75+
==== Java Config
76+
77+
[source,java]
78+
----
79+
@Bean
80+
S3Deserializer<String> s3Deserializer() {
81+
return new S3StringDeserializer();
82+
}
83+
----
84+
85+
== Configuration of `S3ItemReader`
86+
87+
Given the `S3Client` and `S3Deserializer` beans, you can now configure the `S3ItemReader`.
88+
89+
=== Java Config
90+
91+
To configure the `S3ItemReader`, you need to set up the AWS S3 client and specify the bucket and object key from which you want to read data.
92+
[source,java]
93+
----
94+
@Bean
95+
ItemReader<String> downloadItemReader() throws Exception {
96+
return new S3ItemReader.Builder<String>()
97+
.s3Client(s3Client())
98+
.bucketName("bucket_name")
99+
.objectKey("object_key")
100+
.deserializer(s3Deserializer())
101+
.bufferSize((int) DataSize.ofMegabytes(1).toBytes()) // Default 128 Bytes
102+
.build();
103+
}
104+
----
105+
106+
There is also an additional option to set the `bufferSize` which is the size of the buffer used to read data from S3. The default value is 128 bytes, but you can increase it to improve memory consumption The bast value for this parameter is the average length of the lines in your file.
107+
108+
=== Alternative reader
109+
110+
Instead of `S3ItemReader` you can also use `FlatFileItemReader` with `InputStreamResources` to read files from S3 as well.
111+
To do so this package exposes a `S3InputStream` that can be used for that purpose. Below an example:
112+
113+
[source,java]
114+
----
115+
@Bean
116+
ItemReader<String> itemReader() throws Exception {
117+
final var inputStreamResource = new InputStreamResource(
118+
new S3InputStream(s3Client(),
119+
"bucket_name",
120+
"object_key"));
121+
122+
return new FlatFileItemReaderBuilder<String>()
123+
.name("itemReader")
124+
.resource(inputStreamResource)
125+
.lineMapper(new PassThroughLineMapper( ))
126+
.build();
127+
}
128+
----
129+
130+
== Configuration of `S3ItemWriter`
131+
132+
Given the `S3Client` and `S3Serializer` beans, you can now configure the `S3ItemWriter`.
133+
134+
=== Java Config
135+
136+
To configure the `S3ItemWriter`, you need to set up the AWS S3 client and specify the bucket and object key to which you want to write data.
137+
[source,java]
138+
----
139+
@Bean
140+
ItemWriter<String> uploadItemWriter() throws IOException {
141+
return new S3ItemWriter.Builder<String>()
142+
.s3Client(s3Client())
143+
.bucketName("bucket_name")
144+
.objectKey("object_key")
145+
.multipartUpload(true) // Default is false
146+
.partSize((int) DataSize.ofMegabytes(10).toBytes()) // Default is 5 MB
147+
.contentType("text/csv") // Default is application/octet-stream
148+
.serializer(s3Serializer())
149+
.build();
150+
}
151+
----
152+
153+
There are several additional options you can set for the `S3ItemWriter`:
154+
155+
* `multipartUpload`: If set to `true`, the writer will use multipart upload for large files. The default is `false`.
156+
* `partSize`: The size of each part in a multipart upload. The default is 5 MB.
157+
* `contentType`: The content type of the uploaded file. The default is `application/octet-stream`.

0 commit comments

Comments
 (0)