|
| 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_avro] |
| 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.FormatOptions; |
| 24 | +import com.google.cloud.bigquery.Job; |
| 25 | +import com.google.cloud.bigquery.JobInfo; |
| 26 | +import com.google.cloud.bigquery.LoadJobConfiguration; |
| 27 | +import com.google.cloud.bigquery.TableId; |
| 28 | + |
| 29 | +// Sample to load Avro data from Cloud Storage into a new BigQuery table |
| 30 | +public class LoadAvroFromGCS { |
| 31 | + |
| 32 | + public static void runLoadAvroFromGCS() { |
| 33 | + // TODO(developer): Replace these variables before running the sample. |
| 34 | + String datasetName = "MY_DATASET_NAME"; |
| 35 | + String tableName = "MY_TABLE_NAME"; |
| 36 | + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.avro"; |
| 37 | + loadAvroFromGCS(datasetName, tableName, sourceUri); |
| 38 | + } |
| 39 | + |
| 40 | + public static void loadAvroFromGCS(String datasetName, String tableName, String sourceUri) { |
| 41 | + try { |
| 42 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 43 | + // once, and can be reused for multiple requests. |
| 44 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 45 | + |
| 46 | + TableId tableId = TableId.of(datasetName, tableName); |
| 47 | + LoadJobConfiguration loadConfig = |
| 48 | + LoadJobConfiguration.of(tableId, sourceUri, FormatOptions.avro()); |
| 49 | + |
| 50 | + // Load data from a GCS Avro file into the table |
| 51 | + Job job = bigquery.create(JobInfo.of(loadConfig)); |
| 52 | + // Blocks until this load table job completes its execution, either failing or succeeding. |
| 53 | + job = job.waitFor(); |
| 54 | + if (job.isDone()) { |
| 55 | + System.out.println("Avro from GCS successfully loaded in a table"); |
| 56 | + } else { |
| 57 | + System.out.println( |
| 58 | + "BigQuery was unable to load into the table due to an error:" |
| 59 | + + job.getStatus().getError()); |
| 60 | + } |
| 61 | + } catch (BigQueryException | InterruptedException e) { |
| 62 | + System.out.println("Column not added during load append \n" + e.toString()); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +// [END bigquery_load_table_gcs_avro] |
0 commit comments