Search in sources :

Example 6 with TableRow

use of com.google.api.services.bigquery.model.TableRow in project beam by apache.

the class FakeBigQueryServices method encodeQuery.

static String encodeQuery(List<TableRow> rows) throws IOException {
    ListCoder<TableRow> listCoder = ListCoder.of(TableRowJsonCoder.of());
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    listCoder.encode(rows, output, Context.OUTER);
    return Base64.encodeBase64String(output.toByteArray());
}
Also used : TableRow(com.google.api.services.bigquery.model.TableRow) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with TableRow

use of com.google.api.services.bigquery.model.TableRow in project beam by apache.

the class FakeBigQueryServices method rowsFromEncodedQuery.

static List<TableRow> rowsFromEncodedQuery(String query) throws IOException {
    ListCoder<TableRow> listCoder = ListCoder.of(TableRowJsonCoder.of());
    ByteArrayInputStream input = new ByteArrayInputStream(Base64.decodeBase64(query));
    List<TableRow> rows = listCoder.decode(input, Context.OUTER);
    for (TableRow row : rows) {
        convertNumbers(row);
    }
    return rows;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TableRow(com.google.api.services.bigquery.model.TableRow)

Example 8 with TableRow

use of com.google.api.services.bigquery.model.TableRow in project beam by apache.

the class FakeDatasetService method insertAll.

@Override
public long insertAll(TableReference ref, List<ValueInSingleWindow<TableRow>> rowList, @Nullable List<String> insertIdList, InsertRetryPolicy retryPolicy, List<ValueInSingleWindow<TableRow>> failedInserts) throws IOException, InterruptedException {
    Map<TableRow, List<TableDataInsertAllResponse.InsertErrors>> insertErrors = getInsertErrors();
    synchronized (BigQueryIOTest.tables) {
        if (insertIdList != null) {
            assertEquals(rowList.size(), insertIdList.size());
        } else {
            insertIdList = Lists.newArrayListWithExpectedSize(rowList.size());
            for (int i = 0; i < rowList.size(); ++i) {
                insertIdList.add(Integer.toString(ThreadLocalRandom.current().nextInt()));
            }
        }
        long dataSize = 0;
        TableContainer tableContainer = getTableContainer(ref.getProjectId(), ref.getDatasetId(), ref.getTableId());
        for (int i = 0; i < rowList.size(); ++i) {
            TableRow row = rowList.get(i).getValue();
            List<TableDataInsertAllResponse.InsertErrors> allErrors = insertErrors.get(row);
            boolean shouldInsert = true;
            if (allErrors != null) {
                for (TableDataInsertAllResponse.InsertErrors errors : allErrors) {
                    if (!retryPolicy.shouldRetry(new Context(errors))) {
                        shouldInsert = false;
                    }
                }
            }
            if (shouldInsert) {
                dataSize += tableContainer.addRow(row, insertIdList.get(i));
            } else {
                failedInserts.add(rowList.get(i));
            }
        }
        return dataSize;
    }
}
Also used : Context(org.apache.beam.sdk.io.gcp.bigquery.InsertRetryPolicy.Context) TableRow(com.google.api.services.bigquery.model.TableRow) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) List(java.util.List)

Example 9 with TableRow

use of com.google.api.services.bigquery.model.TableRow in project beam by apache.

the class FakeDatasetService method getInsertErrors.

Map<TableRow, List<TableDataInsertAllResponse.InsertErrors>> getInsertErrors() {
    Map<TableRow, List<TableDataInsertAllResponse.InsertErrors>> parsedInsertErrors = Maps.newHashMap();
    synchronized (BigQueryIOTest.tables) {
        for (Map.Entry<String, List<String>> entry : this.insertErrors.entrySet()) {
            TableRow tableRow = BigQueryHelpers.fromJsonString(entry.getKey(), TableRow.class);
            List<TableDataInsertAllResponse.InsertErrors> allErrors = Lists.newArrayList();
            for (String errorsString : entry.getValue()) {
                allErrors.add(BigQueryHelpers.fromJsonString(errorsString, TableDataInsertAllResponse.InsertErrors.class));
            }
            parsedInsertErrors.put(tableRow, allErrors);
        }
    }
    return parsedInsertErrors;
}
Also used : TableRow(com.google.api.services.bigquery.model.TableRow) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with TableRow

use of com.google.api.services.bigquery.model.TableRow in project beam by apache.

the class BigQueryServicesImplTest method testInsertRetrySelectRows.

/**
   * Tests that {@link DatasetServiceImpl#insertAll} retries selected rows on failure.
   */
@Test
public void testInsertRetrySelectRows() throws Exception {
    TableReference ref = new TableReference().setProjectId("project").setDatasetId("dataset").setTableId("table");
    List<ValueInSingleWindow<TableRow>> rows = ImmutableList.of(wrapTableRow(new TableRow().set("row", "a")), wrapTableRow(new TableRow().set("row", "b")));
    List<String> insertIds = ImmutableList.of("a", "b");
    final TableDataInsertAllResponse bFailed = new TableDataInsertAllResponse().setInsertErrors(ImmutableList.of(new InsertErrors().setIndex(1L).setErrors(ImmutableList.of(new ErrorProto()))));
    final TableDataInsertAllResponse allRowsSucceeded = new TableDataInsertAllResponse();
    when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
    when(response.getStatusCode()).thenReturn(200).thenReturn(200);
    when(response.getContent()).thenReturn(toStream(bFailed)).thenReturn(toStream(allRowsSucceeded));
    DatasetServiceImpl dataService = new DatasetServiceImpl(bigquery, PipelineOptionsFactory.create());
    dataService.insertAll(ref, rows, insertIds, BackOffAdapter.toGcpBackOff(TEST_BACKOFF.backoff()), new MockSleeper(), InsertRetryPolicy.alwaysRetry(), null);
    verify(response, times(2)).getStatusCode();
    verify(response, times(2)).getContent();
    verify(response, times(2)).getContentType();
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference) ErrorProto(com.google.api.services.bigquery.model.ErrorProto) DatasetServiceImpl(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServicesImpl.DatasetServiceImpl) TableRow(com.google.api.services.bigquery.model.TableRow) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) ValueInSingleWindow(org.apache.beam.sdk.values.ValueInSingleWindow) InsertErrors(com.google.api.services.bigquery.model.TableDataInsertAllResponse.InsertErrors) Matchers.containsString(org.hamcrest.Matchers.containsString) MockSleeper(com.google.api.client.testing.util.MockSleeper) Test(org.junit.Test)

Aggregations

TableRow (com.google.api.services.bigquery.model.TableRow)73 Test (org.junit.Test)43 TableReference (com.google.api.services.bigquery.model.TableReference)24 TableSchema (com.google.api.services.bigquery.model.TableSchema)18 Pipeline (org.apache.beam.sdk.Pipeline)16 KV (org.apache.beam.sdk.values.KV)15 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)14 JsonSchemaToTableSchema (org.apache.beam.sdk.io.gcp.bigquery.BigQueryHelpers.JsonSchemaToTableSchema)14 BigQueryHelpers.toJsonString (org.apache.beam.sdk.io.gcp.bigquery.BigQueryHelpers.toJsonString)13 TestPipeline (org.apache.beam.sdk.testing.TestPipeline)12 BigQueryHelpers.createTempTableReference (org.apache.beam.sdk.io.gcp.bigquery.BigQueryHelpers.createTempTableReference)11 Table (com.google.api.services.bigquery.model.Table)10 HashBasedTable (com.google.common.collect.HashBasedTable)10 JobStatus (com.google.api.services.bigquery.model.JobStatus)9 TableDataInsertAllResponse (com.google.api.services.bigquery.model.TableDataInsertAllResponse)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Map (java.util.Map)8 ValueInSingleWindow (org.apache.beam.sdk.values.ValueInSingleWindow)7 JobStatistics (com.google.api.services.bigquery.model.JobStatistics)6