Skip to content

Commit b6e9643

Browse files
committed
new spring-batch-s3 module
1 parent f603b07 commit b6e9643

32 files changed

+2632
-0
lines changed

spring-batch-s3/.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*.{adoc,bat,groovy,html,java,js,jsp,kt,kts,md,properties,py,rb,sh,sql,svg,txt,xml,xsd}]
4+
charset = utf-8
5+
6+
[*.{groovy,java,kt,kts,xml,xsd}]
7+
indent_style = tab
8+
indent_size = 4
9+
continuation_indent_size = 8
10+
end_of_line = lf

spring-batch-s3/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.flattened-pom.xml

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-ntp
2+
-V
48.4 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip

spring-batch-s3/README.adoc

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
= spring-batch-s3
2+
:toc:
3+
:icons: font
4+
:source-highlighter: highlightjs
5+
6+
Spring Batch extension for Amazon S3 (even other S3 compatible may be supported) which contains `S3ItemReader` and `S3ItemWriter` implementations
7+
for reading from and writing to S3 buckets, including support for multipart uploads.
8+
9+
*Note*: these writers are based on the *AWS SDK V2*.
10+
11+
== Installation
12+
13+
To use the `spring-batch-s3` extension, you need to add the following dependency to your Maven or Gradle project:
14+
15+
=== Maven
16+
17+
[source,xml]
18+
----
19+
<dependency>
20+
<groupId>org.springframework.batch.extensions</groupId>
21+
<artifactId>spring-batch-s3</artifactId>
22+
<version>${spring-batch-extensions.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>software.amazon.awssdk</groupId>
26+
<artifactId>apache-client</artifactId>
27+
<version>${aws.sdk.version}</version>
28+
</dependency>
29+
----
30+
31+
=== Gradle
32+
33+
[source,groovy]
34+
----
35+
implementation 'org.springframework.batch.extensions:spring-batch-s3:${springBatchExtensionsVersion}'
36+
implementation 'software.amazon.awssdk:apache-client:${awsSdkVersion}'
37+
----
38+
39+
== Known limitations
40+
41+
* 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.
42+
43+
== Pre-requisites
44+
45+
In order to set up these components you need to provide some additional beans in your Spring Batch configuration:
46+
47+
* An `S3Client` bean to interact with AWS S3.
48+
* In case you want to use the `S3ItemReader`: an instance of `S3Deserializer` for the data you want to read.
49+
* In case you want to use the `S3ItemWriter`: an instance of `S3Serializer` for the data you want to write.
50+
51+
There are two examples of implementation for both `S3Serializer` and `S3Deserializer` provided in this project:
52+
53+
* `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.
54+
* `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.
55+
56+
More details in the JavaDocs of the classes.
57+
58+
=== Configuration of the `S3Client`
59+
60+
To use the `S3ItemReader` and `S3ItemWriter`, you need to configure the AWS S3 client.
61+
This can be done using Java configuration or XML configuration.
62+
63+
So far only this synchronous client is supported, you can't use the `S3AsyncClient` with these components.
64+
65+
==== Java Config
66+
67+
[source,java]
68+
----
69+
@Bean
70+
public S3Client s3Client() {
71+
return S3Client.builder().build();
72+
}
73+
----
74+
75+
=== Configure `S3Serializer`
76+
77+
`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.
78+
79+
==== Java Config
80+
81+
[source,java]
82+
----
83+
@Bean
84+
S3Serializer<String> s3Serializer() {
85+
return new S3StringSerializer();
86+
}
87+
----
88+
89+
=== Configure `S3Deserializer`
90+
91+
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.
92+
93+
In case you don't want to implement your serializer checkout the "Alternatives readers" section below.
94+
95+
==== Java Config
96+
97+
[source,java]
98+
----
99+
@Bean
100+
S3Deserializer<String> s3Deserializer() {
101+
return new S3StringDeserializer();
102+
}
103+
----
104+
105+
== Configuration of `S3ItemReader`
106+
107+
Given the `S3Client` and `S3Deserializer` beans, you can now configure the `S3ItemReader`.
108+
109+
=== Java Config
110+
111+
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.
112+
[source,java]
113+
----
114+
@Bean
115+
ItemReader<String> downloadItemReader() throws Exception {
116+
return new S3ItemReader.Builder<String>()
117+
.s3Client(s3Client())
118+
.bucketName("bucket_name")
119+
.objectKey("object_key")
120+
.deserializer(s3Deserializer())
121+
.bufferSize(1024 * 1024) // Default 128 Bytes
122+
.build();
123+
}
124+
----
125+
126+
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.
127+
128+
=== Alternative reader
129+
130+
Instead `S3ItemReader` you can also use `FlatFileItemReader` with `InputStreamResources` to read files from S3 as well.
131+
To do so this package exposes a `S3InputStreamResource` that can be used for that purpose. Below an example:
132+
133+
[source,java]
134+
----
135+
@Bean
136+
ItemReader<String> itemReader() throws Exception {
137+
final var inputStreamResource = new InputStreamResource(
138+
new S3InputStream(s3Client(),
139+
"bucket_name",
140+
"object_key"));
141+
142+
return new FlatFileItemReaderBuilder<String>()
143+
.name("itemReader")
144+
.resource(inputStreamResource)
145+
.lineMapper(new PassThroughLineMapper( ))
146+
.build();
147+
}
148+
----
149+
150+
== Configuration of `S3ItemWriter`
151+
152+
Given the `S3Client` and `S3Serializer` beans, you can now configure the `S3ItemWriter`.
153+
154+
=== Java Config
155+
156+
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.
157+
[source,java]
158+
----
159+
@Bean
160+
ItemWriter<String> uploadItemWriter() throws IOException {
161+
return new S3ItemWriter.Builder<String>()
162+
.s3Client(s3Client())
163+
.bucketName("bucket_name")
164+
.objectKey("object_key")
165+
.multipartUpload(true) // Default is false
166+
.partSize(10 * 1024 * 1024) // Default is 5 MB
167+
.contentType("text/csv") // Default is application/octet-stream
168+
.serializer(s3Serializer())
169+
.build();
170+
}
171+
----
172+
173+
There are several additional options you can set for the `S3ItemWriter`:
174+
* `multipartUpload`: If set to `true`, the writer will use multipart upload for large files. The default is `false`.
175+
* `partSize`: The size of each part in a multipart upload. The default is 5 MB.
176+
* `contentType`: The content type of the uploaded file. The default is `application/octet-stream`.
177+
178+
== Links
179+
180+
* https://github.com/spring-projects/spring-batch-extensions
181+
* https://spring.io/projects/spring-batch
182+
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/home.html

0 commit comments

Comments
 (0)