Search in sources :

Example 1 with QueryResponse

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

the class ITBigQuerySnippets method testRunQuery.

@Test
public void testRunQuery() throws InterruptedException {
    QueryResponse queryResponse = bigquerySnippets.runQuery(QUERY);
    assertNotNull(queryResponse);
    assertTrue(queryResponse.jobCompleted());
    assertFalse(queryResponse.hasErrors());
    QueryResult result = queryResponse.getResult();
    assertNotNull(result);
    assertTrue(bigquerySnippets.cancelJob(queryResponse.getJobId().getJob()));
    queryResponse = bigquerySnippets.queryResults(QUERY);
    assertNotNull(queryResponse);
    assertTrue(queryResponse.jobCompleted());
    assertFalse(queryResponse.hasErrors());
    result = queryResponse.getResult();
    assertNotNull(result);
    assertTrue(bigquerySnippets.cancelJobFromId(queryResponse.getJobId().getJob()));
}
Also used : QueryResult(com.google.cloud.bigquery.QueryResult) QueryResponse(com.google.cloud.bigquery.QueryResponse) Test(org.junit.Test)

Example 2 with QueryResponse

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

the class ITBigQueryTest method testCreateViewTable.

@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.getTable());
    TableInfo tableInfo = TableInfo.of(tableId, viewDefinition);
    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);
    assertEquals(createdTable.getTableId(), remoteTable.getTableId());
    assertTrue(remoteTable.getDefinition() instanceof ViewDefinition);
    Schema expectedSchema = Schema.newBuilder().addField(Field.newBuilder("TimestampField", Field.Type.timestamp()).setMode(Field.Mode.NULLABLE).build()).addField(Field.newBuilder("StringField", Field.Type.string()).setMode(Field.Mode.NULLABLE).build()).addField(Field.newBuilder("BooleanField", Field.Type.bool()).setMode(Field.Mode.NULLABLE).build()).build();
    assertEquals(expectedSchema, remoteTable.getDefinition().getSchema());
    QueryRequest request = QueryRequest.newBuilder("SELECT * FROM " + 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);
    }
    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(remoteTable.delete());
}
Also used : TableId(com.google.cloud.bigquery.TableId) Table(com.google.cloud.bigquery.Table) QueryRequest(com.google.cloud.bigquery.QueryRequest) ViewDefinition(com.google.cloud.bigquery.ViewDefinition) Schema(com.google.cloud.bigquery.Schema) QueryResponse(com.google.cloud.bigquery.QueryResponse) TableInfo(com.google.cloud.bigquery.TableInfo) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Example 3 with QueryResponse

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

the class ITBigQueryTest method testPositionalQueryParameters.

@Test
public void testPositionalQueryParameters() throws InterruptedException {
    String query = new StringBuilder().append("SELECT TimestampField, StringField, BooleanField FROM ").append(TABLE_ID.getTable()).append(" WHERE StringField = ?").append(" AND TimestampField > ?").append(" AND IntegerField IN UNNEST(?)").append(" AND IntegerField < ?").append(" AND FloatField > ?").toString();
    QueryParameterValue stringParameter = QueryParameterValue.string("stringValue");
    QueryParameterValue timestampParameter = QueryParameterValue.timestamp("2014-01-01 07:00:00.000000+00:00");
    QueryParameterValue intArrayParameter = QueryParameterValue.array(new Integer[] { 3, 4 }, Integer.class);
    QueryParameterValue int64Parameter = QueryParameterValue.int64(5);
    QueryParameterValue float64Parameter = QueryParameterValue.float64(0.5);
    QueryRequest request = QueryRequest.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).setUseLegacySql(false).addPositionalParameter(stringParameter).addPositionalParameter(timestampParameter).addPositionalParameter(intArrayParameter).addPositionalParameter(int64Parameter).addPositionalParameter(float64Parameter).build();
    QueryResponse response = queryAndWaitForResponse(request);
    assertEquals(QUERY_RESULT_SCHEMA, response.getResult().getSchema());
    assertEquals(2, Iterables.size(response.getResult().getValues()));
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) Test(org.junit.Test)

Example 4 with QueryResponse

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

the class ITBigQueryTest method testBytesParameter.

@Test
public void testBytesParameter() throws Exception {
    String query = new StringBuilder().append("SELECT BYTE_LENGTH(@p) AS length").toString();
    QueryParameterValue bytesParameter = QueryParameterValue.bytes(new byte[] { 1, 3 });
    QueryRequest request = QueryRequest.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).setUseLegacySql(false).addNamedParameter("p", bytesParameter).build();
    QueryResponse response = queryAndWaitForResponse(request);
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        rowCount++;
        assertEquals(2, row.get(0).getLongValue());
    }
    assertEquals(1, rowCount);
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Example 5 with QueryResponse

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

the class BigQuerySnippets method runQueryWithParameters.

/**
   * Example of running a query with query parameters.
   */
// [TARGET query(QueryRequest)]
// [VARIABLE "SELECT distinct(corpus) FROM `bigquery-public-data.samples.shakespeare` where word_count > @wordCount"]
public QueryResponse runQueryWithParameters(String query) throws InterruptedException {
    // [START runQueryWithParameters]
    QueryRequest request = QueryRequest.newBuilder(query).setUseLegacySql(// standard SQL is required to use query parameters
    false).addNamedParameter("wordCount", QueryParameterValue.int64(5)).build();
    QueryResponse response = bigquery.query(request);
    // Wait for things to finish
    while (!response.jobCompleted()) {
        Thread.sleep(1000);
        response = bigquery.getQueryResults(response.getJobId());
    }
    if (response.hasErrors()) {
    // handle errors
    }
    QueryResult result = response.getResult();
    for (List<FieldValue> row : result.iterateAll()) {
    // do something with the data
    }
    // [END runQueryWithParameters]
    return response;
}
Also used : QueryResult(com.google.cloud.bigquery.QueryResult) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue)

Aggregations

QueryResponse (com.google.cloud.bigquery.QueryResponse)16 QueryRequest (com.google.cloud.bigquery.QueryRequest)11 FieldValue (com.google.cloud.bigquery.FieldValue)9 Test (org.junit.Test)9 QueryResult (com.google.cloud.bigquery.QueryResult)5 TableId (com.google.cloud.bigquery.TableId)5 BigQuery (com.google.cloud.bigquery.BigQuery)3 Job (com.google.cloud.bigquery.Job)3 QueryParameterValue (com.google.cloud.bigquery.QueryParameterValue)3 Table (com.google.cloud.bigquery.Table)3 JobStatistics (com.google.cloud.bigquery.JobStatistics)2 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)2 Schema (com.google.cloud.bigquery.Schema)2 TableInfo (com.google.cloud.bigquery.TableInfo)2 ExternalTableDefinition (com.google.cloud.bigquery.ExternalTableDefinition)1 Field (com.google.cloud.bigquery.Field)1 FieldValueList (com.google.cloud.bigquery.FieldValueList)1 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)1 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)1 JobId (com.google.cloud.bigquery.JobId)1