use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class BigQueryRowCountIT method testPipelineOptionInjection.
/**
* This tests if the pipeline options are injected in the path of SQL Transform.
*/
@Test
public void testPipelineOptionInjection() {
BigQueryTestTableProvider provider = new BigQueryTestTableProvider();
Table table = getTable("testTable", bigQuery.tableSpec());
provider.addTable("testTable", table);
pipeline.apply(Create.of(new TableRow().set("id", 1).set("name", "name1"), new TableRow().set("id", 2).set("name", "name2"), new TableRow().set("id", 3).set("name", "name3")).withCoder(TableRowJsonCoder.of())).apply(BigQueryIO.writeTableRows().to(bigQuery.tableSpec()).withSchema(new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("id").setType("INTEGER"), new TableFieldSchema().setName("name").setType("STRING")))).withoutValidation());
pipeline.run().waitUntilFinish();
// changing pipeline options
readingPipeline.getOptions().setJobName(FAKE_JOB_NAME);
// Reading from the table should update the statistics of bigQuery table
readingPipeline.apply(SqlTransform.query(" select * from testTable ").withDefaultTableProvider("bigquery", provider));
readingPipeline.run().waitUntilFinish();
BigQueryTestTable sqlTable = (BigQueryTestTable) provider.buildBeamSqlTable(table);
assertEquals(FAKE_JOB_NAME, sqlTable.getJobName());
}
use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class BigQueryRowCountIT method testNonEmptyTable.
@Test
public void testNonEmptyTable() {
BigQueryTableProvider provider = new BigQueryTableProvider();
Table table = getTable("testTable", bigQuery.tableSpec());
pipeline.apply(Create.of(new TableRow().set("id", 1).set("name", "name1"), new TableRow().set("id", 2).set("name", "name2"), new TableRow().set("id", 3).set("name", "name3")).withCoder(TableRowJsonCoder.of())).apply(BigQueryIO.writeTableRows().to(bigQuery.tableSpec()).withSchema(new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("id").setType("INTEGER"), new TableFieldSchema().setName("name").setType("STRING")))).withoutValidation());
pipeline.run().waitUntilFinish();
BeamSqlTable sqlTable = provider.buildBeamSqlTable(table);
BeamTableStatistics size1 = sqlTable.getTableStatistics(TestPipeline.testingPipelineOptions());
assertNotNull(size1);
assertEquals(3d, size1.getRowCount(), 0.1);
}
use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class TableRowToStorageApiProto method descriptorSchemaFromTableFieldSchemas.
private static DescriptorProto descriptorSchemaFromTableFieldSchemas(Iterable<TableFieldSchema> tableFieldSchemas) {
DescriptorProto.Builder descriptorBuilder = DescriptorProto.newBuilder();
// Create a unique name for the descriptor ('-' characters cannot be used).
descriptorBuilder.setName("D" + UUID.randomUUID().toString().replace("-", "_"));
int i = 1;
for (TableFieldSchema fieldSchema : tableFieldSchemas) {
fieldDescriptorFromTableField(fieldSchema, i++, descriptorBuilder);
}
return descriptorBuilder.build();
}
use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class BigQueryIOWriteTest method testTriggeredFileLoadsWithTempTables.
public void testTriggeredFileLoadsWithTempTables(String tableRef) throws Exception {
if (useStorageApi || !useStreaming) {
return;
}
List<TableRow> elements = Lists.newArrayList();
for (int i = 0; i < 30; ++i) {
elements.add(new TableRow().set("number", i));
}
TestStream<TableRow> testStream = TestStream.create(TableRowJsonCoder.of()).addElements(elements.get(0), Iterables.toArray(elements.subList(1, 10), TableRow.class)).advanceProcessingTime(Duration.standardMinutes(1)).addElements(elements.get(10), Iterables.toArray(elements.subList(11, 20), TableRow.class)).advanceProcessingTime(Duration.standardMinutes(1)).addElements(elements.get(20), Iterables.toArray(elements.subList(21, 30), TableRow.class)).advanceWatermarkToInfinity();
BigQueryIO.Write.Method method = Method.FILE_LOADS;
p.apply(testStream).apply(BigQueryIO.writeTableRows().to(tableRef).withSchema(new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("number").setType("INTEGER")))).withTestServices(fakeBqServices).withTriggeringFrequency(Duration.standardSeconds(30)).withNumFileShards(2).withMaxBytesPerPartition(1).withMaxFilesPerPartition(1).withMethod(method).withoutValidation());
p.run();
final int projectIdSplitter = tableRef.indexOf(':');
final String projectId = projectIdSplitter == -1 ? "project-id" : tableRef.substring(0, projectIdSplitter);
assertThat(fakeDatasetService.getAllRows(projectId, "dataset-id", "table-id"), containsInAnyOrder(Iterables.toArray(elements, TableRow.class)));
}
use of com.google.api.services.bigquery.model.TableFieldSchema in project beam by apache.
the class BigQueryIOWriteTest method testCreateNever.
@Test
public void testCreateNever() throws Exception {
BigQueryIO.Write.Method method = useStreaming ? (useStorageApi ? (useStorageApiApproximate ? Method.STORAGE_API_AT_LEAST_ONCE : Method.STORAGE_WRITE_API) : Method.STREAMING_INSERTS) : useStorageApi ? Method.STORAGE_WRITE_API : Method.FILE_LOADS;
p.enableAbandonedNodeEnforcement(false);
TableReference tableRef = BigQueryHelpers.parseTableSpec("project-id:dataset-id.table");
TableSchema tableSchema = new TableSchema().setFields(ImmutableList.of(new TableFieldSchema().setName("name").setType("STRING"), new TableFieldSchema().setName("number").setType("INTEGER")));
fakeDatasetService.createTable(new Table().setTableReference(tableRef).setSchema(tableSchema));
PCollection<TableRow> tableRows = p.apply(GenerateSequence.from(0).to(10)).apply(MapElements.via(new SimpleFunction<Long, TableRow>() {
@Override
public TableRow apply(Long input) {
return new TableRow().set("name", "name " + input).set("number", input);
}
})).setCoder(TableRowJsonCoder.of());
tableRows.apply(BigQueryIO.writeTableRows().to(tableRef).withMethod(method).withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_NEVER).withTestServices(fakeBqServices).withoutValidation());
p.run();
}
Aggregations