|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.batch.extensions.bigquery.reader.builder; |
| 18 | + |
| 19 | +import com.google.cloud.bigquery.BigQuery; |
| 20 | +import com.google.cloud.bigquery.FieldValueList; |
| 21 | +import com.google.cloud.bigquery.QueryJobConfiguration; |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | +import org.springframework.batch.extensions.bigquery.reader.BigQueryInteractiveQueryItemReader; |
| 24 | +import org.springframework.core.convert.converter.Converter; |
| 25 | +import org.springframework.util.Assert; |
| 26 | + |
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +/** |
| 30 | + * A builder for {@link BigQueryInteractiveQueryItemReader}. |
| 31 | + * |
| 32 | + * @param <T> your DTO type |
| 33 | + * @author Volodymyr Perebykivskyi |
| 34 | + * @since 0.2.0 |
| 35 | + * @see <a href="https://github.com/spring-projects/spring-batch-extensions/tree/main/spring-batch-bigquery/src/test/java/org/springframework/batch/extensions/bigquery/unit/reader/builder/BigQueryInteractiveQueryItemReaderBuilderTests.java">Examples</a> |
| 36 | + */ |
| 37 | +public class BigQueryInteractiveQueryItemReaderBuilder<T> { |
| 38 | + |
| 39 | + private BigQuery bigQuery; |
| 40 | + private String query; |
| 41 | + private Converter<FieldValueList, T> rowMapper; |
| 42 | + private QueryJobConfiguration jobConfiguration; |
| 43 | + |
| 44 | + /** |
| 45 | + * BigQuery service, responsible for API calls. |
| 46 | + * |
| 47 | + * @param bigQuery BigQuery service |
| 48 | + * @return {@link BigQueryInteractiveQueryItemReaderBuilder} |
| 49 | + * @see BigQueryInteractiveQueryItemReader#setBigQuery(BigQuery) |
| 50 | + */ |
| 51 | + public BigQueryInteractiveQueryItemReaderBuilder<T> bigQuery(BigQuery bigQuery) { |
| 52 | + this.bigQuery = bigQuery; |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Schema of the query: {@code SELECT <column> FROM <dataset>.<table>}. |
| 58 | + * <p> |
| 59 | + * It is really recommended to use {@code LIMIT n} |
| 60 | + * because BigQuery charges you for the amount of data that is being processed. |
| 61 | + * |
| 62 | + * @param query your query to run |
| 63 | + * @return {@link BigQueryInteractiveQueryItemReaderBuilder} |
| 64 | + * @see BigQueryInteractiveQueryItemReader#setJobConfiguration(QueryJobConfiguration) |
| 65 | + */ |
| 66 | + public BigQueryInteractiveQueryItemReaderBuilder<T> query(String query) { |
| 67 | + this.query = query; |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Row mapper which transforms single BigQuery row into desired type. |
| 73 | + * |
| 74 | + * @param rowMapper your row mapper |
| 75 | + * @return {@link BigQueryInteractiveQueryItemReaderBuilder} |
| 76 | + * @see BigQueryInteractiveQueryItemReader#setRowMapper(Converter) |
| 77 | + */ |
| 78 | + public BigQueryInteractiveQueryItemReaderBuilder<T> rowMapper(Converter<FieldValueList, T> rowMapper) { |
| 79 | + this.rowMapper = rowMapper; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Specifies query to run, destination table, etc. |
| 85 | + * |
| 86 | + * @param jobConfiguration BigQuery job configuration |
| 87 | + * @return {@link BigQueryInteractiveQueryItemReaderBuilder} |
| 88 | + * @see BigQueryInteractiveQueryItemReader#setJobConfiguration(QueryJobConfiguration) |
| 89 | + */ |
| 90 | + public BigQueryInteractiveQueryItemReaderBuilder<T> jobConfiguration(QueryJobConfiguration jobConfiguration) { |
| 91 | + this.jobConfiguration = jobConfiguration; |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Please do not forget about {@link BigQueryInteractiveQueryItemReader#afterPropertiesSet()}. |
| 97 | + * |
| 98 | + * @return {@link BigQueryInteractiveQueryItemReader} |
| 99 | + */ |
| 100 | + public BigQueryInteractiveQueryItemReader<T> build() { |
| 101 | + BigQueryInteractiveQueryItemReader<T> reader = new BigQueryInteractiveQueryItemReader<>(); |
| 102 | + |
| 103 | + reader.setBigQuery(this.bigQuery); |
| 104 | + reader.setRowMapper(this.rowMapper); |
| 105 | + |
| 106 | + if (Objects.nonNull(this.jobConfiguration)) { |
| 107 | + reader.setJobConfiguration(this.jobConfiguration); |
| 108 | + } else { |
| 109 | + Assert.isTrue(StringUtils.isNotBlank(this.query), "No query provided"); |
| 110 | + reader.setJobConfiguration(QueryJobConfiguration.newBuilder(this.query).build()); |
| 111 | + } |
| 112 | + |
| 113 | + return reader; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments