Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3994be6

Browse files
author
Praful Makani
authoredNov 24, 2020
docs(samples): add create external table using hivepartitioningoptions (#969)
1 parent 08249bc commit 3994be6

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
public class SetHivePartitioningOptionsIT {
33+
34+
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
35+
private final Logger log = Logger.getLogger(this.getClass().getName());
36+
private String tableName;
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
private PrintStream originalPrintStream;
40+
41+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
42+
43+
private static String requireEnvVar(String varName) {
44+
String value = System.getenv(varName);
45+
assertNotNull(
46+
"Environment variable " + varName + " is required to perform these tests.",
47+
System.getenv(varName));
48+
return value;
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("BIGQUERY_DATASET_NAME");
54+
}
55+
56+
@Before
57+
public void setUp() {
58+
// Create a test table
59+
tableName = "SET_HIVEPARTITIONINGOPTIONS_FROM_GCS_TEST_" + ID;
60+
bout = new ByteArrayOutputStream();
61+
out = new PrintStream(bout);
62+
originalPrintStream = System.out;
63+
System.setOut(out);
64+
}
65+
66+
@After
67+
public void tearDown() {
68+
// Clean up
69+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
70+
// restores print statements in the original method
71+
System.out.flush();
72+
System.setOut(originalPrintStream);
73+
log.log(Level.INFO, bout.toString());
74+
}
75+
76+
@Test
77+
public void testSetHivePartitioningOptions() {
78+
String sourceUri = "gs://cloud-samples-data/bigquery/hive-partitioning-samples/customlayout/*";
79+
String sourceUriPrefix =
80+
"gs://cloud-samples-data/bigquery/hive-partitioning-samples/customlayout/{pkey:STRING}/";
81+
SetHivePartitioningOptions.setHivePartitioningOptions(
82+
BIGQUERY_DATASET_NAME, tableName, sourceUriPrefix, sourceUri);
83+
assertThat(bout.toString()).contains("External table created using hivepartitioningoptions");
84+
}
85+
}

0 commit comments

Comments
 (0)
Failed to load comments.