|
| 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_csv_autodetect] |
| 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.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 CSV data with autodetect schema from Cloud Storage into a new BigQuery table |
| 30 | +public class LoadCsvFromGCSAutodetect { |
| 31 | + |
| 32 | + public static void runLoadCsvFromGCSAutodetect() { |
| 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.csv"; |
| 37 | + loadCsvFromGCSAutodetect(datasetName, tableName, sourceUri); |
| 38 | + } |
| 39 | + |
| 40 | + public static void loadCsvFromGCSAutodetect( |
| 41 | + String datasetName, String tableName, String sourceUri) { |
| 42 | + try { |
| 43 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 44 | + // once, and can be reused for multiple requests. |
| 45 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 46 | + |
| 47 | + TableId tableId = TableId.of(datasetName, tableName); |
| 48 | + |
| 49 | + // Skip header row in the file. |
| 50 | + CsvOptions csvOptions = CsvOptions.newBuilder().setSkipLeadingRows(1).build(); |
| 51 | + |
| 52 | + LoadJobConfiguration loadConfig = |
| 53 | + LoadJobConfiguration.newBuilder(tableId, sourceUri) |
| 54 | + .setFormatOptions(csvOptions) |
| 55 | + .setAutodetect(true) |
| 56 | + .build(); |
| 57 | + |
| 58 | + // Load data from a GCS CSV file into the table |
| 59 | + Job job = bigquery.create(JobInfo.of(loadConfig)); |
| 60 | + // Blocks until this load table job completes its execution, either failing or succeeding. |
| 61 | + job = job.waitFor(); |
| 62 | + if (job.isDone() && job.getStatus().getError() == null) { |
| 63 | + System.out.println("CSV Autodetect from GCS successfully loaded in a table"); |
| 64 | + } else { |
| 65 | + System.out.println( |
| 66 | + "BigQuery was unable to load into the table due to an error:" |
| 67 | + + job.getStatus().getError()); |
| 68 | + } |
| 69 | + } catch (BigQueryException | InterruptedException e) { |
| 70 | + System.out.println("Column not added during load append \n" + e.toString()); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +// [END bigquery_load_table_gcs_csv_autodetect] |
0 commit comments