|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 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 | + * http://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 com.example.bigquery; |
| 18 | + |
| 19 | +// [START bigquery_relax_column_load_append] |
| 20 | +import com.google.cloud.bigquery.BigQuery; |
| 21 | +import com.google.cloud.bigquery.BigQueryException; |
| 22 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 23 | +import com.google.cloud.bigquery.CsvOptions; |
| 24 | +import com.google.cloud.bigquery.Field; |
| 25 | +import com.google.cloud.bigquery.Job; |
| 26 | +import com.google.cloud.bigquery.JobInfo; |
| 27 | +import com.google.cloud.bigquery.LoadJobConfiguration; |
| 28 | +import com.google.cloud.bigquery.Schema; |
| 29 | +import com.google.cloud.bigquery.StandardSQLTypeName; |
| 30 | +import com.google.cloud.bigquery.Table; |
| 31 | +import com.google.cloud.bigquery.TableId; |
| 32 | +import com.google.common.collect.ImmutableList; |
| 33 | + |
| 34 | +// Sample to append relax column in a table. |
| 35 | +public class RelaxColumnLoadAppend { |
| 36 | + |
| 37 | + public static void main(String[] args) { |
| 38 | + // TODO(developer): Replace these variables before running the sample. |
| 39 | + String datasetName = "MY_DATASET_NAME"; |
| 40 | + String tableName = "MY_TABLE_NAME"; |
| 41 | + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv"; |
| 42 | + relaxColumnLoadAppend(datasetName, tableName, sourceUri); |
| 43 | + } |
| 44 | + |
| 45 | + public static void relaxColumnLoadAppend(String datasetName, String tableName, String sourceUri) { |
| 46 | + try { |
| 47 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 48 | + // once, and can be reused for multiple requests. |
| 49 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 50 | + |
| 51 | + // Retrieve destination table reference |
| 52 | + Table table = bigquery.getTable(TableId.of(datasetName, tableName)); |
| 53 | + |
| 54 | + // column as a 'REQUIRED' field. |
| 55 | + Field name = |
| 56 | + Field.newBuilder("name", StandardSQLTypeName.STRING).setMode(Field.Mode.REQUIRED).build(); |
| 57 | + Field postAbbr = |
| 58 | + Field.newBuilder("post_abbr", StandardSQLTypeName.STRING) |
| 59 | + .setMode(Field.Mode.REQUIRED) |
| 60 | + .build(); |
| 61 | + Schema schema = Schema.of(name, postAbbr); |
| 62 | + |
| 63 | + // Skip header row in the file. |
| 64 | + CsvOptions csvOptions = CsvOptions.newBuilder().setSkipLeadingRows(1).build(); |
| 65 | + |
| 66 | + // Set job options |
| 67 | + LoadJobConfiguration loadConfig = |
| 68 | + LoadJobConfiguration.newBuilder(table.getTableId(), sourceUri) |
| 69 | + .setSchema(schema) |
| 70 | + .setFormatOptions(csvOptions) |
| 71 | + .setSchemaUpdateOptions( |
| 72 | + ImmutableList.of(JobInfo.SchemaUpdateOption.ALLOW_FIELD_RELAXATION)) |
| 73 | + .setWriteDisposition(JobInfo.WriteDisposition.WRITE_APPEND) |
| 74 | + .build(); |
| 75 | + |
| 76 | + // Create a load job and wait for it to complete. |
| 77 | + Job job = bigquery.create(JobInfo.of(loadConfig)); |
| 78 | + job = job.waitFor(); |
| 79 | + // Check the job's status for errors |
| 80 | + if (job.isDone() && job.getStatus().getError() == null) { |
| 81 | + System.out.println("Relax column append successfully loaded in a table"); |
| 82 | + } else { |
| 83 | + System.out.println( |
| 84 | + "BigQuery was unable to load into the table due to an error:" |
| 85 | + + job.getStatus().getError()); |
| 86 | + } |
| 87 | + } catch (BigQueryException | InterruptedException e) { |
| 88 | + System.out.println("Column not added during load append \n" + e.toString()); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | +// [END bigquery_relax_column_load_append] |
0 commit comments