Skip to content

Commit ef757ad

Browse files
author
Praful Makani
authored
docs(samples): add query on external temp table from gcs (#593)
1 parent 8ef8db2 commit ef757ad

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_temp]
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.TableResult;
30+
31+
// Sample to queries an external data source using a temporary table
32+
public class QueryExternalGCSTemp {
33+
34+
public static void runQueryExternalGCSTemp() {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String tableName = "MY_TABLE_NAME";
37+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";
38+
Schema schema =
39+
Schema.of(
40+
Field.of("name", StandardSQLTypeName.STRING),
41+
Field.of("post_abbr", StandardSQLTypeName.STRING));
42+
String query = String.format("SELECT * FROM %s WHERE name LIKE 'W%%'", tableName);
43+
queryExternalGCSTemp(tableName, sourceUri, schema, query);
44+
}
45+
46+
public static void queryExternalGCSTemp(
47+
String tableName, String sourceUri, Schema schema, String query) {
48+
try {
49+
// Initialize client that will be used to send requests. This client only needs to be created
50+
// once, and can be reused for multiple requests.
51+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
52+
53+
// Skip header row in the file.
54+
CsvOptions csvOptions = CsvOptions.newBuilder().setSkipLeadingRows(1).build();
55+
56+
// Configure the external data source and query job.
57+
ExternalTableDefinition externalTable =
58+
ExternalTableDefinition.newBuilder(sourceUri, csvOptions).setSchema(schema).build();
59+
QueryJobConfiguration queryConfig =
60+
QueryJobConfiguration.newBuilder(query)
61+
.addTableDefinition(tableName, externalTable)
62+
.build();
63+
64+
// Example query to find states starting with 'W'
65+
TableResult results = bigquery.query(queryConfig);
66+
67+
results
68+
.iterateAll()
69+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
70+
71+
System.out.println("Query on external temporary table performed successfully.");
72+
} catch (BigQueryException | InterruptedException e) {
73+
System.out.println("Query not performed \n" + e.toString());
74+
}
75+
}
76+
}
77+
// [END bigquery_query_external_gcs_temp]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
21+
import com.google.cloud.bigquery.Field;
22+
import com.google.cloud.bigquery.Schema;
23+
import com.google.cloud.bigquery.StandardSQLTypeName;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.util.UUID;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
public class QueryExternalGCSTempIT {
32+
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
@Before
37+
public void setUp() {
38+
bout = new ByteArrayOutputStream();
39+
out = new PrintStream(bout);
40+
System.setOut(out);
41+
}
42+
43+
@After
44+
public void tearDown() {
45+
System.setOut(null);
46+
}
47+
48+
@Test
49+
public void testQueryExternalGCSTemp() {
50+
String tableName =
51+
"EXTERNAL_CSV_TEMP_TABLE_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8);
52+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv";
53+
Schema schema =
54+
Schema.of(
55+
Field.of("name", StandardSQLTypeName.STRING),
56+
Field.of("post_abbr", StandardSQLTypeName.STRING));
57+
String query = String.format("SELECT * FROM %s WHERE name LIKE 'W%%'", tableName);
58+
QueryExternalGCSTemp.queryExternalGCSTemp(tableName, sourceUri, schema, query);
59+
assertThat(bout.toString())
60+
.contains("Query on external temporary table performed successfully.");
61+
}
62+
}

0 commit comments

Comments
 (0)