use of com.google.cloud.bigquery.TableId 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));
}
use of com.google.cloud.bigquery.TableId in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQuerySnippets method testInsertAllAndListTableData.
@Test
public void testInsertAllAndListTableData() throws IOException, InterruptedException {
String tableName = "test_insert_all_and_list_table_data";
String fieldName1 = "booleanField";
String fieldName2 = "bytesField";
String fieldName3 = "recordField";
String fieldName4 = "stringField";
TableId tableId = TableId.of(DATASET, tableName);
Schema schema = Schema.of(Field.of(fieldName1, Type.bool()), Field.of(fieldName2, Type.bytes()), Field.of(fieldName3, Type.record(Field.of(fieldName4, Type.string()))));
TableInfo table = TableInfo.of(tableId, StandardTableDefinition.of(schema));
assertNotNull(bigquery.create(table));
InsertAllResponse response = bigquerySnippets.insertAll(DATASET, tableName);
assertFalse(response.hasErrors());
assertTrue(response.getInsertErrors().isEmpty());
Page<List<FieldValue>> listPage = bigquerySnippets.listTableDataFromId(DATASET, tableName);
while (Iterators.size(listPage.iterateAll().iterator()) < 1) {
Thread.sleep(500);
listPage = bigquerySnippets.listTableDataFromId(DATASET, tableName);
}
List<FieldValue> row = listPage.getValues().iterator().next();
assertEquals(true, row.get(0).getBooleanValue());
assertArrayEquals(new byte[] { 0xA, 0xD, 0xD, 0xE, 0xD }, row.get(1).getBytesValue());
assertEquals("Hello, World!", row.get(2).getRecordValue().get(0).getStringValue());
assertTrue(bigquerySnippets.deleteTable(DATASET, tableName));
}
use of com.google.cloud.bigquery.TableId in project google-cloud-java by GoogleCloudPlatform.
the class BigQuerySnippets method writeFileToTable.
/**
* Example of writing a local file to a table.
*/
// [TARGET writer(WriteChannelConfiguration)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE FileSystems.getDefault().getPath(".", "my-data.csv")]
public long writeFileToTable(String datasetName, String tableName, Path csvPath) throws IOException, InterruptedException, TimeoutException {
// [START writeFileToTable]
TableId tableId = TableId.of(datasetName, tableName);
WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build();
TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration);
// Write data to writer
try (OutputStream stream = Channels.newOutputStream(writer)) {
Files.copy(csvPath, stream);
}
// Get load job
Job job = writer.getJob();
job = job.waitFor();
LoadStatistics stats = job.getStatistics();
return stats.getOutputRows();
// [END writeFileToTable]
}
use of com.google.cloud.bigquery.TableId in project google-cloud-java by GoogleCloudPlatform.
the class BigQuerySnippets method deleteTableFromId.
/**
* Example of deleting a table.
*/
// [TARGET delete(TableId)]
// [VARIABLE "my_project_id"]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
public Boolean deleteTableFromId(String projectId, String datasetName, String tableName) {
// [START deleteTableFromId]
TableId tableId = TableId.of(projectId, datasetName, tableName);
Boolean deleted = bigquery.delete(tableId);
if (deleted) {
// the table was deleted
} else {
// the table was not found
}
// [END deleteTableFromId]
return deleted;
}
use of com.google.cloud.bigquery.TableId 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;
}
Aggregations