Search in sources :

Example 1 with StandardTableDefinition

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

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

Example 3 with StandardTableDefinition

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

the class ITBigQueryTest method testUpdateTable.

@Test
public void testUpdateTable() {
    String tableName = "test_update_table";
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    Table updatedTable = bigquery.update(tableInfo.toBuilder().setDescription("newDescription").build());
    assertEquals(DATASET, updatedTable.getTableId().getDataset());
    assertEquals(tableName, updatedTable.getTableId().getTable());
    assertEquals(TABLE_SCHEMA, updatedTable.getDefinition().getSchema());
    assertEquals("newDescription", updatedTable.getDescription());
    assertTrue(updatedTable.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)

Example 4 with StandardTableDefinition

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

the class ITBigQueryTest method testCreateAndGetTable.

@Test
public void testCreateAndGetTable() {
    String tableName = "test_create_and_get_table";
    TableId tableId = TableId.of(DATASET, tableName);
    TimePartitioning partitioning = TimePartitioning.of(Type.DAY);
    StandardTableDefinition tableDefinition = StandardTableDefinition.newBuilder().setSchema(TABLE_SCHEMA).setTimePartitioning(partitioning).build();
    Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
    assertNotNull(createdTable);
    assertEquals(DATASET, createdTable.getTableId().getDataset());
    assertEquals(tableName, createdTable.getTableId().getTable());
    Table remoteTable = bigquery.getTable(DATASET, tableName);
    assertNotNull(remoteTable);
    assertTrue(remoteTable.getDefinition() instanceof StandardTableDefinition);
    assertEquals(createdTable.getTableId(), remoteTable.getTableId());
    assertEquals(TableDefinition.Type.TABLE, remoteTable.getDefinition().getType());
    assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
    assertNotNull(remoteTable.getCreationTime());
    assertNotNull(remoteTable.getLastModifiedTime());
    assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes());
    assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows());
    assertEquals(partitioning, remoteTable.<StandardTableDefinition>getDefinition().getTimePartitioning());
    assertTrue(remoteTable.delete());
}
Also used : TableId(com.google.cloud.bigquery.TableId) Table(com.google.cloud.bigquery.Table) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) TimePartitioning(com.google.cloud.bigquery.TimePartitioning) Test(org.junit.Test)

Example 5 with StandardTableDefinition

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

the class DatasetSnippets method createTable.

/**
   * Example of creating a table in the dataset with schema and time partitioning.
   */
// [TARGET create(String, TableDefinition, TableOption...)]
// [VARIABLE “my_table”]
// [VARIABLE “my_field”]
public Table createTable(String tableName, String fieldName) {
    // [START createTable]
    Schema schema = Schema.of(Field.of(fieldName, Type.string()));
    StandardTableDefinition definition = StandardTableDefinition.newBuilder().setSchema(schema).setTimePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY)).build();
    Table table = dataset.create(tableName, definition);
    // [END createTable]
    return table;
}
Also used : Table(com.google.cloud.bigquery.Table) Schema(com.google.cloud.bigquery.Schema) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition)

Aggregations

StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)14 Test (org.junit.Test)11 Table (com.google.cloud.bigquery.Table)10 TableInfo (com.google.cloud.bigquery.TableInfo)9 TableId (com.google.cloud.bigquery.TableId)6 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)4 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)4 CopyJobConfiguration (com.google.cloud.bigquery.CopyJobConfiguration)3 Job (com.google.cloud.bigquery.Job)3 Schema (com.google.cloud.bigquery.Schema)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BigQuery (com.google.cloud.bigquery.BigQuery)1 Field (com.google.cloud.bigquery.Field)1 FieldValue (com.google.cloud.bigquery.FieldValue)1 QueryRequest (com.google.cloud.bigquery.QueryRequest)1 QueryResponse (com.google.cloud.bigquery.QueryResponse)1 TimePartitioning (com.google.cloud.bigquery.TimePartitioning)1 Gson (com.google.gson.Gson)1