2222
2323package com .google .cloud .examples .bigquery .snippets ;
2424
25- import com .google .api .client .util .Charsets ;
2625import com .google .api .gax .paging .Page ;
2726import com .google .cloud .bigquery .BigQuery ;
2827import com .google .cloud .bigquery .BigQuery .DatasetDeleteOption ;
6160import com .google .cloud .bigquery .WriteChannelConfiguration ;
6261import java .io .IOException ;
6362import java .io .OutputStream ;
64- import java .nio .ByteBuffer ;
6563import java .nio .channels .Channels ;
6664import java .nio .file .Files ;
6765import java .nio .file .Path ;
@@ -310,62 +308,6 @@ public Table getTableFromId(String projectId, String datasetName, String tableNa
310308 return table ;
311309 }
312310
313- /** Example of creating a channel with which to write to a table. */
314- // [TARGET writer(WriteChannelConfiguration)]
315- // [VARIABLE "my_dataset_name"]
316- // [VARIABLE "my_table_name"]
317- // [VARIABLE "StringValue1\nStringValue2\n"]
318- public long writeToTable (String datasetName , String tableName , String csvData )
319- throws IOException , InterruptedException , TimeoutException {
320- // [START ]
321- TableId tableId = TableId .of (datasetName , tableName );
322- WriteChannelConfiguration writeChannelConfiguration =
323- WriteChannelConfiguration .newBuilder (tableId ).setFormatOptions (FormatOptions .csv ()).build ();
324- TableDataWriteChannel writer = bigquery .writer (writeChannelConfiguration );
325- // Write data to writer
326- try {
327- writer .write (ByteBuffer .wrap (csvData .getBytes (Charsets .UTF_8 )));
328- } finally {
329- writer .close ();
330- }
331- // Get load job
332- Job job = writer .getJob ();
333- job = job .waitFor ();
334- LoadStatistics stats = job .getStatistics ();
335- return stats .getOutputRows ();
336- // [END ]
337- }
338-
339- /** Example of creating a channel with which to write to a table. */
340- // [TARGET writer(JobId, WriteChannelConfiguration)]
341- // [VARIABLE "my_dataset_name"]
342- // [VARIABLE "my_table_name"]
343- // [VARIABLE "StringValue1\nStringValue2\n"]
344- // [VARIABLE "us"]
345- public long writeToTableLocation (
346- String datasetName , String tableName , String csvData , String location )
347- throws IOException , InterruptedException , TimeoutException {
348- // [START ]
349- TableId tableId = TableId .of (datasetName , tableName );
350- WriteChannelConfiguration writeChannelConfiguration =
351- WriteChannelConfiguration .newBuilder (tableId ).setFormatOptions (FormatOptions .csv ()).build ();
352- // The location must be specified; other fields can be auto-detected.
353- JobId jobId = JobId .newBuilder ().setLocation (location ).build ();
354- TableDataWriteChannel writer = bigquery .writer (jobId , writeChannelConfiguration );
355- // Write data to writer
356- try {
357- writer .write (ByteBuffer .wrap (csvData .getBytes (Charsets .UTF_8 )));
358- } finally {
359- writer .close ();
360- }
361- // Get load job
362- Job job = writer .getJob ();
363- job = job .waitFor ();
364- LoadStatistics stats = job .getStatistics ();
365- return stats .getOutputRows ();
366- // [END ]
367- }
368-
369311 /** Example of writing a local file to a table. */
370312 // [TARGET writer(WriteChannelConfiguration)]
371313 // [VARIABLE "my_dataset_name"]
@@ -378,7 +320,10 @@ public long writeFileToTable(String datasetName, String tableName, Path csvPath,
378320 TableId tableId = TableId .of (datasetName , tableName );
379321 WriteChannelConfiguration writeChannelConfiguration =
380322 WriteChannelConfiguration .newBuilder (tableId ).setFormatOptions (FormatOptions .csv ()).build ();
381- // The location must be specified; other fields can be auto-detected.
323+ // Generally, location can be inferred based on the location of the referenced dataset.
324+ // However,
325+ // it can also be set explicitly to force job execution to be routed to a specific processing
326+ // location. See https://cloud.google.com/bigquery/docs/locations for more info.
382327 JobId jobId = JobId .newBuilder ().setLocation (location ).build ();
383328 TableDataWriteChannel writer = bigquery .writer (jobId , writeChannelConfiguration );
384329 // Write data to writer
0 commit comments