Skip to content

Commit a6cc3f1

Browse files
committed
bigquery: show base64 encode and records in insertAll snippets.
Showing a raw base64 string isn't great. Instead, show a way to encode the bytes to a base64 string using Guava's BaseEncoding class. Also, it wasn't clear how to insert values of type record, so show that, too.
1 parent da12d8f commit a6cc3f1

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,11 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
790790
* Map<String, Object> rowContent = new HashMap<>();
791791
* rowContent.put("booleanField", true);
792792
* // Bytes are passed in base64
793-
* rowContent.put("bytesField", "DQ4KDQ==");
793+
* rowContent.put("bytesField", BaseEncoding.base64().encode(new byte[]{0xA, 0xD, 0xD, 0xE, 0xD}));
794+
* // Records are passed as a map
795+
* Map<String, Object> recordsContent = new HashMap<>();
796+
* recordsContent.put("stringField", "Hello, World!");
797+
* rowContent.put("recordField", recordsContent);
794798
* InsertAllResponse response = bigquery.insertAll(InsertAllRequest.newBuilder(tableId)
795799
* .addRow("rowId", rowContent)
796800
* // More rows can be added in the same RPC by invoking .addRow() on the builder
@@ -1014,15 +1018,16 @@ Page<List<FieldValue>> listTableData(String datasetId, String tableId,
10141018
* WriteChannelConfiguration.newBuilder(tableId)
10151019
* .setFormatOptions(FormatOptions.csv())
10161020
* .build();
1017-
* BaseWriteChannel<BigQueryOptions, WriteChannelConfiguration> writer =
1018-
* bigquery.writer(writeChannelConfiguration);
1021+
* TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
10191022
* // Write data to writer
10201023
* try {
10211024
* writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
10221025
* } catch (IOException e) {
10231026
* // Unable to write data
10241027
* }
10251028
* writer.close();
1029+
* // Get load job
1030+
* Job job = writer.getJob();
10261031
* }</pre>
10271032
*
10281033
* @throws BigQueryException upon failure

google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/BigQuerySnippets.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import com.google.cloud.bigquery.TableId;
5757
import com.google.cloud.bigquery.TableInfo;
5858
import com.google.cloud.bigquery.WriteChannelConfiguration;
59+
import com.google.common.io.BaseEncoding;
5960

6061
import java.io.IOException;
6162
import java.nio.ByteBuffer;
@@ -366,7 +367,11 @@ public InsertAllResponse insertAll(String datasetName, String tableName) {
366367
Map<String, Object> rowContent = new HashMap<>();
367368
rowContent.put("booleanField", true);
368369
// Bytes are passed in base64
369-
rowContent.put("bytesField", "DQ4KDQ==");
370+
rowContent.put("bytesField", BaseEncoding.base64().encode(new byte[]{0xA, 0xD, 0xD, 0xE, 0xD}));
371+
// Records are passed as a map
372+
Map<String, Object> recordsContent = new HashMap<>();
373+
recordsContent.put("stringField", "Hello, World!");
374+
rowContent.put("recordField", recordsContent);
370375
InsertAllResponse response = bigquery.insertAll(InsertAllRequest.newBuilder(tableId)
371376
.addRow("rowId", rowContent)
372377
// More rows can be added in the same RPC by invoking .addRow() on the builder

google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITBigQuerySnippets.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,14 @@ public void testInsertAllAndListTableData() throws IOException, InterruptedExcep
192192
String tableName = "test_insert_all_and_list_table_data";
193193
String fieldName1 = "booleanField";
194194
String fieldName2 = "bytesField";
195+
String fieldName3 = "recordField";
196+
String fieldName4 = "stringField";
195197
TableId tableId = TableId.of(DATASET, tableName);
196198
Schema schema =
197-
Schema.of(Field.of(fieldName1, Type.bool()), Field.of(fieldName2, Type.bytes()));
199+
Schema.of(
200+
Field.of(fieldName1, Type.bool()),
201+
Field.of(fieldName2, Type.bytes()),
202+
Field.of(fieldName3, Type.record(Field.of(fieldName4, Type.string()))));
198203
TableInfo table = TableInfo.of(tableId, StandardTableDefinition.of(schema));
199204
assertNotNull(bigquery.create(table));
200205
InsertAllResponse response = bigquerySnippets.insertAll(DATASET, tableName);
@@ -207,7 +212,8 @@ public void testInsertAllAndListTableData() throws IOException, InterruptedExcep
207212
}
208213
List<FieldValue> row = listPage.getValues().iterator().next();
209214
assertEquals(true, row.get(0).getBooleanValue());
210-
assertArrayEquals(new byte[]{0xD, 0xE, 0xA, 0xD}, row.get(1).getBytesValue());
215+
assertArrayEquals(new byte[]{0xA, 0xD, 0xD, 0xE, 0xD}, row.get(1).getBytesValue());
216+
assertEquals("Hello, World!", row.get(2).getRecordValue().get(0).getStringValue());
211217
assertTrue(bigquerySnippets.deleteTable(DATASET, tableName));
212218
}
213219

0 commit comments

Comments
 (0)