|
| 1 | +/* |
| 2 | + * Copyright 2022 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 com.google.api.gax.paging.Page; |
| 20 | +import com.google.cloud.bigquery.BigQuery; |
| 21 | +import com.google.cloud.bigquery.BigQueryError; |
| 22 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 23 | +import com.google.cloud.bigquery.Dataset; |
| 24 | +import com.google.cloud.bigquery.DatasetInfo; |
| 25 | +import com.google.cloud.bigquery.Field; |
| 26 | +import com.google.cloud.bigquery.FieldValueList; |
| 27 | +import com.google.cloud.bigquery.InsertAllRequest; |
| 28 | +import com.google.cloud.bigquery.InsertAllResponse; |
| 29 | +import com.google.cloud.bigquery.QueryJobConfiguration; |
| 30 | +import com.google.cloud.bigquery.Schema; |
| 31 | +import com.google.cloud.bigquery.StandardSQLTypeName; |
| 32 | +import com.google.cloud.bigquery.StandardTableDefinition; |
| 33 | +import com.google.cloud.bigquery.Table; |
| 34 | +import com.google.cloud.bigquery.TableDefinition; |
| 35 | +import com.google.cloud.bigquery.TableId; |
| 36 | +import com.google.cloud.bigquery.TableInfo; |
| 37 | +import com.google.cloud.bigquery.TableResult; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.UUID; |
| 42 | + |
| 43 | +/** |
| 44 | + * Sample application demonstrating BigQuery operations. |
| 45 | + * |
| 46 | + * <p>Note: This application will create a BigQuery dataset in your GCP project. You can delete this |
| 47 | + * by viewing BigQuery in Cloud Console https://console.cloud.google.com/bigquery or by uncommenting |
| 48 | + * the call to `deleteDataset(..)` made in main(). |
| 49 | + */ |
| 50 | +public class NativeImageBigquerySample { |
| 51 | + |
| 52 | + private static final String DATASET_ID = "nativeimage_test_dataset"; |
| 53 | + |
| 54 | + private static final String TABLE_ID = "nativeimage_test_table"; |
| 55 | + |
| 56 | + private static final Schema TABLE_SCHEMA = |
| 57 | + Schema.of( |
| 58 | + Field.of("id", StandardSQLTypeName.STRING), Field.of("age", StandardSQLTypeName.INT64)); |
| 59 | + |
| 60 | + /** Entrypoint to the application. */ |
| 61 | + public static void main(String[] args) throws InterruptedException { |
| 62 | + BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService(); |
| 63 | + |
| 64 | + if (!hasDataset(bigQuery, DATASET_ID)) { |
| 65 | + createDataset(bigQuery, DATASET_ID); |
| 66 | + } |
| 67 | + |
| 68 | + String tableName = TABLE_ID + "_" + UUID.randomUUID().toString().replace("-", ""); |
| 69 | + createTable(bigQuery, DATASET_ID, tableName, TABLE_SCHEMA); |
| 70 | + String testId = "TestUser-" + UUID.randomUUID().toString(); |
| 71 | + int testAge = 40; |
| 72 | + insertTestRecord(bigQuery, DATASET_ID, tableName, testId, testAge); |
| 73 | + queryTable(bigQuery, DATASET_ID, tableName); |
| 74 | + |
| 75 | + // Clean up resources. |
| 76 | + deleteTable(bigQuery, DATASET_ID, tableName); |
| 77 | + |
| 78 | + // Uncomment this to delete the created dataset. |
| 79 | + // deleteDataset(bigQuery, DATASET_ID); |
| 80 | + } |
| 81 | + |
| 82 | + static String queryTable(BigQuery bigQuery, String datasetName, String tableName) |
| 83 | + throws InterruptedException { |
| 84 | + String fullyQualifiedTable = datasetName + "." + tableName; |
| 85 | + String query = "SELECT * FROM " + fullyQualifiedTable; |
| 86 | + |
| 87 | + QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); |
| 88 | + TableResult results = bigQuery.query(queryConfig); |
| 89 | + |
| 90 | + String result = ""; |
| 91 | + System.out.println("Queried the following records: "); |
| 92 | + for (FieldValueList row : results.iterateAll()) { |
| 93 | + String rowStatement = |
| 94 | + String.format( |
| 95 | + "User id: %s | age: %d\n", |
| 96 | + row.get("id").getStringValue(), row.get("age").getLongValue()); |
| 97 | + result += rowStatement; |
| 98 | + System.out.println(row); |
| 99 | + } |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + static void insertTestRecord( |
| 104 | + BigQuery bigQuery, String datasetName, String tableName, String id, int age) { |
| 105 | + |
| 106 | + Map<String, Object> rowContent = new HashMap<>(); |
| 107 | + rowContent.put("id", id); |
| 108 | + rowContent.put("age", age); |
| 109 | + |
| 110 | + InsertAllRequest request = |
| 111 | + InsertAllRequest.newBuilder(datasetName, tableName).addRow(rowContent).build(); |
| 112 | + |
| 113 | + InsertAllResponse response = bigQuery.insertAll(request); |
| 114 | + |
| 115 | + if (response.hasErrors()) { |
| 116 | + System.out.println("Insert resulted in errors:"); |
| 117 | + for (Map.Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) { |
| 118 | + System.out.println("Response error: \n" + entry.getValue()); |
| 119 | + } |
| 120 | + } else { |
| 121 | + System.out.println("Successfully inserted test row."); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + static void createTable(BigQuery bigQuery, String datasetName, String tableName, Schema schema) { |
| 126 | + |
| 127 | + TableId tableId = TableId.of(datasetName, tableName); |
| 128 | + TableDefinition tableDefinition = StandardTableDefinition.of(schema); |
| 129 | + TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); |
| 130 | + bigQuery.create(tableInfo); |
| 131 | + System.out.println("Created new table: " + tableName); |
| 132 | + } |
| 133 | + |
| 134 | + static boolean hasTable(BigQuery bigQuery, String datasetName, String tableName) { |
| 135 | + |
| 136 | + Page<Table> tables = bigQuery.listTables(datasetName); |
| 137 | + for (Table table : tables.iterateAll()) { |
| 138 | + if (tableName.equals(table.getTableId().getTable())) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + } |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + static void createDataset(BigQuery bigQuery, String datasetName) { |
| 146 | + DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build(); |
| 147 | + Dataset newDataset = bigQuery.create(datasetInfo); |
| 148 | + System.out.println("Created new dataset: " + newDataset.getDatasetId().getDataset()); |
| 149 | + } |
| 150 | + |
| 151 | + static boolean hasDataset(BigQuery bigQuery, String datasetName) { |
| 152 | + Page<Dataset> datasets = bigQuery.listDatasets(); |
| 153 | + for (Dataset dataset : datasets.iterateAll()) { |
| 154 | + if (datasetName.equals(dataset.getDatasetId().getDataset())) { |
| 155 | + return true; |
| 156 | + } |
| 157 | + } |
| 158 | + return false; |
| 159 | + } |
| 160 | + |
| 161 | + static void deleteTable(BigQuery bigQuery, String datasetName, String tableName) { |
| 162 | + bigQuery.getTable(datasetName, tableName).delete(); |
| 163 | + System.out.println("Deleted table: " + tableName); |
| 164 | + } |
| 165 | + |
| 166 | + static void deleteDataset(BigQuery bigQuery, String datasetName) { |
| 167 | + bigQuery.getDataset(datasetName).delete(); |
| 168 | + System.out.println("Deleting dataset " + datasetName); |
| 169 | + } |
| 170 | +} |
0 commit comments