Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1014,15 +1014,42 @@ Page<List<FieldValue>> listTableData(String datasetId, String tableId,
* WriteChannelConfiguration.newBuilder(tableId)
* .setFormatOptions(FormatOptions.csv())
* .build();
* BaseWriteChannel<BigQueryOptions, WriteChannelConfiguration> writer =
* bigquery.writer(writeChannelConfiguration);
* TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
* // Write data to writer
* try {
* writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
* } finally {
* writer.close();
* }
* // Get load job
* Job job = writer.getJob();
* job = job.waitFor();
* LoadStatistics stats = job.getStatistics();
* return stats.getOutputRows();
* }</pre>
*
* <p>Example of writing a local file to a table.
* <pre> {@code
* String datasetName = "my_dataset_name";
* String tableName = "my_table_name";
* ReadableByteChannel csvReader = Files.newByteChannel(FileSystems.getDefault().getPath(".", "my-data.csv"));
* TableId tableId = TableId.of(datasetName, tableName);
* WriteChannelConfiguration writeChannelConfiguration =
* WriteChannelConfiguration.newBuilder(tableId)
* .setFormatOptions(FormatOptions.csv())
* .build();
* TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
* // Write data to writer
* try {
* writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
* } catch (IOException e) {
* // Unable to write data
* ByteStreams.copy(csvReader, writer);
* } finally {
* writer.close();
* }
* writer.close();
* // Get load job
* Job job = writer.getJob();
* job = job.waitFor();
* LoadStatistics stats = job.getStatistics();
* return stats.getOutputRows();
* }</pre>
*
* @throws BigQueryException upon failure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.cloud.bigquery.JobConfiguration;
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.JobStatistics.LoadStatistics;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.QueryRequest;
import com.google.cloud.bigquery.QueryResponse;
Expand All @@ -56,14 +57,17 @@
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import com.google.cloud.bigquery.WriteChannelConfiguration;
import com.google.common.io.ByteStreams;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeoutException;

/**
* This class contains a number of snippets for the {@link BigQuery} interface.
Expand Down Expand Up @@ -331,26 +335,57 @@ public Table getTableFromId(String projectId, String datasetName, String tableNa
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE "StringValue1\nStringValue2\n"]
public TableDataWriteChannel writeToTable(String datasetName, String tableName, String csvData)
throws IOException {
public long writeToTable(String datasetName, String tableName, String csvData)
throws IOException, InterruptedException, TimeoutException {
// [START writeToTable]
TableId tableId = TableId.of(datasetName, tableName);
WriteChannelConfiguration writeChannelConfiguration =
WriteChannelConfiguration.newBuilder(tableId)
.setFormatOptions(FormatOptions.csv())
.build();
TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
// Write data to writer
try {
writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
} finally {
writer.close();
}
// Get load job
Job job = writer.getJob();
job = job.waitFor();
LoadStatistics stats = job.getStatistics();
return stats.getOutputRows();
// [END writeToTable]
}

/**
* Example of writing a local file to a table.
*/
// [TARGET writer(WriteChannelConfiguration)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE Files.newByteChannel(FileSystems.getDefault().getPath(".", "my-data.csv"))]
public long writeFileToTable(String datasetName, String tableName, ReadableByteChannel csvReader)
throws IOException, InterruptedException, TimeoutException {
// [START writeFileToTable]
TableId tableId = TableId.of(datasetName, tableName);
WriteChannelConfiguration writeChannelConfiguration =
WriteChannelConfiguration.newBuilder(tableId)
.setFormatOptions(FormatOptions.csv())
.build();
TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
// Write data to writer
try {
writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8)));
} catch (IOException e) {
// Unable to write data
ByteStreams.copy(csvReader, writer);
} finally {
writer.close();
}
writer.close();
// Get load job
Job job = writer.getJob();
// [END writeToTable]
return writer;
job = job.waitFor();
LoadStatistics stats = job.getStatistics();
return stats.getOutputRows();
// [END writeFileToTable]
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@
import org.junit.Test;
import org.junit.rules.Timeout;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -177,13 +181,19 @@ public void testWriteAndListTableData()
String tableName = "test_write_and_list_table_data";
String fieldName = "string_field";
assertNotNull(bigquerySnippets.createTable(DATASET, tableName, fieldName));
TableDataWriteChannel channel =
long outputRows =
bigquerySnippets.writeToTable(DATASET, tableName, "StringValue1\nStringValue2\n");
channel.getJob().waitFor();
assertEquals(2L, outputRows);
InputStream stream =
new ByteArrayInputStream("StringValue3\nStringValue4\n".getBytes(StandardCharsets.UTF_8));
outputRows = bigquerySnippets.writeFileToTable(DATASET, tableName, Channels.newChannel(stream));
assertEquals(2L, outputRows);
Page<List<FieldValue>> listPage = bigquerySnippets.listTableData(DATASET, tableName);
Iterator<List<FieldValue>> rowIterator = listPage.getValues().iterator();
assertEquals("StringValue1", rowIterator.next().get(0).getStringValue());
assertEquals("StringValue2", rowIterator.next().get(0).getStringValue());
assertEquals("StringValue3", rowIterator.next().get(0).getStringValue());
assertEquals("StringValue4", rowIterator.next().get(0).getStringValue());
assertTrue(bigquerySnippets.deleteTable(DATASET, tableName));
}

Expand Down