Search in sources :

Example 11 with Table

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

the class BigQuerySnippets method getTableFromId.

/**
   * Example of getting a table.
   */
// [TARGET getTable(TableId, TableOption...)]
// [VARIABLE "my_project_id"]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
public Table getTableFromId(String projectId, String datasetName, String tableName) {
    // [START getTableFromId]
    TableId tableId = TableId.of(projectId, datasetName, tableName);
    Table table = bigquery.getTable(tableId);
    // [END getTableFromId]
    return table;
}
Also used : TableId(com.google.cloud.bigquery.TableId) Table(com.google.cloud.bigquery.Table)

Example 12 with Table

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

the class BigQuerySnippets method listTablesFromId.

/**
   * Example of listing the tables in a dataset.
   */
// [TARGET listTables(DatasetId, TableListOption...)]
// [VARIABLE "my_project_id"]
// [VARIABLE "my_dataset_name"]
public Page<Table> listTablesFromId(String projectId, String datasetName) {
    // [START listTablesFromId]
    DatasetId datasetId = DatasetId.of(projectId, datasetName);
    Page<Table> tables = bigquery.listTables(datasetId, TableListOption.pageSize(100));
    for (Table table : tables.iterateAll()) {
    // do something with the table
    }
    // [END listTablesFromId]
    return tables;
}
Also used : Table(com.google.cloud.bigquery.Table) DatasetId(com.google.cloud.bigquery.DatasetId)

Example 13 with Table

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

the class ITBigQueryTest method testUpdateTableWithSelectedFields.

@Test
public void testUpdateTableWithSelectedFields() {
    String tableName = "test_update_with_selected_fields_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("newDescr").build(), TableOption.fields(TableField.DESCRIPTION));
    assertTrue(updatedTable.getDefinition() instanceof StandardTableDefinition);
    assertEquals(DATASET, updatedTable.getTableId().getDataset());
    assertEquals(tableName, updatedTable.getTableId().getTable());
    assertEquals("newDescr", updatedTable.getDescription());
    assertNull(updatedTable.getDefinition().getSchema());
    assertNull(updatedTable.getLastModifiedTime());
    assertNull(updatedTable.<StandardTableDefinition>getDefinition().getNumBytes());
    assertNull(updatedTable.<StandardTableDefinition>getDefinition().getNumRows());
    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 14 with Table

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

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

the class ITBigQueryTest method testCreateAndGetTableWithSelectedField.

@Test
public void testCreateAndGetTableWithSelectedField() {
    String tableName = "test_create_and_get_selected_fields_table";
    TableId tableId = TableId.of(DATASET, tableName);
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
    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, TableOption.fields(TableField.CREATION_TIME));
    assertNotNull(remoteTable);
    assertTrue(remoteTable.getDefinition() instanceof StandardTableDefinition);
    assertEquals(createdTable.getTableId(), remoteTable.getTableId());
    assertEquals(TableDefinition.Type.TABLE, remoteTable.getDefinition().getType());
    assertNotNull(remoteTable.getCreationTime());
    assertNull(remoteTable.getDefinition().getSchema());
    assertNull(remoteTable.getLastModifiedTime());
    assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes());
    assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows());
    assertNull(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) 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