Skip to content

Commit 9a1828f

Browse files
committed
[bq] refactoring & dependency upgrade
1 parent 50f93d3 commit 9a1828f

29 files changed

+161
-174
lines changed

.github/workflows/spring-batch-bigquery.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
cache: 'maven'
2121
- name: Build with Maven
2222
run: mvn -B package --file pom.xml
23-
working-directory: spring-batch-bigquery
23+
working-directory: spring-batch-bigquery

spring-batch-bigquery/README.adoc

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# spring-batch-bigquery
1+
= spring-batch-bigquery
22

3-
Spring Batch extension which contains an `ItemWriter` implementation for https://cloud.google.com/bigquery[BigQuery] based on https://github.com/googleapis/java-bigquery[Java BigQuery]. It supports writing https://en.wikipedia.org/wiki/Comma-separated_values[CSV], https://en.wikipedia.org/wiki/JSON[JSON] using https://cloud.google.com/bigquery/docs/batch-loading-data[load jobs].
3+
Spring Batch extension which contains an `ItemWriter` implementation for https://cloud.google.com/bigquery[BigQuery] based on https://github.com/googleapis/java-bigquery[Java BigQuery].
4+
It supports writing https://en.wikipedia.org/wiki/Comma-separated_values[CSV], https://en.wikipedia.org/wiki/JSON[JSON] using https://cloud.google.com/bigquery/docs/batch-loading-data[load jobs].
45

5-
## Configuration of `BigQueryCsvItemWriter`
6+
== Configuration of `BigQueryCsvItemWriter`
67

78
Next to the https://docs.spring.io/spring-batch/reference/html/configureJob.html[configuration of Spring Batch] one needs to configure the `BigQueryCsvItemWriter`.
89

9-
```javaBigQueryCsv
10+
[source,java]
11+
----
1012
@Bean
1113
BigQueryCsvItemWriter<MyDto> bigQueryCsvWriter() {
1214
WriteChannelConfiguration writeConfiguration = WriteChannelConfiguration
@@ -20,19 +22,19 @@ BigQueryCsvItemWriter<MyDto> bigQueryCsvWriter() {
2022
.writeChannelConfig(writeConfiguration)
2123
.build();
2224
}
23-
```
25+
----
2426

2527
Additional examples could be found in https://github.com/spring-projects/spring-batch-extensions/blob/main/spring-batch-bigquery/src/test/java/org/springframework/batch/extensions/bigquery/writer/builder/[here].
2628

27-
## Configuration properties
29+
== Configuration properties
2830
[cols="1,1,4"]
29-
.Properties for item writer
31+
.Properties for an item writer
3032
|===
3133
| Property | Required | Description
3234

3335
| `bigQuery` | yes | BigQuery object that provided by BigQuery Java Library. Responsible for connection with BigQuery.
3436
| `writeChannelConfig` | yes | BigQuery write channel config provided by BigQuery Java Library. Responsible for configuring data type, data channel, jobs that will be sent to BigQuery.
35-
| `rowMapper` | no | Your own converter that specifies how to convert input CSV / JSON to byte array.
36-
| `datasetInfo` | no | Your way to customize to how to create BigQuery dataset.
37+
| `rowMapper` | no | Your own converter that specifies how to convert input CSV / JSON to a byte array.
38+
| `datasetInfo` | no | Your way to customize how to create BigQuery dataset.
3739
| `jobConsumer` | no | Your custom handler for BigQuery Job provided by BigQuery Java Library.
38-
|===
40+
|===

spring-batch-bigquery/pom.xml

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
3-
~ Copyright 2002-2023 the original author or authors.
3+
~ Copyright 2002-2024 the original author or authors.
44
~
55
~ Licensed under the Apache License, Version 2.0 (the "License");
66
~ you may not use this file except in compliance with the License.
@@ -13,7 +13,9 @@
1313
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
~ See the License for the specific language governing permissions and
1515
~ limitations under the License.
16-
--><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
16+
--><project xmlns="http://maven.apache.org/POM/4.0.0"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
1719

1820
<modelVersion>4.0.0</modelVersion>
1921

@@ -51,50 +53,45 @@
5153

5254
<!-- Dependent on Spring Batch core -->
5355
<java.version>17</java.version>
54-
<logback.version>1.4.14</logback.version>
56+
<logback.version>1.5.7</logback.version>
5557
</properties>
5658

5759
<dependencies>
5860
<dependency>
5961
<groupId>org.springframework.batch</groupId>
6062
<artifactId>spring-batch-core</artifactId>
61-
<version>5.1.0</version>
63+
<version>5.1.2</version>
6264
</dependency>
6365

6466
<dependency>
6567
<groupId>com.google.cloud</groupId>
6668
<artifactId>google-cloud-bigquery</artifactId>
67-
<version>2.35.0</version>
69+
<version>2.42.0</version>
6870
</dependency>
6971
<dependency>
7072
<groupId>com.fasterxml.jackson.dataformat</groupId>
7173
<artifactId>jackson-dataformat-csv</artifactId>
72-
<version>2.16.0</version>
74+
<version>2.17.2</version>
7375
</dependency>
7476

7577
<dependency>
7678
<groupId>org.apache.commons</groupId>
7779
<artifactId>commons-lang3</artifactId>
78-
<version>3.14.0</version>
79-
</dependency>
80-
<dependency>
81-
<groupId>org.apache.commons</groupId>
82-
<artifactId>commons-collections4</artifactId>
83-
<version>4.4</version>
80+
<version>3.16.0</version>
8481
</dependency>
8582

8683

8784
<!-- Test Dependencies -->
8885
<dependency>
8986
<groupId>org.junit.jupiter</groupId>
9087
<artifactId>junit-jupiter-api</artifactId>
91-
<version>5.10.1</version>
88+
<version>5.11.0</version>
9289
<scope>test</scope>
9390
</dependency>
9491
<dependency>
9592
<groupId>org.mockito</groupId>
9693
<artifactId>mockito-core</artifactId>
97-
<version>5.8.0</version>
94+
<version>5.12.0</version>
9895
<scope>test</scope>
9996
</dependency>
10097
<dependency>
@@ -112,7 +109,7 @@
112109
<dependency>
113110
<groupId>org.slf4j</groupId>
114111
<artifactId>slf4j-api</artifactId>
115-
<version>2.0.9</version>
112+
<version>2.0.16</version>
116113
<scope>test</scope>
117114
</dependency>
118115

@@ -125,7 +122,7 @@
125122
<plugin>
126123
<groupId>org.apache.maven.plugins</groupId>
127124
<artifactId>maven-compiler-plugin</artifactId>
128-
<version>3.11.0</version>
125+
<version>3.13.0</version>
129126
<configuration>
130127
<source>${java.version}</source>
131128
<target>${java.version}</target>
@@ -136,7 +133,7 @@
136133
<plugin>
137134
<groupId>org.apache.maven.plugins</groupId>
138135
<artifactId>maven-surefire-plugin</artifactId>
139-
<version>3.2.2</version>
136+
<version>3.3.1</version>
140137
<configuration>
141138
<includes>
142139
<!-- Integration tests are omitted because they are designed to be run locally -->
@@ -149,7 +146,7 @@
149146
<plugin>
150147
<groupId>org.apache.maven.plugins</groupId>
151148
<artifactId>maven-javadoc-plugin</artifactId>
152-
<version>3.6.3</version>
149+
<version>3.8.0</version>
153150
<executions>
154151
<execution>
155152
<id>attach-javadocs</id>
@@ -162,7 +159,7 @@
162159
<plugin>
163160
<groupId>org.apache.maven.plugins</groupId>
164161
<artifactId>maven-source-plugin</artifactId>
165-
<version>3.3.0</version>
162+
<version>3.3.1</version>
166163
<executions>
167164
<execution>
168165
<id>attach-sources</id>
@@ -175,4 +172,4 @@
175172
</plugins>
176173
</build>
177174

178-
</project>
175+
</project>

spring-batch-bigquery/src/main/java/org/springframework/batch/extensions/bigquery/reader/BigQueryQueryItemReader.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@
2727
import org.springframework.util.Assert;
2828

2929
import java.util.Iterator;
30-
import java.util.Objects;
3130

3231
/**
3332
* BigQuery {@link ItemReader} that accepts simple query as the input.
@@ -37,7 +36,7 @@
3736
* <p>
3837
* Also, worth mentioning that you should take into account concurrency limits.
3938
* <p>
40-
* Results of this query by default are stored in a shape of temporary table.
39+
* Results of this query by default are stored in the shape of a temporary table.
4140
*
4241
* @param <T> your DTO type
4342
* @author Volodymyr Perebykivskyi
@@ -84,7 +83,7 @@ public void setJobConfiguration(QueryJobConfiguration jobConfiguration) {
8483

8584
@Override
8685
public T read() throws Exception {
87-
if (Objects.isNull(iterator)) {
86+
if (iterator == null) {
8887
doOpen();
8988
}
9089

@@ -109,4 +108,4 @@ public void afterPropertiesSet() {
109108
Assert.notNull(this.jobConfiguration, "Job configuration must be provided");
110109
}
111110

112-
}
111+
}

spring-batch-bigquery/src/main/java/org/springframework/batch/extensions/bigquery/reader/builder/BigQueryQueryItemReaderBuilder.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,8 +24,6 @@
2424
import org.springframework.core.convert.converter.Converter;
2525
import org.springframework.util.Assert;
2626

27-
import java.util.Objects;
28-
2927
/**
3028
* A builder for {@link BigQueryQueryItemReader}.
3129
*
@@ -70,7 +68,7 @@ public BigQueryQueryItemReaderBuilder<T> query(String query) {
7068
}
7169

7270
/**
73-
* Row mapper which transforms single BigQuery row into desired type.
71+
* Row mapper which transforms single BigQuery row into a desired type.
7472
*
7573
* @param rowMapper your row mapper
7674
* @return {@link BigQueryQueryItemReaderBuilder}
@@ -94,7 +92,7 @@ public BigQueryQueryItemReaderBuilder<T> jobConfiguration(QueryJobConfiguration
9492
}
9593

9694
/**
97-
* Please do not forget about {@link BigQueryQueryItemReader#afterPropertiesSet()}.
95+
* Please remember about {@link BigQueryQueryItemReader#afterPropertiesSet()}.
9896
*
9997
* @return {@link BigQueryQueryItemReader}
10098
*/
@@ -104,14 +102,14 @@ public BigQueryQueryItemReader<T> build() {
104102
reader.setBigQuery(this.bigQuery);
105103
reader.setRowMapper(this.rowMapper);
106104

107-
if (Objects.nonNull(this.jobConfiguration)) {
108-
reader.setJobConfiguration(this.jobConfiguration);
109-
} else {
105+
if (this.jobConfiguration == null) {
110106
Assert.isTrue(StringUtils.isNotBlank(this.query), "No query provided");
111107
reader.setJobConfiguration(QueryJobConfiguration.newBuilder(this.query).build());
108+
} else {
109+
reader.setJobConfiguration(this.jobConfiguration);
112110
}
113111

114112
return reader;
115113
}
116114

117-
}
115+
}

spring-batch-bigquery/src/main/java/org/springframework/batch/extensions/bigquery/writer/BigQueryBaseItemWriter.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,7 @@ public abstract class BigQueryBaseItemWriter<T> implements ItemWriter<T> {
5454

5555
/** Logger that can be reused */
5656
protected final Log logger = LogFactory.getLog(getClass());
57+
5758
private final AtomicLong bigQueryWriteCounter = new AtomicLong();
5859

5960
/**
@@ -77,7 +78,7 @@ public abstract class BigQueryBaseItemWriter<T> implements ItemWriter<T> {
7778

7879

7980
/**
80-
* Fetches table from provided configuration.
81+
* Fetches table from the provided configuration.
8182
*
8283
* @return {@link Table} that is described in {@link BigQueryBaseItemWriter#writeChannelConfig}
8384
*/
@@ -123,7 +124,7 @@ public void setBigQuery(BigQuery bigQuery) {
123124

124125
@Override
125126
public void write(Chunk<? extends T> chunk) throws Exception {
126-
if (BooleanUtils.isFalse(chunk.isEmpty())) {
127+
if (!chunk.isEmpty()) {
127128
List<? extends T> items = chunk.getItems();
128129
doInitializeProperties(items);
129130

@@ -147,8 +148,8 @@ private ByteBuffer mapDataToBigQueryFormat(List<? extends T> items) throws IOExc
147148
}
148149

149150
/*
150-
* It is extremely important to create larger ByteBuffer,
151-
* if you call TableDataWriteChannel too many times, it leads to BigQuery exceptions.
151+
* It is extremely important to create larger ByteBuffer.
152+
* If you call TableDataWriteChannel too many times, it leads to BigQuery exceptions.
152153
*/
153154
byteBuffer = ByteBuffer.wrap(outputStream.toByteArray());
154155
}
@@ -170,9 +171,9 @@ private void doWriteDataToBigQuery(ByteBuffer byteBuffer) throws IOException {
170171
finally {
171172
String logMessage = "Write operation submitted: " + bigQueryWriteCounter.incrementAndGet();
172173

173-
if (Objects.nonNull(writeChannel)) {
174+
if (writeChannel != null) {
174175
logMessage += " -- Job ID: " + writeChannel.getJob().getJobId().getJob();
175-
if (Objects.nonNull(this.jobConsumer)) {
176+
if (this.jobConsumer != null) {
176177
this.jobConsumer.accept(writeChannel.getJob());
177178
}
178179
}
@@ -212,7 +213,7 @@ protected void baseAfterPropertiesSet(Supplier<Void> formatSpecificChecks) {
212213
Assert.notNull(this.writeChannelConfig.getFormat(), "Data format must be provided");
213214

214215
String dataset = this.writeChannelConfig.getDestinationTable().getDataset();
215-
if (Objects.isNull(this.datasetInfo)) {
216+
if (this.datasetInfo == null) {
216217
this.datasetInfo = DatasetInfo.newBuilder(dataset).build();
217218
}
218219

@@ -228,13 +229,11 @@ private void createDataset() {
228229
TableId tableId = this.writeChannelConfig.getDestinationTable();
229230
String datasetToCheck = tableId.getDataset();
230231

231-
if (Objects.nonNull(datasetToCheck)) {
232+
if (datasetToCheck != null) {
232233
Dataset foundDataset = this.bigQuery.getDataset(datasetToCheck);
233234

234-
if (Objects.isNull(foundDataset)) {
235-
if (Objects.nonNull(this.datasetInfo)) {
236-
this.bigQuery.create(this.datasetInfo);
237-
}
235+
if (foundDataset == null && this.datasetInfo != null) {
236+
this.bigQuery.create(this.datasetInfo);
238237
}
239238
}
240239
}
@@ -264,7 +263,7 @@ private boolean isDatastore() {
264263
}
265264

266265
/**
267-
* Schema can be computed on BigQuery side during upload,
266+
* Schema can be computed on the BigQuery side during upload,
268267
* so it is good to know when schema is supplied by user manually.
269268
*
270269
* @param table BigQuery table
@@ -287,12 +286,12 @@ protected boolean tableHasDefinedSchema(Table table) {
287286
protected abstract void doInitializeProperties(List<? extends T> items);
288287

289288
/**
290-
* Converts chunk into byte array.
289+
* Converts chunk into a byte array.
291290
* Each data type should be converted with respect to its specification.
292291
*
293292
* @param items current chunk
294293
* @return {@link List<byte[]>} converted list of byte arrays
295294
*/
296295
protected abstract List<byte[]> convertObjectsToByteArrays(List<? extends T> items);
297296

298-
}
297+
}

0 commit comments

Comments
 (0)