Search in sources :

Example 16 with Table

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

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

Example 18 with Table

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

the class ITBigQueryTest method testInsertAllWithSuffix.

@Test
public void testInsertAllWithSuffix() throws InterruptedException {
    String tableName = "test_insert_all_with_suffix_table";
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
    assertNotNull(bigquery.create(tableInfo));
    ImmutableMap.Builder<String, Object> builder1 = ImmutableMap.builder();
    builder1.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
    builder1.put("StringField", "stringValue");
    builder1.put("IntegerArrayField", ImmutableList.of(0, 1));
    builder1.put("BooleanField", false);
    builder1.put("BytesField", BYTES_BASE64);
    builder1.put("RecordField", ImmutableMap.of("TimestampField", "1969-07-20 20:18:04 UTC", "IntegerArrayField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    builder1.put("IntegerField", 5);
    builder1.put("FloatField", 1.2);
    ImmutableMap.Builder<String, Object> builder2 = ImmutableMap.builder();
    builder2.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
    builder2.put("StringField", "stringValue");
    builder2.put("IntegerArrayField", ImmutableList.of(0, 1));
    builder2.put("BooleanField", false);
    builder2.put("BytesField", BYTES_BASE64);
    builder2.put("RecordField", ImmutableMap.of("TimestampField", "1969-07-20 20:18:04 UTC", "IntegerArrayField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
    builder2.put("IntegerField", 5);
    builder2.put("FloatField", 1.2);
    InsertAllRequest request = InsertAllRequest.newBuilder(tableInfo.getTableId()).addRow(builder1.build()).addRow(builder2.build()).setTemplateSuffix("_suffix").build();
    InsertAllResponse response = bigquery.insertAll(request);
    assertFalse(response.hasErrors());
    assertEquals(0, response.getInsertErrors().size());
    String newTableName = tableName + "_suffix";
    Table suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields());
    // wait until the new table is created. If the table is never created the test will time-out
    while (suffixTable == null) {
        Thread.sleep(1000L);
        suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields());
    }
    assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
    assertTrue(suffixTable.delete());
}
Also used : Table(com.google.cloud.bigquery.Table) TableInfo(com.google.cloud.bigquery.TableInfo) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) InsertAllRequest(com.google.cloud.bigquery.InsertAllRequest) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 19 with Table

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

the class BigQuerySnippets method createTable.

/**
   * Example of creating a table.
   */
// [TARGET create(TableInfo, TableOption...)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE "string_field"]
public Table createTable(String datasetName, String tableName, String fieldName) {
    // [START createTable]
    TableId tableId = TableId.of(datasetName, tableName);
    // Table field definition
    Field field = Field.of(fieldName, Field.Type.string());
    // Table schema definition
    Schema schema = Schema.of(field);
    TableDefinition tableDefinition = StandardTableDefinition.of(schema);
    TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
    Table table = bigquery.create(tableInfo);
    // [END createTable]
    return table;
}
Also used : TableId(com.google.cloud.bigquery.TableId) Field(com.google.cloud.bigquery.Field) Table(com.google.cloud.bigquery.Table) Schema(com.google.cloud.bigquery.Schema) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) TableDefinition(com.google.cloud.bigquery.TableDefinition) TableInfo(com.google.cloud.bigquery.TableInfo)

Example 20 with Table

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

the class ITBigQuerySnippets method testCreateGetAndDeleteTable.

@Test
public void testCreateGetAndDeleteTable() throws InterruptedException {
    String tableName = "test_create_get_delete";
    String fieldName = "aField";
    Table table = bigquerySnippets.createTable(DATASET, tableName, fieldName);
    assertNotNull(table);
    TableId tableId = TableId.of(bigquery.getOptions().getProjectId(), DATASET, tableName);
    assertEquals(tableId, bigquerySnippets.getTable(tableId.getDataset(), tableId.getTable()).getTableId());
    assertNotNull(bigquerySnippets.updateTable(DATASET, tableName, "new friendly name"));
    assertEquals("new friendly name", bigquerySnippets.getTableFromId(tableId.getProject(), tableId.getDataset(), tableId.getTable()).getFriendlyName());
    Set<TableId> tables = Sets.newHashSet(Iterators.transform(bigquerySnippets.listTables(DATASET).iterateAll().iterator(), TO_TABLE_ID_FUNCTION));
    while (!tables.contains(tableId)) {
        Thread.sleep(500);
        tables = Sets.newHashSet(Iterators.transform(bigquerySnippets.listTables(DATASET).iterateAll().iterator(), TO_TABLE_ID_FUNCTION));
    }
    tables = Sets.newHashSet(Iterators.transform(bigquerySnippets.listTablesFromId(tableId.getProject(), DATASET).iterateAll().iterator(), TO_TABLE_ID_FUNCTION));
    while (!tables.contains(tableId)) {
        Thread.sleep(500);
        tables = Sets.newHashSet(Iterators.transform(bigquerySnippets.listTablesFromId(tableId.getProject(), DATASET).iterateAll().iterator(), TO_TABLE_ID_FUNCTION));
    }
    assertTrue(bigquerySnippets.deleteTable(DATASET, tableName));
    assertFalse(bigquerySnippets.deleteTableFromId(tableId.getProject(), DATASET, tableName));
}
Also used : TableId(com.google.cloud.bigquery.TableId) Table(com.google.cloud.bigquery.Table) 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