use of com.google.api.services.bigquery.model.TableReference in project beam by apache.
the class BigQueryServicesImplTest method testInsertDoesNotRetry.
/**
* Tests that {@link DatasetServiceImpl#insertAll} does not retry non-rate-limited attempts.
*/
@Test
public void testInsertDoesNotRetry() throws Throwable {
TableReference ref = new TableReference().setProjectId("project").setDatasetId("dataset").setTableId("table");
List<ValueInSingleWindow<TableRow>> rows = new ArrayList<>();
rows.add(wrapTableRow(new TableRow()));
// First response is 403 not-rate-limited, second response has valid payload but should not
// be invoked.
when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
when(response.getStatusCode()).thenReturn(403).thenReturn(200);
when(response.getContent()).thenReturn(toStream(errorWithReasonAndStatus("actually forbidden", 403))).thenReturn(toStream(new TableDataInsertAllResponse()));
thrown.expect(GoogleJsonResponseException.class);
thrown.expectMessage("actually forbidden");
DatasetServiceImpl dataService = new DatasetServiceImpl(bigquery, PipelineOptionsFactory.create());
try {
dataService.insertAll(ref, rows, null, BackOffAdapter.toGcpBackOff(TEST_BACKOFF.backoff()), new MockSleeper(), InsertRetryPolicy.alwaysRetry(), null);
fail();
} catch (RuntimeException e) {
verify(response, times(1)).getStatusCode();
verify(response, times(1)).getContent();
verify(response, times(1)).getContentType();
throw e.getCause();
}
}
use of com.google.api.services.bigquery.model.TableReference in project beam by apache.
the class BigQueryIOTest method testWriteValidatesDataset.
private void testWriteValidatesDataset(boolean unbounded) throws Exception {
String projectId = "someproject";
String datasetId = "somedataset";
BigQueryOptions options = TestPipeline.testingPipelineOptions().as(BigQueryOptions.class);
options.setProject(projectId);
FakeBigQueryServices fakeBqServices = new FakeBigQueryServices().withJobService(new FakeJobService()).withDatasetService(new FakeDatasetService());
Pipeline p = TestPipeline.create(options);
TableReference tableRef = new TableReference();
tableRef.setDatasetId(datasetId);
tableRef.setTableId("sometable");
PCollection<TableRow> tableRows;
if (unbounded) {
tableRows = p.apply(GenerateSequence.from(0)).apply(MapElements.via(new SimpleFunction<Long, TableRow>() {
@Override
public TableRow apply(Long input) {
return null;
}
})).setCoder(TableRowJsonCoder.of());
} else {
tableRows = p.apply(Create.empty(TableRowJsonCoder.of()));
}
thrown.expect(RuntimeException.class);
// Message will be one of following depending on the execution environment.
thrown.expectMessage(Matchers.either(Matchers.containsString("Unable to confirm BigQuery dataset presence")).or(Matchers.containsString("BigQuery dataset not found for table")));
tableRows.apply(BigQueryIO.writeTableRows().to(tableRef).withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED).withSchema(new TableSchema()).withTestServices(fakeBqServices));
p.run();
}
use of com.google.api.services.bigquery.model.TableReference in project beam by apache.
the class BigQueryIOTest method testTableParsing_noProjectId.
@Test
public void testTableParsing_noProjectId() {
TableReference ref = BigQueryHelpers.parseTableSpec("data_set.table_name");
Assert.assertEquals(null, ref.getProjectId());
Assert.assertEquals("data_set", ref.getDatasetId());
Assert.assertEquals("table_name", ref.getTableId());
}
use of com.google.api.services.bigquery.model.TableReference in project beam by apache.
the class FakeJobService method runCopyJob.
private JobStatus runCopyJob(JobConfigurationTableCopy copy) throws InterruptedException, IOException {
List<TableReference> sources = copy.getSourceTables();
TableReference destination = copy.getDestinationTable();
WriteDisposition writeDisposition = WriteDisposition.valueOf(copy.getWriteDisposition());
CreateDisposition createDisposition = CreateDisposition.valueOf(copy.getCreateDisposition());
Table existingTable = datasetService.getTable(destination);
if (!validateDispositions(existingTable, createDisposition, writeDisposition)) {
return new JobStatus().setState("FAILED").setErrorResult(new ErrorProto());
}
List<TableRow> allRows = Lists.newArrayList();
for (TableReference source : sources) {
allRows.addAll(datasetService.getAllRows(source.getProjectId(), source.getDatasetId(), source.getTableId()));
}
datasetService.createTable(new Table().setTableReference(destination));
datasetService.insertAll(destination, allRows, null);
return new JobStatus().setState("DONE");
}
use of com.google.api.services.bigquery.model.TableReference in project beam by apache.
the class FakeJobService method runExtractJob.
private JobStatus runExtractJob(Job job, JobConfigurationExtract extract) throws InterruptedException, IOException {
TableReference sourceTable = extract.getSourceTable();
List<TableRow> rows = datasetService.getAllRows(sourceTable.getProjectId(), sourceTable.getDatasetId(), sourceTable.getTableId());
TableSchema schema = datasetService.getTable(sourceTable).getSchema();
List<Long> destinationFileCounts = Lists.newArrayList();
for (String destination : extract.getDestinationUris()) {
destinationFileCounts.add(writeRows(sourceTable.getTableId(), rows, schema, destination));
}
job.setStatistics(new JobStatistics().setExtract(new JobStatistics4().setDestinationUriFileCounts(destinationFileCounts)));
return new JobStatus().setState("DONE");
}
Aggregations