Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b07dee3

Browse files
Praful Makanistephaniewang526
Praful Makani
andauthoredJul 21, 2020
docs(samples): add insert rows without rowids in a table (#573)
* docs(samples): add insert rows without rowids in a table * add comment Co-authored-by: Stephanie Wang <stephaniewang526@users.noreply.github.com>
1 parent 29b8a7f commit b07dee3

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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_table_insert_rows_explicit_none_insert_ids]
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.InsertAllRequest;
25+
import com.google.cloud.bigquery.InsertAllResponse;
26+
import com.google.cloud.bigquery.TableId;
27+
import java.util.ArrayList;
28+
import java.util.HashMap;
29+
import java.util.List;
30+
import java.util.Map;
31+
32+
// Sample to insert rows without row ids in a table
33+
public class TableInsertRowsWithoutRowIds {
34+
35+
public static void runTableInsertRowsWithoutRowIds() {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String datasetName = "MY_DATASET_NAME";
38+
String tableName = "MY_TABLE_NAME";
39+
// Create rows to insert
40+
Map<String, Object> rowContent1 = new HashMap<>();
41+
rowContent1.put("stringField", "Phred Phlyntstone");
42+
rowContent1.put("numericField", 32);
43+
Map<String, Object> rowContent2 = new HashMap<>();
44+
rowContent2.put("stringField", "Wylma Phlyntstone");
45+
rowContent2.put("numericField", 29);
46+
List<InsertAllRequest.RowToInsert> rowContent = new ArrayList<>();
47+
// insertId is null if not specified
48+
rowContent.add(InsertAllRequest.RowToInsert.of(rowContent1));
49+
rowContent.add(InsertAllRequest.RowToInsert.of(rowContent2));
50+
tableInsertRowsWithoutRowIds(datasetName, tableName, rowContent);
51+
}
52+
53+
public static void tableInsertRowsWithoutRowIds(
54+
String datasetName, String tableName, Iterable<InsertAllRequest.RowToInsert> rows) {
55+
try {
56+
// Initialize client that will be used to send requests. This client only needs to be created
57+
// once, and can be reused for multiple requests.
58+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
59+
60+
// Get table
61+
TableId tableId = TableId.of(datasetName, tableName);
62+
63+
// Inserts rowContent into datasetName:tableId.
64+
InsertAllResponse response =
65+
bigquery.insertAll(InsertAllRequest.newBuilder(tableId).setRows(rows).build());
66+
67+
if (response.hasErrors()) {
68+
// If any of the insertions failed, this lets you inspect the errors
69+
for (Map.Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) {
70+
System.out.println("Response error: \n" + entry.getValue());
71+
}
72+
}
73+
System.out.println("Rows successfully inserted into table without row ids");
74+
} catch (BigQueryException e) {
75+
System.out.println("Insert operation not performed \n" + e.toString());
76+
}
77+
}
78+
}
79+
// [END bigquery_table_insert_rows_explicit_none_insert_ids]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.InsertAllRequest;
24+
import com.google.cloud.bigquery.LegacySQLTypeName;
25+
import com.google.cloud.bigquery.Schema;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.PrintStream;
28+
import java.util.ArrayList;
29+
import java.util.HashMap;
30+
import java.util.List;
31+
import java.util.Map;
32+
import java.util.UUID;
33+
import org.junit.After;
34+
import org.junit.Before;
35+
import org.junit.BeforeClass;
36+
import org.junit.Test;
37+
38+
public class TableInsertRowsWithoutRowIdsIT {
39+
40+
private String tableName;
41+
private ByteArrayOutputStream bout;
42+
private PrintStream out;
43+
44+
private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME");
45+
46+
private static void requireEnvVar(String varName) {
47+
assertNotNull(
48+
"Environment variable " + varName + " is required to perform these tests.",
49+
System.getenv(varName));
50+
}
51+
52+
@BeforeClass
53+
public static void checkRequirements() {
54+
requireEnvVar("BIGQUERY_DATASET_NAME");
55+
}
56+
57+
@Before
58+
public void setUp() {
59+
bout = new ByteArrayOutputStream();
60+
out = new PrintStream(bout);
61+
System.setOut(out);
62+
63+
tableName = "INSERT_ROW_WITHOUT_ROW_ID_TEST" + UUID.randomUUID().toString().substring(0, 8);
64+
Schema schema =
65+
Schema.of(
66+
Field.of("stringField", LegacySQLTypeName.STRING),
67+
Field.of("numericField", LegacySQLTypeName.NUMERIC));
68+
69+
// Create table in dataset for testing
70+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
71+
72+
bout = new ByteArrayOutputStream();
73+
out = new PrintStream(bout);
74+
System.setOut(out);
75+
}
76+
77+
@After
78+
public void tearDown() {
79+
// Clean up
80+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
81+
System.setOut(null);
82+
}
83+
84+
@Test
85+
public void testTableInsertRowsWithoutRowIds() {
86+
// Create rows to insert
87+
Map<String, Object> rowContent1 = new HashMap<>();
88+
rowContent1.put("stringField", "Phred Phlyntstone");
89+
rowContent1.put("numericField", 32);
90+
Map<String, Object> rowContent2 = new HashMap<>();
91+
rowContent2.put("stringField", "Wylma Phlyntstone");
92+
rowContent2.put("numericField", 29);
93+
List<InsertAllRequest.RowToInsert> rowContent = new ArrayList<>();
94+
rowContent.add(InsertAllRequest.RowToInsert.of(rowContent1));
95+
rowContent.add(InsertAllRequest.RowToInsert.of(rowContent2));
96+
TableInsertRowsWithoutRowIds.tableInsertRowsWithoutRowIds(
97+
BIGQUERY_DATASET_NAME, tableName, rowContent);
98+
assertThat(bout.toString()).contains("Rows successfully inserted into table without row ids");
99+
}
100+
}

0 commit comments

Comments
 (0)
Failed to load comments.