Skip to content

Commit 3780dd0

Browse files
committed
---
yaml --- r: 7681 b: refs/heads/tswast-patch-1 c: 4bc52c3 h: refs/heads/master i: 7679: 91f58f2
1 parent e804638 commit 3780dd0

4 files changed

Lines changed: 93 additions & 17 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: da12d8fd3b190875f5903b1a794916ef3da3cb96
60+
refs/heads/tswast-patch-1: 4bc52c3bbc4ffb1c938376227477eca187e1e43b
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQuery.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,15 +1014,42 @@ Page<List<FieldValue>> listTableData(String datasetId, String tableId,
10141014
* WriteChannelConfiguration.newBuilder(tableId)
10151015
* .setFormatOptions(FormatOptions.csv())
10161016
* .build();
1017-
* BaseWriteChannel<BigQueryOptions, WriteChannelConfiguration> writer =
1018-
* bigquery.writer(writeChannelConfiguration);
1017+
* TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
1018+
* // Write data to writer
1019+
* try {
1020+
* writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
1021+
* } finally {
1022+
* writer.close();
1023+
* }
1024+
* // Get load job
1025+
* Job job = writer.getJob();
1026+
* job = job.waitFor();
1027+
* LoadStatistics stats = job.getStatistics();
1028+
* return stats.getOutputRows();
1029+
* }</pre>
1030+
*
1031+
* <p>Example of writing a local file to a table.
1032+
* <pre> {@code
1033+
* String datasetName = "my_dataset_name";
1034+
* String tableName = "my_table_name";
1035+
* ReadableByteChannel csvReader = Files.newByteChannel(FileSystems.getDefault().getPath(".", "my-data.csv"));
1036+
* TableId tableId = TableId.of(datasetName, tableName);
1037+
* WriteChannelConfiguration writeChannelConfiguration =
1038+
* WriteChannelConfiguration.newBuilder(tableId)
1039+
* .setFormatOptions(FormatOptions.csv())
1040+
* .build();
1041+
* TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
10191042
* // Write data to writer
10201043
* try {
1021-
* writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
1022-
* } catch (IOException e) {
1023-
* // Unable to write data
1044+
* ByteStreams.copy(csvReader, writer);
1045+
* } finally {
1046+
* writer.close();
10241047
* }
1025-
* writer.close();
1048+
* // Get load job
1049+
* Job job = writer.getJob();
1050+
* job = job.waitFor();
1051+
* LoadStatistics stats = job.getStatistics();
1052+
* return stats.getOutputRows();
10261053
* }</pre>
10271054
*
10281055
* @throws BigQueryException upon failure

branches/tswast-patch-1/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/BigQuerySnippets.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package com.google.cloud.examples.bigquery.snippets;
2424

2525
import com.google.api.client.util.Charsets;
26+
import com.google.api.services.bigquery.model.JobStatistics;
2627
import com.google.cloud.Page;
2728
import com.google.cloud.bigquery.BigQuery;
2829
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
@@ -44,6 +45,7 @@
4445
import com.google.cloud.bigquery.JobConfiguration;
4546
import com.google.cloud.bigquery.JobId;
4647
import com.google.cloud.bigquery.JobInfo;
48+
import com.google.cloud.bigquery.JobStatistics.LoadStatistics;
4749
import com.google.cloud.bigquery.QueryJobConfiguration;
4850
import com.google.cloud.bigquery.QueryRequest;
4951
import com.google.cloud.bigquery.QueryResponse;
@@ -56,14 +58,20 @@
5658
import com.google.cloud.bigquery.TableId;
5759
import com.google.cloud.bigquery.TableInfo;
5860
import com.google.cloud.bigquery.WriteChannelConfiguration;
61+
import com.google.common.io.ByteStreams;
5962

6063
import java.io.IOException;
6164
import java.nio.ByteBuffer;
65+
import java.nio.channels.ReadableByteChannel;
66+
import java.nio.channels.WritableByteChannel;
67+
import java.nio.file.Files;
68+
import java.nio.file.Path;
6269
import java.util.HashMap;
6370
import java.util.Iterator;
6471
import java.util.List;
6572
import java.util.Map;
6673
import java.util.Map.Entry;
74+
import java.util.concurrent.TimeoutException;
6775

6876
/**
6977
* This class contains a number of snippets for the {@link BigQuery} interface.
@@ -331,26 +339,57 @@ public Table getTableFromId(String projectId, String datasetName, String tableNa
331339
// [VARIABLE "my_dataset_name"]
332340
// [VARIABLE "my_table_name"]
333341
// [VARIABLE "StringValue1\nStringValue2\n"]
334-
public TableDataWriteChannel writeToTable(String datasetName, String tableName, String csvData)
335-
throws IOException {
342+
public long writeToTable(String datasetName, String tableName, String csvData)
343+
throws IOException, InterruptedException, TimeoutException {
336344
// [START writeToTable]
337345
TableId tableId = TableId.of(datasetName, tableName);
346+
WriteChannelConfiguration writeChannelConfiguration =
347+
WriteChannelConfiguration.newBuilder(tableId)
348+
.setFormatOptions(FormatOptions.csv())
349+
.build();
350+
TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
351+
// Write data to writer
352+
try {
353+
writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
354+
} finally {
355+
writer.close();
356+
}
357+
// Get load job
358+
Job job = writer.getJob();
359+
job = job.waitFor();
360+
LoadStatistics stats = job.getStatistics();
361+
return stats.getOutputRows();
362+
// [END writeToTable]
363+
}
364+
365+
/**
366+
* Example of writing a local file to a table.
367+
*/
368+
// [TARGET writer(WriteChannelConfiguration)]
369+
// [VARIABLE "my_dataset_name"]
370+
// [VARIABLE "my_table_name"]
371+
// [VARIABLE Files.newByteChannel(FileSystems.getDefault().getPath(".", "my-data.csv"))]
372+
public long writeFileToTable(String datasetName, String tableName, ReadableByteChannel csvReader)
373+
throws IOException, InterruptedException, TimeoutException {
374+
// [START writeFileToTable]
375+
TableId tableId = TableId.of(datasetName, tableName);
338376
WriteChannelConfiguration writeChannelConfiguration =
339377
WriteChannelConfiguration.newBuilder(tableId)
340378
.setFormatOptions(FormatOptions.csv())
341379
.build();
342380
TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
343381
// Write data to writer
344382
try {
345-
writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
346-
} catch (IOException e) {
347-
// Unable to write data
383+
ByteStreams.copy(csvReader, writer);
384+
} finally {
385+
writer.close();
348386
}
349-
writer.close();
350387
// Get load job
351388
Job job = writer.getJob();
352-
// [END writeToTable]
353-
return writer;
389+
job = job.waitFor();
390+
LoadStatistics stats = job.getStatistics();
391+
return stats.getOutputRows();
392+
// [END writeFileToTable]
354393
}
355394

356395
/**

branches/tswast-patch-1/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITBigQuerySnippets.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@
5353
import org.junit.Test;
5454
import org.junit.rules.Timeout;
5555

56+
import java.io.ByteArrayInputStream;
5657
import java.io.IOException;
58+
import java.io.InputStream;
59+
import java.nio.channels.Channels;
60+
import java.nio.charset.StandardCharsets;
5761
import java.util.Iterator;
5862
import java.util.List;
5963
import java.util.Set;
@@ -177,13 +181,19 @@ public void testWriteAndListTableData()
177181
String tableName = "test_write_and_list_table_data";
178182
String fieldName = "string_field";
179183
assertNotNull(bigquerySnippets.createTable(DATASET, tableName, fieldName));
180-
TableDataWriteChannel channel =
184+
long outputRows =
181185
bigquerySnippets.writeToTable(DATASET, tableName, "StringValue1\nStringValue2\n");
182-
channel.getJob().waitFor();
186+
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));
190+
assertEquals(2L, outputRows);
183191
Page<List<FieldValue>> listPage = bigquerySnippets.listTableData(DATASET, tableName);
184192
Iterator<List<FieldValue>> rowIterator = listPage.getValues().iterator();
185193
assertEquals("StringValue1", rowIterator.next().get(0).getStringValue());
186194
assertEquals("StringValue2", rowIterator.next().get(0).getStringValue());
195+
assertEquals("StringValue3", rowIterator.next().get(0).getStringValue());
196+
assertEquals("StringValue4", rowIterator.next().get(0).getStringValue());
187197
assertTrue(bigquerySnippets.deleteTable(DATASET, tableName));
188198
}
189199

0 commit comments

Comments
 (0)