|
| 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_orc] |
| 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 load ORC data from Cloud Storage into a new BigQuery table |
| 33 | +public class LoadOrcFromGCS { |
| 34 | + |
| 35 | + public static void runLoadOrcFromGCS() { |
| 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.orc"; |
| 40 | + Schema schema = |
| 41 | + Schema.of( |
| 42 | + Field.of("name", StandardSQLTypeName.STRING), |
| 43 | + Field.of("post_abbr", StandardSQLTypeName.STRING)); |
| 44 | + loadOrcFromGCS(datasetName, tableName, sourceUri, schema); |
| 45 | + } |
| 46 | + |
| 47 | + public static void loadOrcFromGCS( |
| 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, FormatOptions.orc()) |
| 57 | + .setSchema(schema) |
| 58 | + .build(); |
| 59 | + |
| 60 | + // Load data from a GCS ORC file into the table |
| 61 | + Job job = bigquery.create(JobInfo.of(loadConfig)); |
| 62 | + // Blocks until this load table job completes its execution, either failing or succeeding. |
| 63 | + job = job.waitFor(); |
| 64 | + if (job.isDone() && job.getStatus().getError() == null) { |
| 65 | + System.out.println("ORC from GCS successfully added during load append job"); |
| 66 | + } else { |
| 67 | + System.out.println( |
| 68 | + "BigQuery was unable to load into the table due to an error:" |
| 69 | + + job.getStatus().getError()); |
| 70 | + } |
| 71 | + } catch (BigQueryException | InterruptedException e) { |
| 72 | + System.out.println("Column not added during load append \n" + e.toString()); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +// [END bigquery_load_table_gcs_orc] |
0 commit comments