-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathBigQueryExecute.java
849 lines (746 loc) · 32.9 KB
/
BigQueryExecute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
* Copyright © 2019 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.cdap.plugin.gcp.bigquery.action;
import com.google.auth.Credentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.DatasetId;
import com.google.cloud.bigquery.EncryptionConfiguration;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.JobStatistics;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableResult;
import com.google.cloud.kms.v1.CryptoKeyName;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import dev.failsafe.Failsafe;
import dev.failsafe.FailsafeException;
import dev.failsafe.RetryPolicy;
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.annotation.Macro;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.cdap.etl.api.action.Action;
import io.cdap.cdap.etl.api.action.ActionContext;
import io.cdap.cdap.etl.common.Constants;
import io.cdap.plugin.gcp.bigquery.exception.BigQueryJobExecutionException;
import io.cdap.plugin.gcp.bigquery.sink.BigQuerySinkUtils;
import io.cdap.plugin.gcp.bigquery.util.BigQueryUtil;
import io.cdap.plugin.gcp.common.CmekUtils;
import io.cdap.plugin.gcp.common.GCPUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
/**
* This class <code>BigQueryExecute</code> executes a single Cloud BigQuery SQL.
* <p>
* The plugin provides the ability different options like choosing interactive or batch execution of sql query, setting
* of resulting dataset and table, enabling/disabling cache, specifying whether the query being executed is legacy or
* standard and retry strategy.
*/
@Plugin(type = Action.PLUGIN_TYPE)
@Name(BigQueryExecute.NAME)
@Description("Execute a Google BigQuery SQL.")
public final class BigQueryExecute extends AbstractBigQueryAction {
private static final Logger LOG = LoggerFactory.getLogger(BigQueryExecute.class);
public static final String NAME = "BigQueryExecute";
private static final String RECORDS_PROCESSED = "records.processed";
private Config config;
private static final String JOB_BACKEND_ERROR = "jobBackendError";
private static final String JOB_INTERNAL_ERROR = "jobInternalError";
private static final Set<String> RETRY_ON_REASON = ImmutableSet.of(JOB_BACKEND_ERROR, JOB_INTERNAL_ERROR);
BigQueryExecute() {
// no args constructor
}
@VisibleForTesting
BigQueryExecute(Config config) {
this.config = config;
}
@Override
public void run(ActionContext context) throws Exception {
FailureCollector collector = context.getFailureCollector();
config.validate(collector, context.getArguments().asMap());
QueryJobConfiguration.Builder builder = QueryJobConfiguration.newBuilder(config.getSql());
// Run at batch priority, which won't count toward concurrent rate limit.
if (config.getMode().equals(QueryJobConfiguration.Priority.BATCH)) {
builder.setPriority(QueryJobConfiguration.Priority.BATCH);
} else {
builder.setPriority(QueryJobConfiguration.Priority.INTERACTIVE);
}
CryptoKeyName cmekKeyName = CmekUtils.getCmekKey(config.cmekKey, context.getArguments().asMap(), collector);
collector.getOrThrowException();
// Save the results of the query to a permanent table.
String datasetName = config.getDataset();
String tableName = config.getTable();
String datasetProjectId = config.getDatasetProject();
if (config.getStoreResults() && datasetProjectId != null && datasetName != null && tableName != null) {
builder.setDestinationTable(TableId.of(datasetProjectId, datasetName, tableName));
builder.setWriteDisposition(JobInfo.WriteDisposition.valueOf(config.getWritePreference()));
}
// Enable or Disable the query cache to force live query evaluation.
if (config.shouldUseCache()) {
builder.setUseQueryCache(true);
}
// Enable legacy SQL
builder.setUseLegacySql(config.isLegacySQL());
// API request - starts the query.
Credentials credentials = config.getServiceAccount() == null ?
null : GCPUtils.loadServiceAccountCredentials(config.getServiceAccount(),
config.isServiceAccountFilePath());
BigQuery bigQuery = GCPUtils.getBigQuery(config.getProject(), credentials, config.getReadTimeout());
//create dataset to store the results if not exists
if (config.getStoreResults() && !Strings.isNullOrEmpty(datasetName) &&
!Strings.isNullOrEmpty(tableName)) {
BigQuerySinkUtils.createDatasetIfNotExists(bigQuery, DatasetId.of(datasetProjectId, datasetName),
config.getLocation(), cmekKeyName,
() -> String.format("Unable to create BigQuery dataset '%s.%s'",
datasetProjectId, datasetName));
if (cmekKeyName != null) {
builder.setDestinationEncryptionConfiguration(
EncryptionConfiguration.newBuilder().setKmsKeyName(cmekKeyName.toString()).build());
}
}
// Add labels for the BigQuery Execute job.
builder.setLabels(BigQueryUtil.getJobLabels(BigQueryUtil.BQ_JOB_TYPE_EXECUTE_TAG, config.getJobLabelKeyValue()));
QueryJobConfiguration queryConfig = builder.build();
// Exponential backoff
if (config.getRetryOnBackendError()) {
try {
executeQueryWithExponentialBackoff(bigQuery, queryConfig, context);
} catch (Throwable e) {
throw new RuntimeException(e);
}
} else {
executeQuery(bigQuery, queryConfig, context);
}
}
protected void executeQueryWithExponentialBackoff(BigQuery bigQuery,
QueryJobConfiguration queryConfig, ActionContext context)
throws Throwable {
try {
Failsafe.with(getRetryPolicy()).run(() -> executeQuery(bigQuery, queryConfig, context));
} catch (FailsafeException e) {
if (e.getCause() != null) {
throw e.getCause();
}
throw e;
}
}
private RetryPolicy<Object> getRetryPolicy() {
return RetryPolicy.builder()
.handle(BigQueryJobExecutionException.class)
.withBackoff(Duration.ofSeconds(config.getInitialRetryDuration()),
Duration.ofSeconds(config.getMaxRetryDuration()), config.getRetryMultiplier())
.withMaxRetries(config.getMaxRetryCount())
.onRetry(event -> LOG.debug("Retrying BigQuery Execute job. Retry count: {}", event.getAttemptCount()))
.onSuccess(event -> LOG.debug("BigQuery Execute job executed successfully."))
.onRetriesExceeded(event -> LOG.error("Retry limit reached for BigQuery Execute job."))
.build();
}
private void executeQuery(BigQuery bigQuery, QueryJobConfiguration queryConfig, ActionContext context)
throws InterruptedException, BigQueryJobExecutionException {
// Location must match that of the dataset(s) referenced in the query.
JobId jobId = JobId.newBuilder().setRandomJob().setLocation(config.getLocation()).build();
Job queryJob;
try {
queryJob = bigQuery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
LOG.info("Executing SQL as job {}.", jobId.getJob());
LOG.debug("The BigQuery SQL is {}", config.getSql());
// Wait for the query to complete
queryJob = queryJob.waitFor();
} catch (BigQueryException e) {
LOG.error("The query job {} failed. Error: {}", jobId.getJob(), e.getError().getMessage());
if (RETRY_ON_REASON.contains(e.getError().getReason())) {
throw new BigQueryJobExecutionException(e.getError().getMessage(), e);
}
throw new RuntimeException(e);
}
// Check for errors
if (queryJob.getStatus().getError() != null) {
// You can also look at queryJob.getStatus().getExecutionErrors() for all
// errors, not just the latest one.
LOG.error("The query job {} failed. Error: {}", jobId.getJob(), queryJob.getStatus().getError());
if (RETRY_ON_REASON.contains(queryJob.getStatus().getError().getReason())) {
throw new BigQueryJobExecutionException(queryJob.getStatus().getError().getMessage());
}
throw new RuntimeException(queryJob.getStatus().getError().getMessage());
}
TableResult queryResults = queryJob.getQueryResults();
long rows = queryResults.getTotalRows();
if (config.shouldSetAsArguments()) {
if (rows == 0 || queryResults.getSchema() == null) {
LOG.warn("The query result does not contain any row or schema, will not save the results in the arguments");
} else {
Schema schema = queryResults.getSchema();
FieldValueList firstRow = queryResults.iterateAll().iterator().next();
for (int i = 0; i < schema.getFields().size(); i++) {
Field field = schema.getFields().get(i);
String name = field.getName();
if (field.getMode().equals(Field.Mode.REPEATED)) {
LOG.warn("Field {} is an array, will not save the value in the argument", name);
continue;
}
if (field.getType().equals(LegacySQLTypeName.RECORD)) {
LOG.warn("Field {} is a record type with nested schema, will not save the value in the argument", name);
continue;
}
context.getArguments().set(name, firstRow.get(name).getStringValue());
}
}
}
context.getMetrics().gauge(RECORDS_PROCESSED, rows);
try {
recordBytesProcessedMetric(context, queryJob);
} catch (Exception exception) {
// log the exception but not fail the pipeline
LOG.warn("Exception while trying to emit bytes processed metric.",
exception);
}
}
private void recordBytesProcessedMetric(ActionContext context, Job queryJob) {
long processedBytes =
((JobStatistics.QueryStatistics) queryJob.getStatistics()).getTotalBytesProcessed();
LOG.info("Job {} processed {} bytes", queryJob.getJobId(), processedBytes);
Map<String, String> tags = new ImmutableMap.Builder<String, String>()
.put(Constants.Metrics.Tag.APP_ENTITY_TYPE, Action.PLUGIN_TYPE)
.put(Constants.Metrics.Tag.APP_ENTITY_TYPE_NAME, BigQueryExecute.NAME)
.build();
context.getMetrics().child(tags).countLong(BigQuerySinkUtils.BYTES_PROCESSED_METRIC,
processedBytes);
}
@Override
public AbstractBigQueryActionConfig getConfig() {
return config;
}
/**
* Config for the plugin.
*/
public static final class Config extends AbstractBigQueryActionConfig {
private static final String MODE = "mode";
private static final String SQL = "sql";
private static final String DATASET = "dataset";
private static final String TABLE = "table";
private static final String WRITE_PREFERENCE = "writePreference";
private static final String NAME_LOCATION = "location";
public static final String NAME_BQ_JOB_LABELS = "jobLabels";
private static final int ERROR_CODE_NOT_FOUND = 404;
private static final String STORE_RESULTS = "storeResults";
private static final String NAME_RETRY_ON_BACKEND_ERROR = "retryOnBackendError";
private static final String NAME_INITIAL_RETRY_DURATION = "initialRetryDuration";
private static final String NAME_MAX_RETRY_DURATION = "maxRetryDuration";
private static final String NAME_RETRY_MULTIPLIER = "retryMultiplier";
private static final String NAME_MAX_RETRY_COUNT = "maxRetryCount";
private static final String NAME_READ_TIMEOUT = "readTimeout";
public static final long DEFAULT_INITIAL_RETRY_DURATION_SECONDS = 1L;
public static final double DEFAULT_RETRY_MULTIPLIER = 2.0;
public static final int DEFAULT_MAX_RETRY_COUNT = 5;
// Sn = a * (1 - r^n) / (r - 1)
public static final long DEFULT_MAX_RETRY_DURATION_SECONDS = 63L;
public static final int DEFAULT_READ_TIMEOUT = 120;
public static final Set<String> VALID_WRITE_PREFERENCES = Arrays.stream(JobInfo.WriteDisposition.values())
.map(Enum::name).collect(Collectors.toSet());
@Description("Dialect of the SQL command. The value must be 'legacy' or 'standard'. " +
"If set to 'standard', the query will use BigQuery's standard SQL: " +
"https://cloud.google.com/bigquery/sql-reference/. If set to 'legacy', BigQuery's legacy SQL " +
"dialect will be used for this query.")
@Macro
private String dialect;
@Name(SQL)
@Description("SQL command to execute.")
@Macro
private String sql;
@Name(MODE)
@Description("Mode to execute the query in. The value must be 'batch' or 'interactive'. " +
"An interactive query is executed as soon as possible and counts towards the concurrent rate " +
"limit and the daily rate limit. A batch query is queued and started as soon as idle resources " +
"are available, usually within a few minutes. If the query hasn't started within 3 hours, " +
"its priority is changed to 'interactive'")
@Macro
private String mode;
@Description("Use the cache when executing the query.")
@Macro
private String useCache;
@Name(NAME_LOCATION)
@Description("Location of the job. Must match the location of the dataset specified in the query. Defaults to 'US'")
@Macro
private String location;
@Name(DATASET)
@Description("Dataset to store the query results in. If not specified, the results will not be stored.")
@Macro
@Nullable
private String dataset;
@Name(TABLE)
@Description("Table to store the query results in. If not specified, the results will not be stored.")
@Macro
@Nullable
private String table;
@Name(NAME_CMEK_KEY)
@Macro
@Nullable
@Description("The GCP customer managed encryption key (CMEK) name used to encrypt data written to the " +
"dataset or table created by the plugin to store the query results. It is only applicable when users choose to " +
"store the query results in a BigQuery table. More information can be found at " +
"https://cloud.google.com/data-fusion/docs/how-to/customer-managed-encryption-keys")
private String cmekKey;
@Description("Row as arguments. For example, if the query is " +
"'select min(id) as min_id, max(id) as max_id from my_dataset.my_table'," +
"an arguments for 'min_id' and 'max_id' will be set based on the query results. " +
"Plugins further down the pipeline can then" +
"reference these values with macros ${min_id} and ${max_id}.")
@Macro
private String rowAsArguments;
@Name(NAME_RETRY_ON_BACKEND_ERROR)
@Description("Whether to retry on backend error. Default is false.")
@Macro
@Nullable
private Boolean retryOnBackendError;
@Name(NAME_INITIAL_RETRY_DURATION)
@Description("Time taken for the first retry. Default is 1 seconds.")
@Nullable
@Macro
private Long initialRetryDuration;
@Name(NAME_MAX_RETRY_DURATION)
@Description("Maximum time in seconds retries can take. Default is 32 seconds.")
@Nullable
@Macro
private Long maxRetryDuration;
@Name(NAME_MAX_RETRY_COUNT)
@Description("Maximum number of retries allowed. Default is 5.")
@Nullable
@Macro
private Integer maxRetryCount;
@Name(NAME_RETRY_MULTIPLIER)
@Description("Multiplier for exponential backoff. Default is 2.")
@Nullable
@Macro
private Double retryMultiplier;
@Name(STORE_RESULTS)
@Nullable
@Description("Whether to store results in a BigQuery Table.")
private Boolean storeResults;
@Name(NAME_BQ_JOB_LABELS)
@Macro
@Nullable
@Description("Key value pairs to be added as labels to the BigQuery job. Keys must be unique. [job_source, type] " +
"are reserved keys and cannot be used as label keys.")
protected String jobLabelKeyValue;
@Name(NAME_READ_TIMEOUT)
@Nullable
@Macro
@Description("Timeout in seconds to read data from an established HTTP connection (Default value is 120).")
private Integer readTimeout;
@Name(WRITE_PREFERENCE)
@Nullable
@Macro
@Description("Specifies if a job should overwrite or append the existing destination table if it already exists.")
private String writePreference;
private Config(@Nullable String project, @Nullable String serviceAccountType, @Nullable String serviceFilePath,
@Nullable String serviceAccountJson, @Nullable String dataset, @Nullable String table,
@Nullable String location, @Nullable String cmekKey, @Nullable String dialect, @Nullable String sql,
@Nullable String mode, @Nullable Boolean storeResults, @Nullable String jobLabelKeyValue,
@Nullable String rowAsArguments, @Nullable Boolean retryOnBackendError,
@Nullable Long initialRetryDuration, @Nullable Long maxRetryDuration,
@Nullable Double retryMultiplier, @Nullable Integer maxRetryCount, @Nullable Integer readTimeout,
@Nullable String writePreference) {
this.project = project;
this.serviceAccountType = serviceAccountType;
this.serviceFilePath = serviceFilePath;
this.serviceAccountJson = serviceAccountJson;
this.dataset = dataset;
this.table = table;
this.location = location;
this.cmekKey = cmekKey;
this.dialect = dialect;
this.sql = sql;
this.mode = mode;
this.rowAsArguments = rowAsArguments;
this.storeResults = storeResults;
this.jobLabelKeyValue = jobLabelKeyValue;
this.retryOnBackendError = retryOnBackendError;
this.initialRetryDuration = initialRetryDuration;
this.maxRetryDuration = maxRetryDuration;
this.maxRetryCount = maxRetryCount;
this.retryMultiplier = retryMultiplier;
this.readTimeout = readTimeout;
this.writePreference = writePreference;
}
public boolean isLegacySQL() {
return dialect.equalsIgnoreCase("legacy");
}
public boolean shouldUseCache() {
return useCache.equalsIgnoreCase("true");
}
public boolean shouldSetAsArguments() {
return rowAsArguments.equalsIgnoreCase("true");
}
public String getLocation() {
return location;
}
public String getSql() {
return sql;
}
public Boolean getStoreResults() {
return storeResults == null || storeResults;
}
public String getWritePreference() {
String defaultPreference = JobInfo.WriteDisposition.WRITE_EMPTY.name();
return Strings.isNullOrEmpty(writePreference) ? defaultPreference : writePreference.toUpperCase();
}
public QueryJobConfiguration.Priority getMode() {
return QueryJobConfiguration.Priority.valueOf(mode.toUpperCase());
}
@Nullable
public String getDataset() {
return dataset;
}
@Nullable
public String getTable() {
return table;
}
@Nullable
public String getJobLabelKeyValue() {
return jobLabelKeyValue;
}
public boolean getRetryOnBackendError() {
return retryOnBackendError == null || retryOnBackendError;
}
public long getInitialRetryDuration() {
return initialRetryDuration == null ? DEFAULT_INITIAL_RETRY_DURATION_SECONDS : initialRetryDuration;
}
public long getMaxRetryDuration() {
return maxRetryDuration == null ? DEFULT_MAX_RETRY_DURATION_SECONDS : maxRetryDuration;
}
public double getRetryMultiplier() {
return retryMultiplier == null ? DEFAULT_RETRY_MULTIPLIER : retryMultiplier;
}
public int getMaxRetryCount() {
return maxRetryCount == null ? DEFAULT_MAX_RETRY_COUNT : maxRetryCount;
}
public int getReadTimeout() {
return readTimeout == null ? DEFAULT_READ_TIMEOUT : readTimeout;
}
@Override
public void validate(FailureCollector failureCollector) {
validate(failureCollector, Collections.emptyMap());
}
public void validate(FailureCollector failureCollector, Map<String, String> arguments) {
// check the mode is valid
if (!containsMacro(MODE)) {
try {
getMode();
} catch (IllegalArgumentException e) {
failureCollector.addFailure(e.getMessage(), "The mode must be 'batch' or 'interactive'.")
.withConfigProperty(MODE);
}
}
if (!containsMacro(SQL)) {
if (Strings.isNullOrEmpty(sql)) {
failureCollector.addFailure("SQL not specified.", "Please specify a SQL to execute")
.withConfigProperty(SQL);
} else {
if (tryGetProject() != null && !containsMacro(NAME_SERVICE_ACCOUNT_FILE_PATH)
&& !containsMacro(NAME_SERVICE_ACCOUNT_JSON)) {
BigQuery bigquery = getBigQuery(failureCollector);
validateSQLSyntax(failureCollector, bigquery);
}
}
}
// validates that either they are null together or not null together
if ((!containsMacro(DATASET) && !containsMacro(TABLE)) &&
(Strings.isNullOrEmpty(dataset) != Strings.isNullOrEmpty(table))) {
failureCollector.addFailure("Dataset and table must be specified together.", null)
.withConfigProperty(TABLE).withConfigProperty(DATASET);
}
if (!containsMacro(DATASET)) {
BigQueryUtil.validateDataset(dataset, DATASET, failureCollector);
}
if (!containsMacro(TABLE)) {
BigQueryUtil.validateTable(table, TABLE, failureCollector);
}
if (!containsMacro(NAME_CMEK_KEY)) {
validateCmekKey(failureCollector, arguments);
}
if (!containsMacro(NAME_BQ_JOB_LABELS)) {
validateJobLabelKeyValue(failureCollector);
}
if (!containsMacro(WRITE_PREFERENCE)) {
validateWritePreference(failureCollector, getWritePreference());
}
failureCollector.getOrThrowException();
}
void validateWritePreference(FailureCollector failureCollector, String writePreference) {
if (!VALID_WRITE_PREFERENCES.contains(writePreference)) {
failureCollector.addFailure(
String.format("Invalid write preference '%s'. Allowed values are '%s'.",
writePreference, VALID_WRITE_PREFERENCES.toString()
),
"Please provide a valid write preference."
)
.withConfigProperty(WRITE_PREFERENCE);
}
}
void validateJobLabelKeyValue(FailureCollector failureCollector) {
BigQueryUtil.validateJobLabelKeyValue(jobLabelKeyValue, failureCollector, NAME_BQ_JOB_LABELS);
// Verify retry configuration when retry on backend error is enabled and none of the retry configuration
// properties are macros.
if (!containsMacro(NAME_RETRY_ON_BACKEND_ERROR) && retryOnBackendError != null && retryOnBackendError &&
!containsMacro(NAME_INITIAL_RETRY_DURATION) && !containsMacro(NAME_MAX_RETRY_DURATION) &&
!containsMacro(NAME_MAX_RETRY_COUNT) && !containsMacro(NAME_RETRY_MULTIPLIER)) {
validateRetryConfiguration(
failureCollector, initialRetryDuration, maxRetryDuration, maxRetryCount, retryMultiplier, readTimeout
);
}
failureCollector.getOrThrowException();
}
void validateRetryConfiguration(FailureCollector failureCollector, Long initialRetryDuration,
Long maxRetryDuration, Integer maxRetryCount, Double retryMultiplier,
Integer readTimeout) {
if (initialRetryDuration != null && initialRetryDuration <= 0) {
failureCollector.addFailure("Initial retry duration must be greater than 0.",
"Please specify a valid initial retry duration.")
.withConfigProperty(NAME_INITIAL_RETRY_DURATION);
}
if (maxRetryDuration != null && maxRetryDuration <= 0) {
failureCollector.addFailure("Max retry duration must be greater than 0.",
"Please specify a valid max retry duration.")
.withConfigProperty(NAME_MAX_RETRY_DURATION);
}
if (maxRetryCount != null && maxRetryCount <= 0) {
failureCollector.addFailure("Max retry count must be greater than 0.",
"Please specify a valid max retry count.")
.withConfigProperty(NAME_MAX_RETRY_COUNT);
}
if (retryMultiplier != null && retryMultiplier <= 1) {
failureCollector.addFailure("Retry multiplier must be strictly greater than 1.",
"Please specify a valid retry multiplier.")
.withConfigProperty(NAME_RETRY_MULTIPLIER);
}
if (maxRetryDuration != null && initialRetryDuration != null && maxRetryDuration <= initialRetryDuration) {
failureCollector.addFailure("Max retry duration must be greater than initial retry duration.",
"Please specify a valid max retry duration.")
.withConfigProperty(NAME_MAX_RETRY_DURATION);
}
if (readTimeout != null && readTimeout <= 0) {
failureCollector.addFailure("Read timeout must be greater than 0.",
"Please specify a valid read timeout")
.withConfigProperty(NAME_READ_TIMEOUT);
}
}
void validateCmekKey(FailureCollector failureCollector, Map<String, String> arguments) {
CryptoKeyName cmekKeyName = CmekUtils.getCmekKey(cmekKey, arguments, failureCollector);
//these fields are needed to check if bucket exists or not and for location validation
if (cmekKeyName == null || containsMacro(DATASET) || containsMacro(NAME_LOCATION) || containsMacro(TABLE) ||
projectOrServiceAccountContainsMacro() || Strings.isNullOrEmpty(dataset) || Strings.isNullOrEmpty(table) ||
containsMacro(DATASET_PROJECT_ID)) {
return;
}
String datasetProjectId = getDatasetProject();
String datasetName = getDataset();
DatasetId datasetId = DatasetId.of(datasetProjectId, datasetName);
TableId tableId = TableId.of(datasetProjectId, datasetName, getTable());
BigQuery bigQuery = getBigQuery(failureCollector);
if (bigQuery == null) {
return;
}
CmekUtils.validateCmekKeyAndDatasetOrTableLocation(bigQuery, datasetId, tableId, cmekKeyName, location,
failureCollector);
}
public void validateSQLSyntax(FailureCollector failureCollector, BigQuery bigQuery) {
QueryJobConfiguration queryJobConfiguration = QueryJobConfiguration.newBuilder(sql).setDryRun(true).build();
try {
bigQuery.create(JobInfo.of(queryJobConfiguration));
} catch (BigQueryException e) {
final String errorMessage;
if (e.getCode() == ERROR_CODE_NOT_FOUND) {
errorMessage = String.format("Resource was not found. Please verify the resource name. If the resource " +
"will be created at runtime, then update to use a macro for the resource name. Error message received " +
"was: %s", e.getMessage());
} else {
errorMessage = e.getMessage();
}
failureCollector.addFailure(String.format("%s. Error code: %s.", errorMessage, e.getCode()),
"Please specify a valid query.")
.withConfigProperty(SQL);
}
}
private BigQuery getBigQuery(FailureCollector failureCollector) {
Credentials credentials = null;
try {
credentials = getServiceAccount() == null ?
null : GCPUtils.loadServiceAccountCredentials(getServiceAccount(), isServiceAccountFilePath());
} catch (IOException e) {
failureCollector.addFailure(e.getMessage(), null);
failureCollector.getOrThrowException();
}
return GCPUtils.getBigQuery(getProject(), credentials, getReadTimeout());
}
public static Builder builder() {
return new Builder();
}
/**
* BigQuery Execute configuration builder.
*/
public static class Builder {
private String serviceAccountType;
private String serviceFilePath;
private String serviceAccountJson;
private String project;
private String dataset;
private String table;
private String cmekKey;
private String location;
private String dialect;
private String sql;
private String mode;
private String rowAsArguments;
private Boolean storeResults;
private String jobLabelKeyValue;
private Boolean retryOnBackendError;
private Long initialRetryDuration;
private Long maxRetryDuration;
private Integer maxRetryCount;
private Double retryMultiplier;
private Integer readTimeout;
private String writePreference;
public Builder setProject(@Nullable String project) {
this.project = project;
return this;
}
public Builder setServiceAccountType(@Nullable String serviceAccountType) {
this.serviceAccountType = serviceAccountType;
return this;
}
public Builder setServiceFilePath(@Nullable String serviceFilePath) {
this.serviceFilePath = serviceFilePath;
return this;
}
public Builder setServiceAccountJson(@Nullable String serviceAccountJson) {
this.serviceAccountJson = serviceAccountJson;
return this;
}
public Builder setDataset(@Nullable String dataset) {
this.dataset = dataset;
return this;
}
public Builder setTable(@Nullable String table) {
this.table = table;
return this;
}
public Builder setCmekKey(@Nullable String cmekKey) {
this.cmekKey = cmekKey;
return this;
}
public Builder setLocation(@Nullable String location) {
this.location = location;
return this;
}
public Builder setDialect(@Nullable String dialect) {
this.dialect = dialect;
return this;
}
public Builder setMode(@Nullable String mode) {
this.mode = mode;
return this;
}
public Builder setRowAsArguments(@Nullable String rowAsArguments) {
this.rowAsArguments = rowAsArguments;
return this;
}
public Builder setSql(@Nullable String sql) {
this.sql = sql;
return this;
}
public Builder setJobLabelKeyValue(@Nullable String jobLabelKeyValue) {
this.jobLabelKeyValue = jobLabelKeyValue;
return this;
}
public Builder setRetryOnBackendError(@Nullable Boolean retryOnBackendError) {
this.retryOnBackendError = retryOnBackendError;
return this;
}
public Builder setStoreResults(@Nullable Boolean storeResults) {
this.storeResults = storeResults;
return this;
}
public Builder setInitialRetryDuration(@Nullable Long initialRetryDuration) {
this.initialRetryDuration = initialRetryDuration;
return this;
}
public Builder setMaxRetryDuration(@Nullable Long maxRetryDuration) {
this.maxRetryDuration = maxRetryDuration;
return this;
}
public Builder setMaxRetryCount(@Nullable Integer maxRetryCount) {
this.maxRetryCount = maxRetryCount;
return this;
}
public Builder setRetryMultiplier(@Nullable Double retryMultiplier) {
this.retryMultiplier = retryMultiplier;
return this;
}
public Builder setReadTimeout(@Nullable Integer readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public Builder setWritePreference(@Nullable String writePreference) {
this.writePreference = writePreference;
return this;
}
public Config build() {
return new Config(
project,
serviceAccountType,
serviceFilePath,
serviceAccountJson,
dataset,
table,
location,
cmekKey,
dialect,
sql,
mode,
storeResults,
jobLabelKeyValue,
rowAsArguments,
retryOnBackendError,
initialRetryDuration,
maxRetryDuration,
retryMultiplier,
maxRetryCount,
readTimeout,
writePreference
);
}
}
}
}