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