|
| 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_inserting_data_types] |
| 20 | +import com.google.cloud.bigquery.BigQuery; |
| 21 | +import com.google.cloud.bigquery.BigQueryError; |
| 22 | +import com.google.cloud.bigquery.BigQueryException; |
| 23 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 24 | +import com.google.cloud.bigquery.Field; |
| 25 | +import com.google.cloud.bigquery.InsertAllRequest; |
| 26 | +import com.google.cloud.bigquery.InsertAllResponse; |
| 27 | +import com.google.cloud.bigquery.Schema; |
| 28 | +import com.google.cloud.bigquery.StandardSQLTypeName; |
| 29 | +import com.google.cloud.bigquery.StandardTableDefinition; |
| 30 | +import com.google.cloud.bigquery.TableDefinition; |
| 31 | +import com.google.cloud.bigquery.TableId; |
| 32 | +import com.google.cloud.bigquery.TableInfo; |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | + |
| 37 | +// Sample to insert data types in a table |
| 38 | +public class InsertingDataTypes { |
| 39 | + |
| 40 | + public static void runInsertingDataTypes() { |
| 41 | + // TODO(developer): Replace these variables before running the sample. |
| 42 | + String datasetName = "MY_DATASET_NAME"; |
| 43 | + String tableName = "MY_TABLE_NAME"; |
| 44 | + |
| 45 | + // Inserting data types |
| 46 | + Field name = Field.of("name", StandardSQLTypeName.STRING); |
| 47 | + Field age = Field.of("age", StandardSQLTypeName.INT64); |
| 48 | + Field school = |
| 49 | + Field.newBuilder("school", StandardSQLTypeName.BYTES).setMode(Field.Mode.REPEATED).build(); |
| 50 | + Field location = Field.of("location", StandardSQLTypeName.GEOGRAPHY); |
| 51 | + Field measurements = |
| 52 | + Field.newBuilder("measurements", StandardSQLTypeName.FLOAT64) |
| 53 | + .setMode(Field.Mode.REPEATED) |
| 54 | + .build(); |
| 55 | + Field day = Field.of("day", StandardSQLTypeName.DATE); |
| 56 | + Field firstTime = Field.of("firstTime", StandardSQLTypeName.DATETIME); |
| 57 | + Field secondTime = Field.of("secondTime", StandardSQLTypeName.TIME); |
| 58 | + Field thirdTime = Field.of("thirdTime", StandardSQLTypeName.TIMESTAMP); |
| 59 | + Field datesTime = |
| 60 | + Field.of("datesTime", StandardSQLTypeName.STRUCT, day, firstTime, secondTime, thirdTime); |
| 61 | + Schema schema = Schema.of(name, age, school, location, measurements, datesTime); |
| 62 | + |
| 63 | + // Inserting Sample data |
| 64 | + Map<String, Object> datesTimeContent = new HashMap<>(); |
| 65 | + datesTimeContent.put("day", "2019-1-12"); |
| 66 | + datesTimeContent.put("firstTime", "2019-02-17 11:24:00.000"); |
| 67 | + datesTimeContent.put("secondTime", "14:00:00"); |
| 68 | + datesTimeContent.put("thirdTime", "2020-04-27T18:07:25.356Z"); |
| 69 | + |
| 70 | + Map<String, Object> rowContent = new HashMap<>(); |
| 71 | + rowContent.put("name", "Tom"); |
| 72 | + rowContent.put("age", 30); |
| 73 | + rowContent.put("school", "Test University".getBytes()); |
| 74 | + rowContent.put("location", "POINT(1 2)"); |
| 75 | + rowContent.put("measurements", new Float[] {50.05f, 100.5f}); |
| 76 | + rowContent.put("datesTime", datesTimeContent); |
| 77 | + |
| 78 | + insertingDataTypes(datasetName, tableName, schema, rowContent); |
| 79 | + } |
| 80 | + |
| 81 | + public static void insertingDataTypes( |
| 82 | + String datasetName, String tableName, Schema schema, Map<String, Object> rowContent) { |
| 83 | + try { |
| 84 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 85 | + // once, and can be reused for multiple requests. |
| 86 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
| 87 | + |
| 88 | + TableId tableId = TableId.of(datasetName, tableName); |
| 89 | + TableDefinition tableDefinition = StandardTableDefinition.of(schema); |
| 90 | + TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); |
| 91 | + |
| 92 | + bigquery.create(tableInfo); |
| 93 | + |
| 94 | + InsertAllResponse response = |
| 95 | + bigquery.insertAll(InsertAllRequest.newBuilder(tableId).addRow(rowContent).build()); |
| 96 | + |
| 97 | + if (response.hasErrors()) { |
| 98 | + // If any of the insertions failed, this lets you inspect the errors |
| 99 | + for (Map.Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) { |
| 100 | + System.out.println("Response error: \n" + entry.getValue()); |
| 101 | + } |
| 102 | + } |
| 103 | + System.out.println("Rows successfully inserted into table"); |
| 104 | + } catch (BigQueryException e) { |
| 105 | + System.out.println("Insert operation not performed \n" + e.toString()); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +// [END bigquery_inserting_data_types] |
0 commit comments