Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.api.client.util.Charsets;
import com.google.api.gax.paging.Page;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.LoadJobConfiguration;
import com.google.cloud.bigquery.TableResult;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQuery.DatasetListOption;
Expand Down Expand Up @@ -63,6 +64,7 @@
import java.nio.channels.Channels;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -378,6 +380,40 @@ public long writeFileToTable(String datasetName, String tableName, Path csvPath)
// [END writeFileToTable]
}

/**
* Example of writing a newline-delimited-json file with textual fields from GCS to a table.
*/
// [TARGET writer(WriteChannelConfiguration)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE "my_source_uri")]

This comment was marked as spam.

This comment was marked as spam.

// [VARIABLE "field_names")]

This comment was marked as spam.

This comment was marked as spam.

public Long writeRemoteFileToTable(String datasetName, String tableName, String sourceUri, List<String> fieldNames)
throws InterruptedException {
// [START bigquery_load_table_gcs_json]
TableId tableId = TableId.of(datasetName, tableName);
LoadJobConfiguration configuration = LoadJobConfiguration.builder(tableId, sourceUri)
.setFormatOptions(FormatOptions.json())
.build();
// Table field definition
ArrayList<Field> fields = new ArrayList<>();
for (String fieldName: fieldNames) {

This comment was marked as spam.

This comment was marked as spam.

fields.add(Field.of(fieldName, LegacySQLTypeName.STRING));
}
// Table schema definition
Schema schema = Schema.of(fields.toArray(new Field[fields.size()]));

This comment was marked as spam.

This comment was marked as spam.

// Create the table
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
bigquery.create(TableInfo.of(tableId, tableDefinition));

This comment was marked as spam.

This comment was marked as spam.

// Load the table
Job remoteLoadJob = bigquery.create(JobInfo.of(configuration));
remoteLoadJob = remoteLoadJob.waitFor();
// Check the table
System.out.println("State: " + remoteLoadJob.getStatus().getState());
return ((StandardTableDefinition) bigquery.getTable(tableId).getDefinition()).getNumRows();
// [END bigquery_load_table_gcs_json]
}

/**
* Example of inserting rows into a table without running a load job.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.api.gax.paging.Page;
Expand Down Expand Up @@ -48,7 +49,10 @@
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -197,6 +201,27 @@ public void testWriteAndListTableData()
assertTrue(bigquerySnippets.deleteTable(DATASET, tableName));
}

@Test
public void testWriteRemoteJsonToTable() throws InterruptedException {
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json";
String datasetName = "test_dataset";
String tableName = "us_states";
List<String> fieldNames = Arrays.asList("name", "post_abbr");
Table table = bigquery.getTable(datasetName, tableName);
assertNull(table);

Long result = bigquerySnippets.writeRemoteFileToTable(datasetName, tableName, sourceUri, fieldNames);
table = bigquery.getTable(datasetName, tableName);
assertNotNull(table);
ArrayList<String> tableFieldNames = new ArrayList<>();
for (Field field: table.getDefinition().getSchema().getFields()) {
tableFieldNames.add(field.getName());
}
assertArrayEquals(fieldNames.toArray(), tableFieldNames.toArray());
bigquery.delete(table.getTableId());
assertEquals(Long.valueOf(50), result);
}

@Test
public void testInsertAllAndListTableData() throws IOException, InterruptedException {
String tableName = "test_insert_all_and_list_table_data";
Expand Down