use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class BigQuerySchemaUpdateOptionsIT method testAllowFieldRelaxation.
@Test
public void testAllowFieldRelaxation() throws Exception {
String tableName = makeTestTable();
Set<SchemaUpdateOption> schemaUpdateOptions = EnumSet.of(BigQueryIO.Write.SchemaUpdateOption.ALLOW_FIELD_RELAXATION);
TableSchema newSchema = new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("optional_field").setType("STRING")));
String value = "hellooo";
TableRow rowToInsert = new TableRow().set("optional_field", value);
String testQuery = String.format("SELECT optional_field FROM [%s.%s];", BIG_QUERY_DATASET_ID, tableName);
List<List<String>> expectedResult = Arrays.asList(Arrays.asList(value));
runWriteTest(schemaUpdateOptions, tableName, newSchema, rowToInsert, testQuery, expectedResult);
}
use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class BigQueryIOReadTest method testEstimatedSizeWithStreamingBuffer.
@Test
public void testEstimatedSizeWithStreamingBuffer() throws Exception {
List<TableRow> data = ImmutableList.of(new TableRow().set("name", "a").set("number", 1L), new TableRow().set("name", "b").set("number", 2L), new TableRow().set("name", "c").set("number", 3L), new TableRow().set("name", "d").set("number", 4L), new TableRow().set("name", "e").set("number", 5L), new TableRow().set("name", "f").set("number", 6L));
TableReference table = BigQueryHelpers.parseTableSpec("project:data_set.table_name");
fakeDatasetService.createDataset("project", "data_set", "", "", null);
fakeDatasetService.createTable(new Table().setTableReference(table).setSchema(new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("name").setType("STRING"), new TableFieldSchema().setName("number").setType("INTEGER")))).setStreamingBuffer(new Streamingbuffer().setEstimatedBytes(BigInteger.valueOf(10))));
fakeDatasetService.insertAll(table, data, null);
String stepUuid = "testStepUuid";
BoundedSource<TableRow> bqSource = BigQueryTableSourceDef.create(fakeBqServices, ValueProvider.StaticValueProvider.of(table)).toSource(stepUuid, TableRowJsonCoder.of(), BigQueryIO.TableRowParser.INSTANCE, false);
PipelineOptions options = PipelineOptionsFactory.create();
// Each row should have 24 bytes (See StringUtf8Coder in detail):
// first 1 byte indicating length and following 23 bytes: {"name":"a","number":1}
// 10 bytes comes from the estimated bytes of the Streamingbuffer
long expectedSize = 24L * data.size() + 10;
assertEquals(expectedSize, bqSource.getEstimatedSizeBytes(options));
}
use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class BigQueryIOReadTest method testEstimatedSizeWithoutStreamingBuffer.
@Test
public void testEstimatedSizeWithoutStreamingBuffer() throws Exception {
List<TableRow> data = ImmutableList.of(new TableRow().set("name", "a").set("number", 1L), new TableRow().set("name", "b").set("number", 2L), new TableRow().set("name", "c").set("number", 3L), new TableRow().set("name", "d").set("number", 4L), new TableRow().set("name", "e").set("number", 5L), new TableRow().set("name", "f").set("number", 6L));
TableReference table = BigQueryHelpers.parseTableSpec("project:data_set.table_name");
fakeDatasetService.createDataset("project", "data_set", "", "", null);
fakeDatasetService.createTable(new Table().setTableReference(table).setSchema(new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("name").setType("STRING"), new TableFieldSchema().setName("number").setType("INTEGER")))));
fakeDatasetService.insertAll(table, data, null);
String stepUuid = "testStepUuid";
BoundedSource<TableRow> bqSource = BigQueryTableSourceDef.create(fakeBqServices, ValueProvider.StaticValueProvider.of(table)).toSource(stepUuid, TableRowJsonCoder.of(), BigQueryIO.TableRowParser.INSTANCE, false);
PipelineOptions options = PipelineOptionsFactory.create();
// Each row should have 24 bytes (See StringUtf8Coder in detail):
// first 1 byte indicating length and following 23 bytes: {"name":"a","number":1}
long expectedSize = 24L * data.size();
assertEquals(expectedSize, bqSource.getEstimatedSizeBytes(options));
}
use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class BigQueryIOStorageQueryTest method testQuerySourceInitialSplit_NoReferencedTables.
/**
* This test simulates the scenario where the SQL text which is executed by the query job doesn't
* by itself refer to any tables (e.g. "SELECT 17 AS value"), and thus there are no referenced
* tables when the dry run of the query is performed.
*/
@Test
public void testQuerySourceInitialSplit_NoReferencedTables() throws Exception {
Table queryResultTable = new Table().setSchema(new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("name").setType("STRING"), new TableFieldSchema().setName("number").setType("INTEGER")))).setNumBytes(1024L * 1024L);
String encodedQuery = FakeBigQueryServices.encodeQueryResult(queryResultTable);
fakeJobService.expectDryRunQuery(options.getProject(), encodedQuery, new JobStatistics().setQuery(new JobStatistics2().setTotalBytesProcessed(1024L * 1024L).setReferencedTables(ImmutableList.of())));
String stepUuid = "testStepUuid";
TableReference tempTableReference = createTempTableReference(options.getProject(), BigQueryResourceNaming.createJobIdPrefix(options.getJobName(), stepUuid, JobType.QUERY), Optional.empty());
CreateReadSessionRequest expectedRequest = CreateReadSessionRequest.newBuilder().setParent("projects/" + options.getProject()).setReadSession(ReadSession.newBuilder().setTable(BigQueryHelpers.toTableResourceName(tempTableReference))).setMaxStreamCount(1024).build();
Schema sessionSchema = SchemaBuilder.record("__root__").fields().name("name").type().nullable().stringType().noDefault().name("number").type().nullable().longType().noDefault().endRecord();
ReadSession.Builder builder = ReadSession.newBuilder().setAvroSchema(AvroSchema.newBuilder().setSchema(sessionSchema.toString())).setDataFormat(DataFormat.AVRO);
for (int i = 0; i < 1024; i++) {
builder.addStreams(ReadStream.newBuilder().setName("stream-" + i));
}
StorageClient fakeStorageClient = mock(StorageClient.class);
when(fakeStorageClient.createReadSession(expectedRequest)).thenReturn(builder.build());
BigQueryStorageQuerySource<TableRow> querySource = BigQueryStorageQuerySource.create(stepUuid, ValueProvider.StaticValueProvider.of(encodedQuery), /* flattenResults = */
true, /* useLegacySql = */
true, /* priority = */
QueryPriority.BATCH, /* location = */
null, /* queryTempDataset = */
null, /* kmsKey = */
null, null, new TableRowParser(), TableRowJsonCoder.of(), new FakeBigQueryServices().withDatasetService(fakeDatasetService).withJobService(fakeJobService).withStorageClient(fakeStorageClient));
List<? extends BoundedSource<TableRow>> sources = querySource.split(1024, options);
assertEquals(1024, sources.size());
}
use of com.google.api.services.bigquery.model.TableFieldSchema in project DataflowJavaSDK-examples by GoogleCloudPlatform.
the class WriteToBigQuery method getSchema.
/**
* Build the output table schema.
*/
protected TableSchema getSchema() {
List<TableFieldSchema> fields = new ArrayList<>();
for (Map.Entry<String, FieldInfo<InputT>> entry : fieldInfo.entrySet()) {
String key = entry.getKey();
FieldInfo<InputT> fcnInfo = entry.getValue();
String bqType = fcnInfo.getFieldType();
fields.add(new TableFieldSchema().setName(key).setType(bqType));
}
return new TableSchema().setFields(fields);
}
Aggregations