|
| 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] |
0 commit comments