Java Code Examples for com.google.api.services.bigquery.model.TableDataInsertAllResponse#InsertErrors

The following examples show how to use com.google.api.services.bigquery.model.TableDataInsertAllResponse#InsertErrors . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextToBigQueryStreamingTest.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
@Test
public void wrapBigQueryInsertErrorReturnsValidJSON() {
  TableRow testRow =
      new TableRow()
          .set(NAME_KEY, testPerson.name)
          .set(AGE_KEY, testPerson.age);
  InsertErrors insertErrors = new TableDataInsertAllResponse.InsertErrors();
  ErrorProto errorProto = new ErrorProto().setMessage(ERROR_MESSAGE);
  insertErrors.setErrors(ImmutableList.of(errorProto));
  TableReference tableReference = new TableReference();
  BigQueryInsertError bigQueryInsertError =
      new BigQueryInsertError(testRow.clone(), insertErrors, tableReference);
  String expected = GSON.toJson(testPerson);

  FailsafeElement<String, String> wrappedValue =
      TextToBigQueryStreaming.wrapBigQueryInsertError(bigQueryInsertError);
  String actualOriginalPayload = wrappedValue.getOriginalPayload();
  String actualPayload = wrappedValue.getPayload();
  String actualErrorMessage = wrappedValue.getErrorMessage();

  assertThat(actualOriginalPayload).isEqualTo(expected);
  assertThat(actualPayload).isEqualTo(expected);
  assertThat(actualErrorMessage).isEqualTo(GSON.toJson(insertErrors));
}
 
Example 2
Source File: FakeDatasetService.java    From beam with Apache License 2.0 6 votes vote down vote up
Map<TableRow, List<TableDataInsertAllResponse.InsertErrors>> getInsertErrors() {
  Map<TableRow, List<TableDataInsertAllResponse.InsertErrors>> parsedInsertErrors =
      Maps.newHashMap();
  synchronized (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;
}
 
Example 3
Source File: ErrorConvertersTest.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a {@link InsertErrors} used by {@link BigQueryInsertError}.
 *
 * @param error string to be added to {@link BigQueryInsertError}
 */
private static InsertErrors getInsertErrors(String error) {
  InsertErrors insertErrors = new TableDataInsertAllResponse.InsertErrors();
  ErrorProto errorProto = new ErrorProto().setMessage(error);
  insertErrors.setErrors(Lists.newArrayList(errorProto));
  return insertErrors;
}
 
Example 4
Source File: BigQueryInsertErrorCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public BigQueryInsertError decode(InputStream inStream) throws IOException {
  TableDataInsertAllResponse.InsertErrors err =
      MAPPER.readValue(
          StringUtf8Coder.of().decode(inStream), TableDataInsertAllResponse.InsertErrors.class);
  TableRow row = TableRowJsonCoder.of().decode(inStream);
  TableReference ref = BigQueryHelpers.parseTableSpec(StringUtf8Coder.of().decode(inStream));
  return new BigQueryInsertError(row, err, ref);
}
 
Example 5
Source File: FakeDatasetService.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Cause a given {@link TableRow} object to fail when it's inserted. The errors link the list will
 * be returned on subsequent retries, and the insert will succeed when the errors run out.
 */
public void failOnInsert(
    Map<TableRow, List<TableDataInsertAllResponse.InsertErrors>> insertErrors) {
  synchronized (tables) {
    for (Map.Entry<TableRow, List<TableDataInsertAllResponse.InsertErrors>> entry :
        insertErrors.entrySet()) {
      List<String> errorStrings = Lists.newArrayList();
      for (TableDataInsertAllResponse.InsertErrors errors : entry.getValue()) {
        errorStrings.add(BigQueryHelpers.toJsonString(errors));
      }
      this.insertErrors.put(BigQueryHelpers.toJsonString(entry.getKey()), errorStrings);
    }
  }
}
 
Example 6
Source File: InsertRetryPolicyTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private TableDataInsertAllResponse.InsertErrors generateErrorAmongMany(
    int numErrors, String baseReason, String exceptionalReason) {
  // The retry policies are expected to search through the entire list of ErrorProtos to determine
  // whether to retry. Stick the exceptionalReason in a random position to exercise this.
  List<ErrorProto> errorProtos = Lists.newArrayListWithExpectedSize(numErrors);
  int exceptionalPosition = ThreadLocalRandom.current().nextInt(numErrors);
  for (int i = 0; i < numErrors; ++i) {
    ErrorProto error = new ErrorProto();
    error.setReason((i == exceptionalPosition) ? exceptionalReason : baseReason);
    errorProtos.add(error);
  }
  TableDataInsertAllResponse.InsertErrors errors = new TableDataInsertAllResponse.InsertErrors();
  errors.setErrors(errorProtos);
  return errors;
}
 
Example 7
Source File: BigQueryUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private void onInsertAll(List<List<Long>> errorIndicesSequence) throws Exception {
  when(mockClient.tabledata()).thenReturn(mockTabledata);

  final List<TableDataInsertAllResponse> responses = new ArrayList<>();
  for (List<Long> errorIndices : errorIndicesSequence) {
    List<TableDataInsertAllResponse.InsertErrors> errors = new ArrayList<>();
    for (long i : errorIndices) {
      TableDataInsertAllResponse.InsertErrors error =
          new TableDataInsertAllResponse.InsertErrors();
      error.setIndex(i);
    }
    TableDataInsertAllResponse response = new TableDataInsertAllResponse();
    response.setInsertErrors(errors);
    responses.add(response);
  }

  doAnswer(
          invocation -> {
            Bigquery.Tabledata.InsertAll mockInsertAll = mock(Bigquery.Tabledata.InsertAll.class);
            when(mockInsertAll.execute())
                .thenReturn(
                    responses.get(0),
                    responses
                        .subList(1, responses.size())
                        .toArray(new TableDataInsertAllResponse[responses.size() - 1]));
            return mockInsertAll;
          })
      .when(mockTabledata)
      .insertAll(anyString(), anyString(), anyString(), any(TableDataInsertAllRequest.class));
}
 
Example 8
Source File: BigQueryIOWriteTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailuresNoRetryPolicy() throws Exception {
  TableRow row1 = new TableRow().set("name", "a").set("number", "1");
  TableRow row2 = new TableRow().set("name", "b").set("number", "2");
  TableRow row3 = new TableRow().set("name", "c").set("number", "3");

  TableDataInsertAllResponse.InsertErrors ephemeralError =
      new TableDataInsertAllResponse.InsertErrors()
          .setErrors(ImmutableList.of(new ErrorProto().setReason("timeout")));

  fakeDatasetService.failOnInsert(
      ImmutableMap.of(
          row1, ImmutableList.of(ephemeralError, ephemeralError),
          row2, ImmutableList.of(ephemeralError, ephemeralError)));

  p.apply(Create.of(row1, row2, row3))
      .apply(
          BigQueryIO.writeTableRows()
              .to("project-id:dataset-id.table-id")
              .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
              .withMethod(BigQueryIO.Write.Method.STREAMING_INSERTS)
              .withSchema(
                  new TableSchema()
                      .setFields(
                          ImmutableList.of(
                              new TableFieldSchema().setName("name").setType("STRING"),
                              new TableFieldSchema().setName("number").setType("INTEGER"))))
              .withTestServices(fakeBqServices)
              .withoutValidation());
  p.run();

  assertThat(
      fakeDatasetService.getAllRows("project-id", "dataset-id", "table-id"),
      containsInAnyOrder(row1, row2, row3));
}
 
Example 9
Source File: ErrorContainer.java    From beam with Apache License 2.0 4 votes vote down vote up
void add(
List<ValueInSingleWindow<T>> failedInserts,
TableDataInsertAllResponse.InsertErrors error,
TableReference ref,
ValueInSingleWindow<TableRow> tableRow);
 
Example 10
Source File: InsertRetryPolicy.java    From beam with Apache License 2.0 4 votes vote down vote up
public TableDataInsertAllResponse.InsertErrors getInsertErrors() {
  return errors;
}
 
Example 11
Source File: InsertRetryPolicy.java    From beam with Apache License 2.0 4 votes vote down vote up
public Context(TableDataInsertAllResponse.InsertErrors errors) {
  this.errors = errors;
}
 
Example 12
Source File: BigQueryInsertError.java    From beam with Apache License 2.0 4 votes vote down vote up
public BigQueryInsertError(
    TableRow row, TableDataInsertAllResponse.InsertErrors error, TableReference table) {
  this.row = row;
  this.error = error;
  this.table = table;
}
 
Example 13
Source File: BigQueryInsertError.java    From beam with Apache License 2.0 4 votes vote down vote up
public TableDataInsertAllResponse.InsertErrors getError() {
  return error;
}
 
Example 14
Source File: FakeDatasetService.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public <T> long insertAll(
    TableReference ref,
    List<ValueInSingleWindow<TableRow>> rowList,
    @Nullable List<String> insertIdList,
    InsertRetryPolicy retryPolicy,
    List<ValueInSingleWindow<T>> failedInserts,
    ErrorContainer<T> errorContainer,
    boolean skipInvalidRows,
    boolean ignoreUnknownValues,
    boolean ignoreInsertIds)
    throws IOException, InterruptedException {
  Map<TableRow, List<TableDataInsertAllResponse.InsertErrors>> insertErrors = getInsertErrors();
  synchronized (tables) {
    if (ignoreInsertIds) {
      insertIdList = null;
    }

    if (insertIdList != null) {
      assertEquals(rowList.size(), insertIdList.size());
    }

    long dataSize = 0;
    TableContainer tableContainer =
        getTableContainer(
            ref.getProjectId(),
            ref.getDatasetId(),
            BigQueryHelpers.stripPartitionDecorator(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) {
        if (insertIdList == null) {
          dataSize += tableContainer.addRow(row, null);
        } else {
          dataSize += tableContainer.addRow(row, insertIdList.get(i));
        }
      } else {
        errorContainer.add(
            failedInserts, allErrors.get(allErrors.size() - 1), ref, rowList.get(i));
      }
    }
    return dataSize;
  }
}
 
Example 15
Source File: BigQueryIOWriteTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testRetryPolicy() throws Exception {
  TableRow row1 = new TableRow().set("name", "a").set("number", "1");
  TableRow row2 = new TableRow().set("name", "b").set("number", "2");
  TableRow row3 = new TableRow().set("name", "c").set("number", "3");

  TableDataInsertAllResponse.InsertErrors ephemeralError =
      new TableDataInsertAllResponse.InsertErrors()
          .setErrors(ImmutableList.of(new ErrorProto().setReason("timeout")));
  TableDataInsertAllResponse.InsertErrors persistentError =
      new TableDataInsertAllResponse.InsertErrors()
          .setErrors(ImmutableList.of(new ErrorProto().setReason("invalidQuery")));

  fakeDatasetService.failOnInsert(
      ImmutableMap.of(
          row1, ImmutableList.of(ephemeralError, ephemeralError),
          row2, ImmutableList.of(ephemeralError, ephemeralError, persistentError)));

  PCollection<TableRow> failedRows =
      p.apply(Create.of(row1, row2, row3))
          .apply(
              BigQueryIO.writeTableRows()
                  .to("project-id:dataset-id.table-id")
                  .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
                  .withMethod(BigQueryIO.Write.Method.STREAMING_INSERTS)
                  .withSchema(
                      new TableSchema()
                          .setFields(
                              ImmutableList.of(
                                  new TableFieldSchema().setName("name").setType("STRING"),
                                  new TableFieldSchema().setName("number").setType("INTEGER"))))
                  .withFailedInsertRetryPolicy(InsertRetryPolicy.retryTransientErrors())
                  .withTestServices(fakeBqServices)
                  .withoutValidation())
          .getFailedInserts();
  // row2 finally fails with a non-retryable error, so we expect to see it in the collection of
  // failed rows.
  PAssert.that(failedRows).containsInAnyOrder(row2);
  p.run();

  // Only row1 and row3 were successfully inserted.
  assertThat(
      fakeDatasetService.getAllRows("project-id", "dataset-id", "table-id"),
      containsInAnyOrder(row1, row3));
}
 
Example 16
Source File: BigQueryIOWriteTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testExtendedErrorRetrieval() throws Exception {
  TableRow row1 = new TableRow().set("name", "a").set("number", "1");
  TableRow row2 = new TableRow().set("name", "b").set("number", "2");
  TableRow row3 = new TableRow().set("name", "c").set("number", "3");
  String tableSpec = "project-id:dataset-id.table-id";

  TableDataInsertAllResponse.InsertErrors ephemeralError =
      new TableDataInsertAllResponse.InsertErrors()
          .setErrors(ImmutableList.of(new ErrorProto().setReason("timeout")));
  TableDataInsertAllResponse.InsertErrors persistentError =
      new TableDataInsertAllResponse.InsertErrors()
          .setErrors(Lists.newArrayList(new ErrorProto().setReason("invalidQuery")));

  fakeDatasetService.failOnInsert(
      ImmutableMap.of(
          row1, ImmutableList.of(ephemeralError, ephemeralError),
          row2, ImmutableList.of(ephemeralError, ephemeralError, persistentError)));

  PCollection<BigQueryInsertError> failedRows =
      p.apply(Create.of(row1, row2, row3))
          .apply(
              BigQueryIO.writeTableRows()
                  .to(tableSpec)
                  .withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
                  .withMethod(BigQueryIO.Write.Method.STREAMING_INSERTS)
                  .withSchema(
                      new TableSchema()
                          .setFields(
                              ImmutableList.of(
                                  new TableFieldSchema().setName("name").setType("STRING"),
                                  new TableFieldSchema().setName("number").setType("INTEGER"))))
                  .withFailedInsertRetryPolicy(InsertRetryPolicy.retryTransientErrors())
                  .withTestServices(fakeBqServices)
                  .withoutValidation()
                  .withExtendedErrorInfo())
          .getFailedInsertsWithErr();

  // row2 finally fails with a non-retryable error, so we expect to see it in the collection of
  // failed rows.
  PAssert.that(failedRows)
      .containsInAnyOrder(
          new BigQueryInsertError(
              row2, persistentError, BigQueryHelpers.parseTableSpec(tableSpec)));
  p.run();

  // Only row1 and row3 were successfully inserted.
  assertThat(
      fakeDatasetService.getAllRows("project-id", "dataset-id", "table-id"),
      containsInAnyOrder(row1, row3));
}