use of com.google.cloud.bigquery.TableInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testInsertAllWithErrors.
@Test
public void testInsertAllWithErrors() {
String tableName = "test_insert_all_with_errors_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", "invalidDate");
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);
ImmutableMap.Builder<String, Object> builder3 = ImmutableMap.builder();
builder3.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
builder3.put("StringField", "stringValue");
builder3.put("IntegerArrayField", ImmutableList.of(0, 1));
builder3.put("BooleanField", false);
builder3.put("BytesField", BYTES_BASE64);
InsertAllRequest request = InsertAllRequest.newBuilder(tableInfo.getTableId()).addRow(builder1.build()).addRow(builder2.build()).addRow(builder3.build()).setSkipInvalidRows(true).build();
InsertAllResponse response = bigquery.insertAll(request);
assertTrue(response.hasErrors());
assertEquals(2, response.getInsertErrors().size());
assertNotNull(response.getErrorsFor(1L));
assertNotNull(response.getErrorsFor(2L));
assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
}
use of com.google.cloud.bigquery.TableInfo 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.TableInfo 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;
}
use of com.google.cloud.bigquery.TableInfo 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());
}
use of com.google.cloud.bigquery.TableInfo 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());
}
Aggregations