Skip to content

Commit a7fceb9

Browse files
tswastgarrettjonesgoogle
authored andcommitted
---
yaml --- r: 5243 b: refs/heads/master c: acf5d14 h: refs/heads/master i: 5241: 8a1cfe1 5239: 9096506
1 parent 1ecdeeb commit a7fceb9

5 files changed

Lines changed: 28 additions & 22 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 029224c20d80b875756ee1dc703d45f4ee3ffc95
2+
refs/heads/master: acf5d14a23e536a8eb752d7049706b22ad82f8f5
33
refs/heads/travis: dae77e558b884bc1b165155482d76c8e40b0fca4
44
refs/heads/gh-pages: 229631582f8957646f81e92ae5a326504f48ee5b
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ 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", BaseEncoding.base64().encode(new byte[]{0xA, 0xD, 0xD, 0xE, 0xD}));
793+
* rowContent.put("bytesField", "Cg0NDg0="); // 0xA, 0xD, 0xD, 0xE, 0xD in base64
794794
* // Records are passed as a map
795795
* Map<String, Object> recordsContent = new HashMap<>();
796796
* recordsContent.put("stringField", "Hello, World!");
@@ -1036,18 +1036,16 @@ Page<List<FieldValue>> listTableData(String datasetId, String tableId,
10361036
* <pre> {@code
10371037
* String datasetName = "my_dataset_name";
10381038
* String tableName = "my_table_name";
1039-
* ReadableByteChannel csvReader = Files.newByteChannel(FileSystems.getDefault().getPath(".", "my-data.csv"));
1039+
* Path csvPath = FileSystems.getDefault().getPath(".", "my-data.csv");
10401040
* TableId tableId = TableId.of(datasetName, tableName);
10411041
* WriteChannelConfiguration writeChannelConfiguration =
10421042
* WriteChannelConfiguration.newBuilder(tableId)
10431043
* .setFormatOptions(FormatOptions.csv())
10441044
* .build();
10451045
* TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
10461046
* // Write data to writer
1047-
* try {
1048-
* ByteStreams.copy(csvReader, writer);
1049-
* } finally {
1050-
* writer.close();
1047+
* try (OutputStream stream = Channels.newOutputStream(writer)) {
1048+
* Files.copy(csvPath, stream);
10511049
* }
10521050
* // Get load job
10531051
* Job job = writer.getJob();

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@
5757
import com.google.cloud.bigquery.TableId;
5858
import com.google.cloud.bigquery.TableInfo;
5959
import com.google.cloud.bigquery.WriteChannelConfiguration;
60-
import com.google.common.io.BaseEncoding;
61-
import com.google.common.io.ByteStreams;
6260

6361
import java.io.IOException;
62+
import java.io.OutputStream;
6463
import java.nio.ByteBuffer;
65-
import java.nio.channels.ReadableByteChannel;
64+
import java.nio.channels.Channels;
65+
import java.nio.file.Files;
66+
import java.nio.file.Path;
6667
import java.util.HashMap;
6768
import java.util.Iterator;
6869
import java.util.List;
@@ -365,8 +366,8 @@ public long writeToTable(String datasetName, String tableName, String csvData)
365366
// [TARGET writer(WriteChannelConfiguration)]
366367
// [VARIABLE "my_dataset_name"]
367368
// [VARIABLE "my_table_name"]
368-
// [VARIABLE Files.newByteChannel(FileSystems.getDefault().getPath(".", "my-data.csv"))]
369-
public long writeFileToTable(String datasetName, String tableName, ReadableByteChannel csvReader)
369+
// [VARIABLE FileSystems.getDefault().getPath(".", "my-data.csv")]
370+
public long writeFileToTable(String datasetName, String tableName, Path csvPath)
370371
throws IOException, InterruptedException, TimeoutException {
371372
// [START writeFileToTable]
372373
TableId tableId = TableId.of(datasetName, tableName);
@@ -376,10 +377,8 @@ public long writeFileToTable(String datasetName, String tableName, ReadableByteC
376377
.build();
377378
TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
378379
// Write data to writer
379-
try {
380-
ByteStreams.copy(csvReader, writer);
381-
} finally {
382-
writer.close();
380+
try (OutputStream stream = Channels.newOutputStream(writer)) {
381+
Files.copy(csvPath, stream);
383382
}
384383
// Get load job
385384
Job job = writer.getJob();
@@ -402,7 +401,7 @@ public InsertAllResponse insertAll(String datasetName, String tableName) {
402401
Map<String, Object> rowContent = new HashMap<>();
403402
rowContent.put("booleanField", true);
404403
// Bytes are passed in base64
405-
rowContent.put("bytesField", BaseEncoding.base64().encode(new byte[]{0xA, 0xD, 0xD, 0xE, 0xD}));
404+
rowContent.put("bytesField", "Cg0NDg0="); // 0xA, 0xD, 0xD, 0xE, 0xD in base64
406405
// Records are passed as a map
407406
Map<String, Object> recordsContent = new HashMap<>();
408407
recordsContent.put("stringField", "Hello, World!");

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
import com.google.cloud.bigquery.Schema;
4040
import com.google.cloud.bigquery.StandardTableDefinition;
4141
import com.google.cloud.bigquery.Table;
42-
import com.google.cloud.bigquery.TableDataWriteChannel;
4342
import com.google.cloud.bigquery.TableId;
4443
import com.google.cloud.bigquery.TableInfo;
4544
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
4645
import com.google.common.base.Function;
4746
import com.google.common.collect.Iterators;
4847
import com.google.common.collect.Sets;
48+
import com.google.common.io.Resources;
4949

5050
import org.junit.AfterClass;
5151
import org.junit.BeforeClass;
@@ -56,8 +56,11 @@
5656
import java.io.ByteArrayInputStream;
5757
import java.io.IOException;
5858
import java.io.InputStream;
59+
import java.net.URISyntaxException;
5960
import java.nio.channels.Channels;
6061
import java.nio.charset.StandardCharsets;
62+
import java.nio.file.Path;
63+
import java.nio.file.Paths;
6164
import java.util.Iterator;
6265
import java.util.List;
6366
import java.util.Set;
@@ -177,17 +180,21 @@ public void testCreateGetAndDeleteDataset() throws InterruptedException {
177180

178181
@Test
179182
public void testWriteAndListTableData()
180-
throws IOException, InterruptedException, TimeoutException {
183+
throws IOException, InterruptedException, TimeoutException, URISyntaxException {
184+
// Create table
181185
String tableName = "test_write_and_list_table_data";
182186
String fieldName = "string_field";
183187
assertNotNull(bigquerySnippets.createTable(DATASET, tableName, fieldName));
188+
// Add rows from string
184189
long outputRows =
185190
bigquerySnippets.writeToTable(DATASET, tableName, "StringValue1\nStringValue2\n");
186191
assertEquals(2L, outputRows);
187-
InputStream stream =
188-
new ByteArrayInputStream("StringValue3\nStringValue4\n".getBytes(StandardCharsets.UTF_8));
189-
outputRows = bigquerySnippets.writeFileToTable(DATASET, tableName, Channels.newChannel(stream));
192+
// Add rows from file
193+
Path csvPath =
194+
Paths.get(Resources.getResource("bigquery/test_write_and_list_table_data.csv").toURI());
195+
outputRows = bigquerySnippets.writeFileToTable(DATASET, tableName, csvPath);
190196
assertEquals(2L, outputRows);
197+
// List all rows
191198
Page<List<FieldValue>> listPage = bigquerySnippets.listTableData(DATASET, tableName);
192199
Iterator<List<FieldValue>> rowIterator = listPage.getValues().iterator();
193200
assertEquals("StringValue1", rowIterator.next().get(0).getStringValue());
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
StringValue3
2+
StringValue4

0 commit comments

Comments
 (0)