com.google.cloud.bigquery.Field

Here are the examples of the java api class com.google.cloud.bigquery.Field taken from open source projects.

1. InsertDataAndQueryTable#main()

View license
public static void main(String... args) throws InterruptedException {
    // Create a service instance
    BigQuery bigquery = BigQueryOptions.defaultInstance().service();
    // Create a dataset
    String datasetId = "my_dataset_id";
    bigquery.create(DatasetInfo.builder(datasetId).build());
    TableId tableId = TableId.of(datasetId, "my_table_id");
    // Table field definition
    Field stringField = Field.of("StringField", Field.Type.string());
    // Table schema definition
    Schema schema = Schema.of(stringField);
    // Create a table
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
    bigquery.create(TableInfo.of(tableId, tableDefinition));
    // Define rows to insert
    Map<String, Object> firstRow = new HashMap<>();
    Map<String, Object> secondRow = new HashMap<>();
    firstRow.put("StringField", "value1");
    secondRow.put("StringField", "value2");
    // Create an insert request
    InsertAllRequest insertRequest = InsertAllRequest.builder(tableId).addRow(firstRow).addRow(secondRow).build();
    // Insert rows
    InsertAllResponse insertResponse = bigquery.insertAll(insertRequest);
    // Check if errors occurred
    if (insertResponse.hasErrors()) {
        System.out.println("Errors occurred while inserting rows");
    }
    // Create a query request
    QueryRequest queryRequest = QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id").maxWaitTime(60000L).pageSize(1000L).build();
    // Request query to be executed and wait for results
    QueryResponse queryResponse = bigquery.query(queryRequest);
    while (!queryResponse.jobCompleted()) {
        Thread.sleep(1000L);
        queryResponse = bigquery.getQueryResults(queryResponse.jobId());
    }
    // Read rows
    Iterator<List<FieldValue>> rowIterator = queryResponse.result().iterateAll();
    System.out.println("Table rows:");
    while (rowIterator.hasNext()) {
        System.out.println(rowIterator.next());
    }
}