Tables: Streaming insert with complex data types

Insert data of various BigQuery-supported types into a table.

Code Sample


Before trying this sample, follow the Java setup instructions in the BigQuery Quickstart Using Client Libraries. For more information, see the BigQuery Java API reference documentation.

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// Sample to insert data types in a table
public class InsertingDataTypes {

  public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    String datasetName = "MY_DATASET_NAME";
    String tableName = "MY_TABLE_NAME";
    insertingDataTypes(datasetName, tableName);

  public static void insertingDataTypes(String datasetName, String tableName) {
    try {
      // Initialize client that will be used to send requests. This client only needs to be created
      // once, and can be reused for multiple requests.
      BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

      // Inserting data types
      Field name = Field.of("name", StandardSQLTypeName.STRING);
      Field age = Field.of("age", StandardSQLTypeName.INT64);
      Field school =
          Field.newBuilder("school", StandardSQLTypeName.BYTES)
      Field location = Field.of("location", StandardSQLTypeName.GEOGRAPHY);
      Field measurements =
          Field.newBuilder("measurements", StandardSQLTypeName.FLOAT64)
      Field day = Field.of("day", StandardSQLTypeName.DATE);
      Field firstTime = Field.of("firstTime", StandardSQLTypeName.DATETIME);
      Field secondTime = Field.of("secondTime", StandardSQLTypeName.TIME);
      Field thirdTime = Field.of("thirdTime", StandardSQLTypeName.TIMESTAMP);
      Field datesTime =
          Field.of("datesTime", StandardSQLTypeName.STRUCT, day, firstTime, secondTime, thirdTime);
      Schema schema = Schema.of(name, age, school, location, measurements, datesTime);

      TableId tableId = TableId.of(datasetName, tableName);
      TableDefinition tableDefinition = StandardTableDefinition.of(schema);
      TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();


      // Inserting Sample data
      Map<String, Object> datesTimeContent = new HashMap<>();
      datesTimeContent.put("day", "2019-1-12");
      datesTimeContent.put("firstTime", "2019-02-17 11:24:00.000");
      datesTimeContent.put("secondTime", "14:00:00");
      datesTimeContent.put("thirdTime", "2020-04-27T18:07:25.356Z");

      Map<String, Object> rowContent = new HashMap<>();
      rowContent.put("name", "Tom");
      rowContent.put("age", 30);
      rowContent.put("school", "Test University".getBytes());
      rowContent.put("location", "POINT(1 2)");
      rowContent.put("measurements", new Float[] {50.05f, 100.5f});
      rowContent.put("datesTime", datesTimeContent);

      InsertAllResponse response =

      if (response.hasErrors()) {
        // If any of the insertions failed, this lets you inspect the errors
        for (Map.Entry<Long, List<BigQueryError>> entry : response.getInsertErrors().entrySet()) {
          System.out.println("Response error: \n" + entry.getValue());
      System.out.println("Rows successfully inserted into table");
    } catch (BigQueryException e) {
      System.out.println("Insert operation not performed \n" + e.toString());


Before trying this sample, follow the Node.js setup instructions in the BigQuery Quickstart Using Client Libraries. For more information, see the BigQuery Node.js API reference documentation.

// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();

async function instertingDataTypes() {
  // Inserts data of various BigQuery-supported types into a table.

   * TODO(developer): Uncomment the following lines before running the sample.
  // const datasetId = 'my_dataset';
  // const tableId = 'my_table';

  // Describe the schema of the table
  // For more information on supported data types, see
  const schema = [
      name: 'name',
      type: 'STRING',
      name: 'age',
      type: 'INTEGER',
      name: 'school',
      type: 'BYTES',
      name: 'location',
      type: 'GEOGRAPHY',
      name: 'measurements',
      mode: 'REPEATED',
      type: 'FLOAT',
      name: 'datesTimes',
      type: 'RECORD',
      fields: [
          name: 'day',
          type: 'DATE',
          name: 'firstTime',
          type: 'DATETIME',
          name: 'secondTime',
          type: 'TIME',
          name: 'thirdTime',
          type: 'TIMESTAMP',

  // For all options, see
  const options = {
    schema: schema,

  // Create a new table in the dataset
  const [table] = await bigquery
    .createTable(tableId, options);

  console.log(`Table ${} created.`);

  // The DATE type represents a logical calendar date, independent of time zone.
  // A DATE value does not represent a specific 24-hour time period.
  // Rather, a given DATE value represents a different 24-hour period when
  // interpreted in different time zones, and may represent a shorter or longer
  // day during Daylight Savings Time transitions.
  const bqDate ='2019-1-12');
  // A DATETIME object represents a date and time, as they might be
  // displayed on a calendar or clock, independent of time zone.
  const bqDatetime = bigquery.datetime('2019-02-17 11:24:00.000');
  // A TIME object represents a time, as might be displayed on a watch,
  // independent of a specific date and timezone.
  const bqTime = bigquery.time('14:00:00');
  // A TIMESTAMP object represents an absolute point in time,
  // independent of any time zone or convention such as Daylight
  // Savings Time with microsecond precision.
  const bqTimestamp = bigquery.timestamp('2020-04-27T18:07:25.356Z');
  const bqGeography = bigquery.geography('POINT(1 2)');
  const schoolBuffer = Buffer.from('Test University');

  // Rows to be inserted into table
  const rows = [
      name: 'Tom',
      age: '30',
      location: bqGeography,
      school: schoolBuffer,
      measurements: [50.05, 100.5],
      datesTimes: {
        day: bqDate,
        firstTime: bqDatetime,
        secondTime: bqTime,
        thirdTime: bqTimestamp,
      name: 'Ada',
      age: '35',
      measurements: [30.08, 121.7],

  // Insert data into table
  await bigquery

  console.log(`Inserted ${rows.length} rows`);