Search in sources :

Example 11 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class InsertDataAndQueryTable method main.

public static void main(String... args) throws InterruptedException {
    // Create a service instance
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    // Create a dataset
    String datasetId = "my_dataset_id";
    bigquery.create(DatasetInfo.newBuilder(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.newBuilder(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.newBuilder("SELECT * FROM my_dataset_id.my_table_id").setMaxWaitTime(60000L).setPageSize(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.getJobId());
    }
    // Read rows
    System.out.println("Table rows:");
    for (List<FieldValue> row : queryResponse.getResult().iterateAll()) {
        System.out.println(row);
    }
}
Also used : TableId(com.google.cloud.bigquery.TableId) BigQuery(com.google.cloud.bigquery.BigQuery) QueryRequest(com.google.cloud.bigquery.QueryRequest) HashMap(java.util.HashMap) Schema(com.google.cloud.bigquery.Schema) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) Field(com.google.cloud.bigquery.Field) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) InsertAllRequest(com.google.cloud.bigquery.InsertAllRequest)

Example 12 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testQueryJob.

@Test
public void testQueryJob() throws InterruptedException, TimeoutException {
    String tableName = "test_query_job_table";
    String query = new StringBuilder().append("SELECT TimestampField, StringField, BooleanField FROM ").append(TABLE_ID.getTable()).toString();
    TableId destinationTable = TableId.of(DATASET, tableName);
    QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setDestinationTable(destinationTable).build();
    Job remoteJob = bigquery.create(JobInfo.of(configuration));
    remoteJob = remoteJob.waitFor();
    assertNull(remoteJob.getStatus().getError());
    QueryResponse response = bigquery.getQueryResults(remoteJob.getJobId());
    while (!response.jobCompleted()) {
        Thread.sleep(1000);
        response = bigquery.getQueryResults(response.getJobId());
    }
    assertFalse(response.hasErrors());
    assertEquals(QUERY_RESULT_SCHEMA, response.getResult().getSchema());
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue booleanCell = row.get(2);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(false, booleanCell.getBooleanValue());
        rowCount++;
    }
    assertEquals(2, rowCount);
    assertTrue(bigquery.delete(DATASET, tableName));
    Job queryJob = bigquery.getJob(remoteJob.getJobId());
    JobStatistics.QueryStatistics statistics = queryJob.getStatistics();
    assertNotNull(statistics.getQueryPlan());
}
Also used : TableId(com.google.cloud.bigquery.TableId) JobStatistics(com.google.cloud.bigquery.JobStatistics) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) Test(org.junit.Test)

Example 13 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testCreateExternalTable.

@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.getTableId().getDataset());
    assertEquals(tableName, createdTable.getTableId().getTable());
    Table remoteTable = bigquery.getTable(DATASET, tableName);
    assertNotNull(remoteTable);
    assertTrue(remoteTable.getDefinition() instanceof ExternalTableDefinition);
    assertEquals(createdTable.getTableId(), remoteTable.getTableId());
    assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
    QueryRequest request = QueryRequest.newBuilder("SELECT TimestampField, StringField, IntegerArrayField, BooleanField FROM " + DATASET + "." + tableName).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).build();
    QueryResponse response = bigquery.query(request);
    while (!response.jobCompleted()) {
        response = bigquery.getQueryResults(response.getJobId());
        Thread.sleep(1000);
    }
    long integerValue = 0;
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        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.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(integerValue, integerCell.getLongValue());
        assertEquals(false, booleanCell.getBooleanValue());
        integerValue = ~integerValue & 0x1;
        rowCount++;
    }
    assertEquals(4, rowCount);
    assertTrue(remoteTable.delete());
}
Also used : TableId(com.google.cloud.bigquery.TableId) ExternalTableDefinition(com.google.cloud.bigquery.ExternalTableDefinition) Table(com.google.cloud.bigquery.Table) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) TableInfo(com.google.cloud.bigquery.TableInfo) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Example 14 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testQuery.

@Test
public void testQuery() throws InterruptedException {
    String query = new StringBuilder().append("SELECT TimestampField, StringField, BooleanField FROM ").append(TABLE_ID.getTable()).toString();
    QueryRequest request = QueryRequest.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).build();
    QueryResponse response = queryAndWaitForResponse(request);
    assertEquals(QUERY_RESULT_SCHEMA, response.getResult().getSchema());
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue booleanCell = row.get(2);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(false, booleanCell.getBooleanValue());
        rowCount++;
    }
    assertEquals(2, rowCount);
    Job queryJob = bigquery.getJob(response.getJobId());
    JobStatistics.QueryStatistics statistics = queryJob.getStatistics();
    assertNotNull(statistics.getQueryPlan());
}
Also used : JobStatistics(com.google.cloud.bigquery.JobStatistics) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue) Job(com.google.cloud.bigquery.Job) Test(org.junit.Test)

Aggregations

FieldValue (com.google.cloud.bigquery.FieldValue)14 QueryResponse (com.google.cloud.bigquery.QueryResponse)9 Test (org.junit.Test)9 QueryRequest (com.google.cloud.bigquery.QueryRequest)8 TableId (com.google.cloud.bigquery.TableId)7 List (java.util.List)5 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)3 Job (com.google.cloud.bigquery.Job)3 QueryResult (com.google.cloud.bigquery.QueryResult)3 Schema (com.google.cloud.bigquery.Schema)3 TableInfo (com.google.cloud.bigquery.TableInfo)3 ImmutableList (com.google.common.collect.ImmutableList)3 JobStatistics (com.google.cloud.bigquery.JobStatistics)2 Table (com.google.cloud.bigquery.Table)2 BigQuery (com.google.cloud.bigquery.BigQuery)1 ExternalTableDefinition (com.google.cloud.bigquery.ExternalTableDefinition)1 Field (com.google.cloud.bigquery.Field)1 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)1 LoadStatistics (com.google.cloud.bigquery.JobStatistics.LoadStatistics)1 LoadJobConfiguration (com.google.cloud.bigquery.LoadJobConfiguration)1