5757import com .google .cloud .bigquery .TableId ;
5858import com .google .cloud .bigquery .TableInfo ;
5959import com .google .cloud .bigquery .WriteChannelConfiguration ;
60- import com .google .common .io .BaseEncoding ;
61- import com .google .common .io .ByteStreams ;
6260
6361import java .io .IOException ;
62+ import java .io .OutputStream ;
6463import 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 ;
6667import java .util .HashMap ;
6768import java .util .Iterator ;
6869import 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!" );
0 commit comments