Skip to content

Commit 12a2bea

Browse files
author
Praful Makani
authored
docs(samples): add insert data types (#479)
1 parent 05d47fc commit 12a2bea

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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]
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
import java.util.UUID;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
35+
public class InsertingDataTypesIT {
36+
37+
private String tableName;
38+
private ByteArrayOutputStream bout;
39+
private PrintStream out;
40+
41+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
42+
43+
private static String requireEnvVar(String varName) {
44+
String value = System.getenv(varName);
45+
assertNotNull(
46+
"Environment variable " + varName + " is required to perform these tests.",
47+
System.getenv(varName));
48+
return value;
49+
}
50+
51+
@BeforeClass
52+
public static void checkRequirements() {
53+
requireEnvVar("BIGQUERY_DATASET_NAME");
54+
}
55+
56+
@Before
57+
public void setUp() {
58+
tableName = "MY_TABLE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
59+
bout = new ByteArrayOutputStream();
60+
out = new PrintStream(bout);
61+
System.setOut(out);
62+
}
63+
64+
@After
65+
public void tearDown() {
66+
// Clean up
67+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
68+
System.setOut(null);
69+
}
70+
71+
@Test
72+
public void testInsertingDataTypes() {
73+
// Inserting data types
74+
Field name = Field.of("name", StandardSQLTypeName.STRING);
75+
Field age = Field.of("age", StandardSQLTypeName.INT64);
76+
Field school =
77+
Field.newBuilder("school", StandardSQLTypeName.BYTES).setMode(Field.Mode.REPEATED).build();
78+
Field location = Field.of("location", StandardSQLTypeName.GEOGRAPHY);
79+
Field measurements =
80+
Field.newBuilder("measurements", StandardSQLTypeName.FLOAT64)
81+
.setMode(Field.Mode.REPEATED)
82+
.build();
83+
Field day = Field.of("day", StandardSQLTypeName.DATE);
84+
Field firstTime = Field.of("firstTime", StandardSQLTypeName.DATETIME);
85+
Field secondTime = Field.of("secondTime", StandardSQLTypeName.TIME);
86+
Field thirdTime = Field.of("thirdTime", StandardSQLTypeName.TIMESTAMP);
87+
Field datesTime =
88+
Field.of("datesTime", StandardSQLTypeName.STRUCT, day, firstTime, secondTime, thirdTime);
89+
Schema schema = Schema.of(name, age, school, location, measurements, datesTime);
90+
91+
// Inserting Sample data
92+
Map<String, Object> datesTimeContent = new HashMap<>();
93+
datesTimeContent.put("day", "2019-1-12");
94+
datesTimeContent.put("firstTime", "2019-02-17 11:24:00.000");
95+
datesTimeContent.put("secondTime", "14:00:00");
96+
datesTimeContent.put("thirdTime", "2020-04-27T18:07:25.356Z");
97+
98+
Map<String, Object> rowContent = new HashMap<>();
99+
rowContent.put("name", "Tom");
100+
rowContent.put("age", 30);
101+
rowContent.put("school", "Test University".getBytes());
102+
rowContent.put("location", "POINT(1 2)");
103+
rowContent.put("measurements", new Float[] {50.05f, 100.5f});
104+
rowContent.put("datesTime", datesTimeContent);
105+
106+
InsertingDataTypes.insertingDataTypes(BIGQUERY_DATASET_NAME, tableName, schema, rowContent);
107+
assertThat(bout.toString()).contains("Rows successfully inserted into table");
108+
}
109+
}

0 commit comments

Comments
 (0)