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());
}
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());
}
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;
}
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");
}
}
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;
}
Aggregations