Search in sources :

Example 1 with Table

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

the class ITTableSnippets method testUpdate.

@Test
public void testUpdate() {
    Table updatedTable = tableSnippets.update();
    assertEquals("new description", updatedTable.getDescription());
}
Also used : Table(com.google.cloud.bigquery.Table) Test(org.junit.Test)

Example 2 with Table

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

the class ITTableSnippets method testDelete.

@Test
public void testDelete() {
    Table doomedTable = bigquery.create(TableInfo.of(DOOMED_TABLE_ID, StandardTableDefinition.of(SCHEMA)));
    TableSnippets doomedTableSnippets = new TableSnippets(doomedTable);
    assertTrue(doomedTableSnippets.delete());
}
Also used : Table(com.google.cloud.bigquery.Table) Test(org.junit.Test)

Example 3 with Table

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

the class ITBigQueryTest method testCreateAndGetJob.

@Test
public void testCreateAndGetJob() throws InterruptedException, TimeoutException {
    String sourceTableName = "test_create_and_get_job_source_table";
    String destinationTableName = "test_create_and_get_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 copyJobConfiguration = CopyJobConfiguration.of(destinationTable, sourceTable);
    Job createdJob = bigquery.create(JobInfo.of(copyJobConfiguration));
    Job remoteJob = bigquery.getJob(createdJob.getJobId());
    assertEquals(createdJob.getJobId(), remoteJob.getJobId());
    CopyJobConfiguration createdConfiguration = createdJob.getConfiguration();
    CopyJobConfiguration remoteConfiguration = remoteJob.getConfiguration();
    assertEquals(createdConfiguration.getSourceTables(), remoteConfiguration.getSourceTables());
    assertEquals(createdConfiguration.getDestinationTable(), remoteConfiguration.getDestinationTable());
    assertEquals(createdConfiguration.getCreateDisposition(), remoteConfiguration.getCreateDisposition());
    assertEquals(createdConfiguration.getWriteDisposition(), remoteConfiguration.getWriteDisposition());
    assertNotNull(remoteJob.getEtag());
    assertNotNull(remoteJob.getStatistics());
    assertNotNull(remoteJob.getStatus());
    assertEquals(createdJob.getSelfLink(), remoteJob.getSelfLink());
    assertEquals(createdJob.getUserEmail(), 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 4 with Table

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

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

the class ITBigQueryTest method testListTables.

@Test
public void testListTables() {
    String tableName = "test_list_tables";
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    Page<Table> tables = bigquery.listTables(DATASET);
    boolean found = false;
    Iterator<Table> tableIterator = tables.getValues().iterator();
    while (tableIterator.hasNext() && !found) {
        if (tableIterator.next().getTableId().equals(createdTable.getTableId())) {
            found = true;
        }
    }
    assertTrue(found);
    assertTrue(createdTable.delete());
}
Also used : Table(com.google.cloud.bigquery.Table) TableInfo(com.google.cloud.bigquery.TableInfo) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) Test(org.junit.Test)

Aggregations

Table (com.google.cloud.bigquery.Table)24 Test (org.junit.Test)18 StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)11 TableId (com.google.cloud.bigquery.TableId)11 TableInfo (com.google.cloud.bigquery.TableInfo)11 Job (com.google.cloud.bigquery.Job)4 Schema (com.google.cloud.bigquery.Schema)4 CopyJobConfiguration (com.google.cloud.bigquery.CopyJobConfiguration)3 Field (com.google.cloud.bigquery.Field)3 FieldValue (com.google.cloud.bigquery.FieldValue)2 QueryRequest (com.google.cloud.bigquery.QueryRequest)2 QueryResponse (com.google.cloud.bigquery.QueryResponse)2 BigQuery (com.google.cloud.bigquery.BigQuery)1 DatasetId (com.google.cloud.bigquery.DatasetId)1 ExternalTableDefinition (com.google.cloud.bigquery.ExternalTableDefinition)1 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)1 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)1 TableDefinition (com.google.cloud.bigquery.TableDefinition)1 TimePartitioning (com.google.cloud.bigquery.TimePartitioning)1 ViewDefinition (com.google.cloud.bigquery.ViewDefinition)1