3131 * @param <T> your DTO type
3232 * @author Volodymyr Perebykivskyi
3333 * @since 0.2.0
34- * @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>
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/BigQueryBatchQueryItemReaderBuilderTests.java">Examples</a>
34+ * @see <a href=
35+ * "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+ * @see <a href=
37+ * "https://github.com/spring-projects/spring-batch-extensions/tree/main/spring-batch-bigquery/src/test/java/org/springframework/batch/extensions/bigquery/unit/reader/builder/BigQueryBatchQueryItemReaderBuilderTests.java">Examples</a>
3638 */
3739public class BigQueryQueryItemReaderBuilder <T > {
3840
39- private BigQuery bigQuery ;
40- private String query ;
41- private Converter < FieldValueList , T > rowMapper ;
42- private QueryJobConfiguration jobConfiguration ;
43- private Class < T > targetType ;
44-
45- /**
46- * BigQuery service, responsible for API calls.
47- *
48- * @param bigQuery BigQuery service
49- * @return {@link BigQueryQueryItemReaderBuilder}
50- * @see BigQueryQueryItemReader#setBigQuery(BigQuery)
51- */
52- public BigQueryQueryItemReaderBuilder < T > bigQuery ( final BigQuery bigQuery ) {
53- this . bigQuery = bigQuery ;
54- return this ;
55- }
56-
57- /**
58- * Schema of the query: {@code SELECT <column> FROM <dataset>.<table>}.
59- * <p>
60- * It is really recommended to use {@code LIMIT n}
61- * because BigQuery charges you for the amount of data that is being processed .
62- *
63- * @param query your query to run
64- * @return {@link BigQueryQueryItemReaderBuilder}
65- * @see BigQueryQueryItemReader#setJobConfiguration(QueryJobConfiguration)
66- */
67- public BigQueryQueryItemReaderBuilder < T > query ( final String query ) {
68- this . query = query ;
69- return this ;
70- }
71-
72- /**
73- * Row mapper which transforms single BigQuery row into a desired type.
74- *
75- * @param rowMapper your row mapper
76- * @return {@link BigQueryQueryItemReaderBuilder}
77- * @see BigQueryQueryItemReader#setRowMapper(Converter)
78- */
79- public BigQueryQueryItemReaderBuilder < T > rowMapper ( final Converter < FieldValueList , T > rowMapper ) {
80- this . rowMapper = rowMapper ;
81- return this ;
82- }
83-
84- /**
85- * Specifies query to run, destination table, etc.
86- *
87- * @param jobConfiguration BigQuery job configuration
88- * @return {@link BigQueryQueryItemReaderBuilder}
89- * @see BigQueryQueryItemReader#setJobConfiguration(QueryJobConfiguration)
90- */
91- public BigQueryQueryItemReaderBuilder <T > jobConfiguration (final QueryJobConfiguration jobConfiguration ) {
92- this .jobConfiguration = jobConfiguration ;
93- return this ;
94- }
95-
96- /**
97- * Specifies a target type which will be used as a result.
98- * Only needed when {@link BigQueryQueryItemReaderBuilder#rowMapper} is not provided.
99- * Take into account that only {@link Class#isRecord()} supported.
100- *
101- * @param targetType a {@link Class} that represent desired type
102- * @return {@link BigQueryQueryItemReaderBuilder}
103- */
104- public BigQueryQueryItemReaderBuilder < T > targetType ( final Class < T > targetType ) {
105- this . targetType = targetType ;
106- return this ;
107- }
108-
109- /**
110- * Please remember about {@link BigQueryQueryItemReader#afterPropertiesSet()}.
111- *
112- * @return {@link BigQueryQueryItemReader}
113- */
114- public BigQueryQueryItemReader < T > build () {
115- final BigQueryQueryItemReader < T > reader = new BigQueryQueryItemReader <>( );
116-
117- reader . setBigQuery (this .bigQuery == null ? BigQueryOptions . getDefaultInstance (). getService () : this . bigQuery );
118-
119- if (this .rowMapper == null ) {
120- Assert . notNull (this .targetType , "No target type provided" );
121- Assert . isTrue ( this . targetType . isRecord (), "Only Java record supported" );
122- reader . setRowMapper ( new RecordMapper < T >(). generateMapper ( this . targetType ));
123- } else {
124- reader . setRowMapper ( this . rowMapper );
125- }
126-
127- if ( this .jobConfiguration == null ) {
128- Assert . isTrue ( StringUtils . hasText (this .query ), "No query provided" );
129- reader . setJobConfiguration ( QueryJobConfiguration . newBuilder ( this . query ). build ());
130- } else {
131- reader .setJobConfiguration (this .jobConfiguration );
132- }
133-
134- return reader ;
135- }
41+ private BigQuery bigQuery ;
42+
43+ private String query ;
44+
45+ private Converter < FieldValueList , T > rowMapper ;
46+
47+ private QueryJobConfiguration jobConfiguration ;
48+
49+ private Class < T > targetType ;
50+
51+ /**
52+ * BigQuery service, responsible for API calls.
53+ * @param bigQuery BigQuery service
54+ * @return {@link BigQueryQueryItemReaderBuilder}
55+ * @see BigQueryQueryItemReader#setBigQuery(BigQuery)
56+ */
57+ public BigQueryQueryItemReaderBuilder < T > bigQuery ( final BigQuery bigQuery ) {
58+ this . bigQuery = bigQuery ;
59+ return this ;
60+ }
61+
62+ /**
63+ * Schema of the query: {@code SELECT column FROM dataset.table} .
64+ * <p>
65+ * It is really recommended to use {@code LIMIT n} because BigQuery charges you for
66+ * the amount of data that is being processed.
67+ * @param query your query to run
68+ * @return {@link BigQueryQueryItemReaderBuilder}
69+ * @see BigQueryQueryItemReader#setJobConfiguration(QueryJobConfiguration)
70+ */
71+ public BigQueryQueryItemReaderBuilder < T > query ( final String query ) {
72+ this . query = query ;
73+ return this ;
74+ }
75+
76+ /* *
77+ * Row mapper which transforms single BigQuery row into a desired type.
78+ * @param rowMapper your row mapper
79+ * @return {@link BigQueryQueryItemReaderBuilder}
80+ * @see BigQueryQueryItemReader#setRowMapper(Converter)
81+ */
82+ public BigQueryQueryItemReaderBuilder < T > rowMapper ( final Converter < FieldValueList , T > rowMapper ) {
83+ this . rowMapper = rowMapper ;
84+ return this ;
85+ }
86+
87+ /**
88+ * Specifies query to run, destination table, etc.
89+ * @param jobConfiguration BigQuery job configuration
90+ * @return {@link BigQueryQueryItemReaderBuilder}
91+ * @see BigQueryQueryItemReader#setJobConfiguration(QueryJobConfiguration)
92+ */
93+ public BigQueryQueryItemReaderBuilder <T > jobConfiguration (final QueryJobConfiguration jobConfiguration ) {
94+ this .jobConfiguration = jobConfiguration ;
95+ return this ;
96+ }
97+
98+ /**
99+ * Specifies a target type which will be used as a result. Only needed when
100+ * {@link BigQueryQueryItemReaderBuilder#rowMapper} is not provided. Take into account
101+ * that only {@link Class#isRecord()} supported.
102+ * @param targetType a {@link Class} that represent desired type
103+ * @return {@link BigQueryQueryItemReaderBuilder}
104+ */
105+ public BigQueryQueryItemReaderBuilder < T > targetType ( final Class < T > targetType ) {
106+ this . targetType = targetType ;
107+ return this ;
108+ }
109+
110+ /**
111+ * Please remember about {@link BigQueryQueryItemReader#afterPropertiesSet()}.
112+ * @return {@link BigQueryQueryItemReader}
113+ */
114+ public BigQueryQueryItemReader < T > build () {
115+ final BigQueryQueryItemReader < T > reader = new BigQueryQueryItemReader <>();
116+
117+ reader . setBigQuery ( this . bigQuery == null ? BigQueryOptions . getDefaultInstance (). getService () : this . bigQuery );
118+
119+ if (this .rowMapper == null ) {
120+ Assert . notNull ( this . targetType , "No target type provided" );
121+ Assert . isTrue (this .targetType . isRecord (), "Only Java record supported" );
122+ reader . setRowMapper ( new RecordMapper < T >(). generateMapper (this .targetType ) );
123+ }
124+ else {
125+ reader . setRowMapper ( this . rowMapper );
126+ }
127+
128+ if ( this . jobConfiguration == null ) {
129+ Assert . isTrue ( StringUtils . hasText ( this .query ), "No query provided" );
130+ reader . setJobConfiguration ( QueryJobConfiguration . newBuilder (this .query ). build () );
131+ }
132+ else {
133+ reader .setJobConfiguration (this .jobConfiguration );
134+ }
135+
136+ return reader ;
137+ }
136138
137139}
0 commit comments