Skip to content

Commit 64bed25

Browse files
author
Praful Makani
authored
docs(samples): add query on external table from gcs (#588)
1 parent 55cd52c commit 64bed25

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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_query_external_gcs_perm]
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.ExternalTableDefinition;
25+
import com.google.cloud.bigquery.Field;
26+
import com.google.cloud.bigquery.QueryJobConfiguration;
27+
import com.google.cloud.bigquery.Schema;
28+
import com.google.cloud.bigquery.StandardSQLTypeName;
29+
import com.google.cloud.bigquery.TableId;
30+
import com.google.cloud.bigquery.TableInfo;
31+
import com.google.cloud.bigquery.TableResult;
32+
33+
// Sample to queries an external data source using a permanent table
34+
public class QueryExternalGCSPerm {
35+
36+
public static void runQueryExternalGCSPerm() {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String datasetName = "MY_DATASET_NAME";
39+
String tableName = "MY_TABLE_NAME";
40+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";
41+
Schema schema =
42+
Schema.of(
43+
Field.of("name", StandardSQLTypeName.STRING),
44+
Field.of("post_abbr", StandardSQLTypeName.STRING));
45+
String query =
46+
String.format("SELECT * FROM %s.%s WHERE name LIKE 'W%%'", datasetName, tableName);
47+
queryExternalGCSPerm(datasetName, tableName, sourceUri, schema, query);
48+
}
49+
50+
public static void queryExternalGCSPerm(
51+
String datasetName, String tableName, String sourceUri, Schema schema, String query) {
52+
try {
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests.
55+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
56+
57+
// Skip header row in the file.
58+
CsvOptions csvOptions = CsvOptions.newBuilder().setSkipLeadingRows(1).build();
59+
60+
TableId tableId = TableId.of(datasetName, tableName);
61+
// Create a permanent table linked to the GCS file
62+
ExternalTableDefinition externalTable =
63+
ExternalTableDefinition.newBuilder(sourceUri, csvOptions).setSchema(schema).build();
64+
bigquery.create(TableInfo.of(tableId, externalTable));
65+
66+
// Example query to find states starting with 'W'
67+
TableResult results = bigquery.query(QueryJobConfiguration.of(query));
68+
69+
results
70+
.iterateAll()
71+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
72+
73+
System.out.println("Query on external permanent table performed successfully.");
74+
} catch (BigQueryException | InterruptedException e) {
75+
System.out.println("Query not performed \n" + e.toString());
76+
}
77+
}
78+
}
79+
// [END bigquery_query_external_gcs_perm]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class QueryExternalGCSPermIT {
34+
35+
private String tableName;
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
40+
41+
private static String requireEnvVar(String varName) {
42+
String value = System.getenv(varName);
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
return value;
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar("BIGQUERY_DATASET_NAME");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
bout = new ByteArrayOutputStream();
57+
out = new PrintStream(bout);
58+
System.setOut(out);
59+
60+
// Create a test table
61+
tableName = "EXTERNAL_CSV_TABLE_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8);
62+
bout = new ByteArrayOutputStream();
63+
out = new PrintStream(bout);
64+
System.setOut(out);
65+
}
66+
67+
@After
68+
public void tearDown() {
69+
// Clean up
70+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
71+
System.setOut(null);
72+
}
73+
74+
@Test
75+
public void testQueryExternalGCSPerm() {
76+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";
77+
Schema schema =
78+
Schema.of(
79+
Field.of("name", StandardSQLTypeName.STRING),
80+
Field.of("post_abbr", StandardSQLTypeName.STRING));
81+
String query =
82+
String.format(
83+
"SELECT * FROM %s.%s WHERE name LIKE 'W%%'", BIGQUERY_DATASET_NAME, tableName);
84+
QueryExternalGCSPerm.queryExternalGCSPerm(
85+
BIGQUERY_DATASET_NAME, tableName, sourceUri, schema, query);
86+
assertThat(bout.toString())
87+
.contains("Query on external permanent table performed successfully.");
88+
}
89+
}

0 commit comments

Comments
 (0)