|
23 | 23 | package com.google.cloud.examples.bigquery.snippets; |
24 | 24 |
|
25 | 25 | import com.google.api.client.util.Charsets; |
| 26 | +import com.google.api.services.bigquery.model.JobStatistics; |
26 | 27 | import com.google.cloud.Page; |
27 | 28 | import com.google.cloud.bigquery.BigQuery; |
28 | 29 | import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; |
|
44 | 45 | import com.google.cloud.bigquery.JobConfiguration; |
45 | 46 | import com.google.cloud.bigquery.JobId; |
46 | 47 | import com.google.cloud.bigquery.JobInfo; |
| 48 | +import com.google.cloud.bigquery.JobStatistics.LoadStatistics; |
47 | 49 | import com.google.cloud.bigquery.QueryJobConfiguration; |
48 | 50 | import com.google.cloud.bigquery.QueryRequest; |
49 | 51 | import com.google.cloud.bigquery.QueryResponse; |
|
56 | 58 | import com.google.cloud.bigquery.TableId; |
57 | 59 | import com.google.cloud.bigquery.TableInfo; |
58 | 60 | import com.google.cloud.bigquery.WriteChannelConfiguration; |
| 61 | +import com.google.common.io.ByteStreams; |
59 | 62 |
|
60 | 63 | import java.io.IOException; |
61 | 64 | 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; |
62 | 69 | import java.util.HashMap; |
63 | 70 | import java.util.Iterator; |
64 | 71 | import java.util.List; |
65 | 72 | import java.util.Map; |
66 | 73 | import java.util.Map.Entry; |
| 74 | +import java.util.concurrent.TimeoutException; |
67 | 75 |
|
68 | 76 | /** |
69 | 77 | * 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 |
331 | 339 | // [VARIABLE "my_dataset_name"] |
332 | 340 | // [VARIABLE "my_table_name"] |
333 | 341 | // [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 { |
336 | 344 | // [START writeToTable] |
337 | 345 | 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); |
338 | 376 | WriteChannelConfiguration writeChannelConfiguration = |
339 | 377 | WriteChannelConfiguration.newBuilder(tableId) |
340 | 378 | .setFormatOptions(FormatOptions.csv()) |
341 | 379 | .build(); |
342 | 380 | TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration); |
343 | 381 | // Write data to writer |
344 | 382 | 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(); |
348 | 386 | } |
349 | | - writer.close(); |
350 | 387 | // Get load job |
351 | 388 | 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] |
354 | 393 | } |
355 | 394 |
|
356 | 395 | /** |
|
0 commit comments