|
| 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_load_table_gcs_json_truncate] |
| 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.Field; |
| 24 | +import com.google.cloud.bigquery.FormatOptions; |
| 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.TableId; |
| 31 | + |
| 32 | +// Sample to overwrite the BigQuery table data by loading a JSON file from GCS |
| 33 | +public class LoadJsonFromGCSTruncate { |
| 34 | + |
| 35 | + public static void runLoadJsonFromGCSTruncate() { |
| 36 | + // TODO(developer): Replace these variables before running the sample. |
| 37 | + String datasetName = "MY_DATASET_NAME"; |
| 38 | + String tableName = "MY_TABLE_NAME"; |
| 39 | + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; |
| 40 | + Schema schema = |
| 41 | + Schema.of( |
| 42 | + Field.of("name", StandardSQLTypeName.STRING), |
| 43 | + Field.of("post_abbr", StandardSQLTypeName.STRING)); |
| 44 | + loadJsonFromGCSTruncate(datasetName, tableName, sourceUri, schema); |
| 45 | + } |
| 46 | + |
| 47 | + public static void loadJsonFromGCSTruncate( |
| 48 | + String datasetName, String tableName, String sourceUri, Schema schema) { |
| 49 | + try { |
| 50 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 51 | + // once, and can be reused for multiple requests. |
| 52 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 53 | + |
| 54 | + TableId tableId = TableId.of(datasetName, tableName); |
| 55 | + LoadJobConfiguration loadConfig = |
| 56 | + LoadJobConfiguration.newBuilder(tableId, sourceUri) |
| 57 | + .setFormatOptions(FormatOptions.json()) |
| 58 | + // Set the write disposition to overwrite existing table data |
| 59 | + .setWriteDisposition(JobInfo.WriteDisposition.WRITE_TRUNCATE) |
| 60 | + .setSchema(schema) |
| 61 | + .build(); |
| 62 | + |
| 63 | + // Load data from a GCS JSON file into the table |
| 64 | + Job job = bigquery.create(JobInfo.of(loadConfig)); |
| 65 | + // Blocks until this load table job completes its execution, either failing or succeeding. |
| 66 | + job = job.waitFor(); |
| 67 | + if (job.isDone()) { |
| 68 | + System.out.println("Table is successfully overwritten by JSON file loaded from GCS"); |
| 69 | + } else { |
| 70 | + System.out.println( |
| 71 | + "BigQuery was unable to load into the table due to an error:" |
| 72 | + + job.getStatus().getError()); |
| 73 | + } |
| 74 | + } catch (BigQueryException | InterruptedException e) { |
| 75 | + System.out.println("Column not added during load append \n" + e.toString()); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +// [END bigquery_load_table_gcs_json_truncate] |
0 commit comments