Search in sources :

Example 6 with Table

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

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

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

the class BigQuerySnippets method updateTable.

/**
   * Example of updating a table by changing its friendly name.
   */
// [TARGET update(TableInfo, TableOption...)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE "new_friendly_name"]
public Table updateTable(String datasetName, String tableName, String newFriendlyName) {
    // [START updateTable]
    Table oldTable = bigquery.getTable(datasetName, tableName);
    TableInfo tableInfo = oldTable.toBuilder().setFriendlyName(newFriendlyName).build();
    Table newTable = bigquery.update(tableInfo);
    // [END updateTable]
    return newTable;
}
Also used : Table(com.google.cloud.bigquery.Table) TableInfo(com.google.cloud.bigquery.TableInfo)

Example 9 with Table

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

the class CreateTableAndLoadData method main.

public static void main(String... args) throws InterruptedException, TimeoutException {
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    TableId tableId = TableId.of("dataset", "table");
    Table table = bigquery.getTable(tableId);
    if (table == null) {
        System.out.println("Creating table " + tableId);
        Field integerField = Field.of("fieldName", Field.Type.integer());
        Schema schema = Schema.of(integerField);
        table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
    }
    System.out.println("Loading data into table " + tableId);
    Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
    loadJob = loadJob.waitFor();
    if (loadJob.getStatus().getError() != null) {
        System.out.println("Job completed with errors");
    } else {
        System.out.println("Job succeeded");
    }
}
Also used : TableId(com.google.cloud.bigquery.TableId) Field(com.google.cloud.bigquery.Field) BigQuery(com.google.cloud.bigquery.BigQuery) Table(com.google.cloud.bigquery.Table) Schema(com.google.cloud.bigquery.Schema) Job(com.google.cloud.bigquery.Job)

Example 10 with Table

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

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