|
| 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 | +packagecom.example.bigquery; |
| 18 | + |
| 19 | +// [START bigquery_set_hivepartitioningoptions] |
| 20 | +importcom.google.cloud.bigquery.BigQuery; |
| 21 | +importcom.google.cloud.bigquery.BigQueryException; |
| 22 | +importcom.google.cloud.bigquery.BigQueryOptions; |
| 23 | +importcom.google.cloud.bigquery.ExternalTableDefinition; |
| 24 | +importcom.google.cloud.bigquery.FormatOptions; |
| 25 | +importcom.google.cloud.bigquery.HivePartitioningOptions; |
| 26 | +importcom.google.cloud.bigquery.TableId; |
| 27 | +importcom.google.cloud.bigquery.TableInfo; |
| 28 | + |
| 29 | +// Sample to create external table using hive partitioning |
| 30 | +publicclassSetHivePartitioningOptions { |
| 31 | + |
| 32 | +publicstaticvoidmain(String[]args) { |
| 33 | +// TODO(developer): Replace these variables before running the sample. |
| 34 | +StringdatasetName ="MY_DATASET_NAME"; |
| 35 | +StringtableName ="MY_TABLE_NAME"; |
| 36 | +StringsourceUri ="gs://cloud-samples-data/bigquery/hive-partitioning-samples/customlayout/*"; |
| 37 | +StringsourceUriPrefix = |
| 38 | +"gs://cloud-samples-data/bigquery/hive-partitioning-samples/customlayout/{pkey:STRING}/"; |
| 39 | +setHivePartitioningOptions(datasetName,tableName,sourceUriPrefix,sourceUri); |
| 40 | + } |
| 41 | + |
| 42 | +publicstaticvoidsetHivePartitioningOptions( |
| 43 | +StringdatasetName,StringtableName,StringsourceUriPrefix,StringsourceUri) { |
| 44 | +try { |
| 45 | +// Initialize client that will be used to send requests. This client only needs to be created |
| 46 | +// once, and can be reused for multiple requests. |
| 47 | +BigQuerybigquery =BigQueryOptions.getDefaultInstance().getService(); |
| 48 | + |
| 49 | +// Configuring partitioning options |
| 50 | +HivePartitioningOptionshivePartitioningOptions = |
| 51 | +HivePartitioningOptions.newBuilder() |
| 52 | + .setMode("CUSTOM") |
| 53 | + .setRequirePartitionFilter(true) |
| 54 | + .setSourceUriPrefix(sourceUriPrefix) |
| 55 | + .build(); |
| 56 | + |
| 57 | +TableIdtableId =TableId.of(datasetName,tableName); |
| 58 | +ExternalTableDefinitioncustomTable = |
| 59 | +ExternalTableDefinition.newBuilder(sourceUri,FormatOptions.parquet()) |
| 60 | + .setAutodetect(true) |
| 61 | + .setHivePartitioningOptions(hivePartitioningOptions) |
| 62 | + .build(); |
| 63 | +bigquery.create(TableInfo.of(tableId,customTable)); |
| 64 | +System.out.println("External table created using hivepartitioningoptions"); |
| 65 | + }catch (BigQueryExceptione) { |
| 66 | +System.out.println("External table was not created" +e.toString()); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +// [END bigquery_set_hivepartitioningoptions] |