com.google.cloud.bigquery.QueryRequest

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

1. ITBigQueryTest#testCreateExternalTable()

Project: gcloud-java
Source File: ITBigQueryTest.java
View license
@Test
public void testCreateExternalTable() throws InterruptedException {
    String tableName = "test_create_external_table";
    TableId tableId = TableId.of(DATASET, tableName);
    ExternalTableDefinition externalTableDefinition = ExternalTableDefinition.of("gs://" + BUCKET + "/" + JSON_LOAD_FILE, TABLE_SCHEMA, FormatOptions.json());
    TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    assertEquals(DATASET, createdTable.tableId().dataset());
    assertEquals(tableName, createdTable.tableId().table());
    Table remoteTable = bigquery.getTable(DATASET, tableName);
    assertNotNull(remoteTable);
    assertTrue(remoteTable.definition() instanceof ExternalTableDefinition);
    assertEquals(createdTable.tableId(), remoteTable.tableId());
    assertEquals(TABLE_SCHEMA, remoteTable.definition().schema());
    QueryRequest request = QueryRequest.builder("SELECT TimestampField, StringField, IntegerField, BooleanField FROM " + DATASET + "." + tableName).defaultDataset(DatasetId.of(DATASET)).maxWaitTime(60000L).pageSize(1000L).build();
    QueryResponse response = bigquery.query(request);
    while (!response.jobCompleted()) {
        response = bigquery.getQueryResults(response.jobId());
        Thread.sleep(1000);
    }
    long integerValue = 0;
    int rowCount = 0;
    for (List<FieldValue> row : response.result().values()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue integerCell = row.get(2);
        FieldValue booleanCell = row.get(3);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute());
        assertEquals(1408452095220000L, timestampCell.timestampValue());
        assertEquals("stringValue", stringCell.stringValue());
        assertEquals(integerValue, integerCell.longValue());
        assertEquals(false, booleanCell.booleanValue());
        integerValue = ~integerValue & 0x1;
        rowCount++;
    }
    assertEquals(4, rowCount);
    assertTrue(remoteTable.delete());
}

2. ITBigQueryTest#testCreateViewTable()

Project: gcloud-java
Source File: ITBigQueryTest.java
View license
@Test
public void testCreateViewTable() throws InterruptedException {
    String tableName = "test_create_view_table";
    TableId tableId = TableId.of(DATASET, tableName);
    ViewDefinition viewDefinition = ViewDefinition.of("SELECT TimestampField, StringField, BooleanField FROM " + DATASET + "." + TABLE_ID.table());
    TableInfo tableInfo = TableInfo.of(tableId, viewDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    assertEquals(DATASET, createdTable.tableId().dataset());
    assertEquals(tableName, createdTable.tableId().table());
    Table remoteTable = bigquery.getTable(DATASET, tableName);
    assertNotNull(remoteTable);
    assertEquals(createdTable.tableId(), remoteTable.tableId());
    assertTrue(remoteTable.definition() instanceof ViewDefinition);
    Schema expectedSchema = Schema.builder().addField(Field.builder("TimestampField", Field.Type.timestamp()).mode(Field.Mode.NULLABLE).build()).addField(Field.builder("StringField", Field.Type.string()).mode(Field.Mode.NULLABLE).build()).addField(Field.builder("BooleanField", Field.Type.bool()).mode(Field.Mode.NULLABLE).build()).build();
    assertEquals(expectedSchema, remoteTable.definition().schema());
    QueryRequest request = QueryRequest.builder("SELECT * FROM " + tableName).defaultDataset(DatasetId.of(DATASET)).maxWaitTime(60000L).pageSize(1000L).build();
    QueryResponse response = bigquery.query(request);
    while (!response.jobCompleted()) {
        response = bigquery.getQueryResults(response.jobId());
        Thread.sleep(1000);
    }
    int rowCount = 0;
    for (List<FieldValue> row : response.result().values()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue booleanCell = row.get(2);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute());
        assertEquals(1408452095220000L, timestampCell.timestampValue());
        assertEquals("stringValue", stringCell.stringValue());
        assertEquals(false, booleanCell.booleanValue());
        rowCount++;
    }
    assertEquals(2, rowCount);
    assertTrue(remoteTable.delete());
}

3. ITBigQueryTest#testQuery()

Project: gcloud-java
Source File: ITBigQueryTest.java
View license
@Test
public void testQuery() throws InterruptedException {
    String query = new StringBuilder().append("SELECT TimestampField, StringField, BooleanField FROM ").append(TABLE_ID.table()).toString();
    QueryRequest request = QueryRequest.builder(query).defaultDataset(DatasetId.of(DATASET)).maxWaitTime(60000L).pageSize(1000L).build();
    QueryResponse response = bigquery.query(request);
    while (!response.jobCompleted()) {
        Thread.sleep(1000);
        response = bigquery.getQueryResults(response.jobId());
    }
    assertEquals(QUERY_RESULT_SCHEMA, response.result().schema());
    int rowCount = 0;
    for (List<FieldValue> row : response.result().values()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue booleanCell = row.get(2);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.attribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.attribute());
        assertEquals(1408452095220000L, timestampCell.timestampValue());
        assertEquals("stringValue", stringCell.stringValue());
        assertEquals(false, booleanCell.booleanValue());
        rowCount++;
    }
    assertEquals(2, rowCount);
    Job queryJob = bigquery.getJob(response.jobId());
    JobStatistics.QueryStatistics statistics = queryJob.statistics();
    assertNotNull(statistics.queryPlan());
}

4. 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());
    }
}