Search in sources :

Example 26 with Job

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

the class ITBigQueryTest method testCopyJob.

@Test
public void testCopyJob() throws InterruptedException, TimeoutException {
    String sourceTableName = "test_copy_job_source_table";
    String destinationTableName = "test_copy_job_destination_table";
    TableId sourceTable = TableId.of(DATASET, sourceTableName);
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    assertEquals(DATASET, createdTable.getTableId().getDataset());
    assertEquals(sourceTableName, createdTable.getTableId().getTable());
    TableId destinationTable = TableId.of(DATASET, destinationTableName);
    CopyJobConfiguration configuration = CopyJobConfiguration.of(destinationTable, sourceTable);
    Job remoteJob = bigquery.create(JobInfo.of(configuration));
    remoteJob = remoteJob.waitFor();
    assertNull(remoteJob.getStatus().getError());
    Table remoteTable = bigquery.getTable(DATASET, destinationTableName);
    assertNotNull(remoteTable);
    assertEquals(destinationTable.getDataset(), remoteTable.getTableId().getDataset());
    assertEquals(destinationTableName, remoteTable.getTableId().getTable());
    assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
    assertTrue(createdTable.delete());
    assertTrue(remoteTable.delete());
}
Also used : TableId(com.google.cloud.bigquery.TableId) CopyJobConfiguration(com.google.cloud.bigquery.CopyJobConfiguration) Table(com.google.cloud.bigquery.Table) TableInfo(com.google.cloud.bigquery.TableInfo) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) Job(com.google.cloud.bigquery.Job) Test(org.junit.Test)

Example 27 with Job

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

the class ITBigQueryTest method testCreateAndGetJobWithSelectedFields.

@Test
public void testCreateAndGetJobWithSelectedFields() throws InterruptedException, TimeoutException {
    String sourceTableName = "test_create_and_get_job_with_selected_fields_source_table";
    String destinationTableName = "test_create_and_get_job_with_selected_fields_destination_table";
    TableId sourceTable = TableId.of(DATASET, sourceTableName);
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    assertEquals(DATASET, createdTable.getTableId().getDataset());
    assertEquals(sourceTableName, createdTable.getTableId().getTable());
    TableId destinationTable = TableId.of(DATASET, destinationTableName);
    CopyJobConfiguration configuration = CopyJobConfiguration.of(destinationTable, sourceTable);
    Job createdJob = bigquery.create(JobInfo.of(configuration), JobOption.fields(JobField.ETAG));
    CopyJobConfiguration createdConfiguration = createdJob.getConfiguration();
    assertNotNull(createdJob.getJobId());
    assertNotNull(createdConfiguration.getSourceTables());
    assertNotNull(createdConfiguration.getDestinationTable());
    assertNotNull(createdJob.getEtag());
    assertNull(createdJob.getStatistics());
    assertNull(createdJob.getStatus());
    assertNull(createdJob.getSelfLink());
    assertNull(createdJob.getUserEmail());
    Job remoteJob = bigquery.getJob(createdJob.getJobId(), JobOption.fields(JobField.ETAG));
    CopyJobConfiguration remoteConfiguration = remoteJob.getConfiguration();
    assertEquals(createdJob.getJobId(), remoteJob.getJobId());
    assertEquals(createdConfiguration.getSourceTables(), remoteConfiguration.getSourceTables());
    assertEquals(createdConfiguration.getDestinationTable(), remoteConfiguration.getDestinationTable());
    assertEquals(createdConfiguration.getCreateDisposition(), remoteConfiguration.getCreateDisposition());
    assertEquals(createdConfiguration.getWriteDisposition(), remoteConfiguration.getWriteDisposition());
    assertNotNull(remoteJob.getEtag());
    assertNull(remoteJob.getStatistics());
    assertNull(remoteJob.getStatus());
    assertNull(remoteJob.getSelfLink());
    assertNull(remoteJob.getUserEmail());
    assertTrue(createdTable.delete());
    Job completedJob = remoteJob.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS), WaitForOption.timeout(1, TimeUnit.MINUTES));
    assertNotNull(completedJob);
    assertNull(completedJob.getStatus().getError());
    assertTrue(bigquery.delete(DATASET, destinationTableName));
}
Also used : TableId(com.google.cloud.bigquery.TableId) CopyJobConfiguration(com.google.cloud.bigquery.CopyJobConfiguration) Table(com.google.cloud.bigquery.Table) TableInfo(com.google.cloud.bigquery.TableInfo) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) Job(com.google.cloud.bigquery.Job) Test(org.junit.Test)

Example 28 with Job

use of com.google.cloud.bigquery.Job 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 29 with Job

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

the class ITBigQueryTest method testCancelJob.

@Test
public void testCancelJob() throws InterruptedException, TimeoutException {
    String destinationTableName = "test_cancel_query_job_table";
    String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable();
    TableId destinationTable = TableId.of(DATASET, destinationTableName);
    QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setDestinationTable(destinationTable).build();
    Job remoteJob = bigquery.create(JobInfo.of(configuration));
    assertTrue(remoteJob.cancel());
    remoteJob = remoteJob.waitFor();
    assertNull(remoteJob.getStatus().getError());
}
Also used : TableId(com.google.cloud.bigquery.TableId) Job(com.google.cloud.bigquery.Job) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) Test(org.junit.Test)

Example 30 with Job

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

Job (com.google.cloud.bigquery.Job)30 Test (org.junit.Test)21 TableId (com.google.cloud.bigquery.TableId)11 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)10 JobConfiguration (com.google.cloud.bigquery.JobConfiguration)8 JobInfo (com.google.cloud.bigquery.JobInfo)8 Table (com.google.cloud.bigquery.Table)4 CopyJobConfiguration (com.google.cloud.bigquery.CopyJobConfiguration)3 FieldValue (com.google.cloud.bigquery.FieldValue)3 LoadStatistics (com.google.cloud.bigquery.JobStatistics.LoadStatistics)3 LoadJobConfiguration (com.google.cloud.bigquery.LoadJobConfiguration)3 StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)3 TableDataWriteChannel (com.google.cloud.bigquery.TableDataWriteChannel)3 TableInfo (com.google.cloud.bigquery.TableInfo)3 WriteChannelConfiguration (com.google.cloud.bigquery.WriteChannelConfiguration)3 TimeoutException (java.util.concurrent.TimeoutException)3 JobId (com.google.cloud.bigquery.JobId)2 JobStatistics (com.google.cloud.bigquery.JobStatistics)2 JobStatus (com.google.cloud.bigquery.JobStatus)2 QueryResponse (com.google.cloud.bigquery.QueryResponse)2