Search in sources :

Example 6 with FieldValue

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

the class BigQuerySnippets method listTableDataFromId.

/**
   * Example of listing table rows, specifying the page size.
   */
// [TARGET listTableData(TableId, TableDataListOption...)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
public Page<List<FieldValue>> listTableDataFromId(String datasetName, String tableName) {
    // [START listTableDataFromId]
    TableId tableIdObject = TableId.of(datasetName, tableName);
    Page<List<FieldValue>> tableData = bigquery.listTableData(tableIdObject, TableDataListOption.pageSize(100));
    for (List<FieldValue> row : tableData.iterateAll()) {
    // do something with the row
    }
    // [END listTableDataFromId]
    return tableData;
}
Also used : TableId(com.google.cloud.bigquery.TableId) List(java.util.List) FieldValue(com.google.cloud.bigquery.FieldValue)

Example 7 with FieldValue

use of com.google.cloud.bigquery.FieldValue 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)

Example 8 with FieldValue

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

the class BigQuerySnippets method queryResults.

/**
   * Example of getting the results of query.
   */
// [TARGET getQueryResults(JobId, QueryResultsOption...)]
// [VARIABLE "SELECT unique(corpus) FROM [bigquery-public-data:samples.shakespeare]"]
public QueryResponse queryResults(final String query) throws InterruptedException {
    // [START queryResults]
    QueryRequest request = QueryRequest.of(query);
    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 queryResults]
    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)

Example 9 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 10 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)

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