|
| 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_set_hivepartitioningoptions] |
| 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.ExternalTableDefinition; |
| 24 | +import com.google.cloud.bigquery.FormatOptions; |
| 25 | +import com.google.cloud.bigquery.HivePartitioningOptions; |
| 26 | +import com.google.cloud.bigquery.TableId; |
| 27 | +import com.google.cloud.bigquery.TableInfo; |
| 28 | + |
| 29 | +// Sample to create external table using hive partitioning |
| 30 | +public class SetHivePartitioningOptions { |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 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/hive-partitioning-samples/customlayout/*"; |
| 37 | + String sourceUriPrefix = |
| 38 | + "gs://cloud-samples-data/bigquery/hive-partitioning-samples/customlayout/{pkey:STRING}/"; |
| 39 | + setHivePartitioningOptions(datasetName, tableName, sourceUriPrefix, sourceUri); |
| 40 | + } |
| 41 | + |
| 42 | + public static void setHivePartitioningOptions( |
| 43 | + String datasetName, String tableName, String sourceUriPrefix, String sourceUri) { |
| 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 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 48 | + |
| 49 | + // Configuring partitioning options |
| 50 | + HivePartitioningOptions hivePartitioningOptions = |
| 51 | + HivePartitioningOptions.newBuilder() |
| 52 | + .setMode("CUSTOM") |
| 53 | + .setRequirePartitionFilter(true) |
| 54 | + .setSourceUriPrefix(sourceUriPrefix) |
| 55 | + .build(); |
| 56 | + |
| 57 | + TableId tableId = TableId.of(datasetName, tableName); |
| 58 | + ExternalTableDefinition customTable = |
| 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 (BigQueryException e) { |
| 66 | + System.out.println("External table was not created" + e.toString()); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +// [END bigquery_set_hivepartitioningoptions] |
0 commit comments