org.apache.kudu.client.OperationResponse Java Examples

The following examples show how to use org.apache.kudu.client.OperationResponse. 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: TestPutKudu.java    From nifi with Apache License 2.0 6 votes vote down vote up
private LinkedList<OperationResponse> queueInsert(MockPutKudu putKudu, KuduSession session, boolean sync, ResultCode... results) throws Exception {
    LinkedList<OperationResponse> responses = new LinkedList<>();
    for (ResultCode result : results) {
        boolean ok = result == OK;
        Tuple<Insert, OperationResponse> tuple = insert(ok);
        putKudu.queue(tuple.getKey());

        if (result == EXCEPTION) {
            when(session.apply(tuple.getKey())).thenThrow(mock(KuduException.class));
            // Stop processing the rest of the records on the first exception
            break;
        } else {
            responses.add(tuple.getValue());
            if (sync) {
                when(session.apply(tuple.getKey())).thenReturn(ok ? null : tuple.getValue());

                // In AUTO_FLUSH_SYNC mode, PutKudu immediately knows when an operation has failed.
                // In that case, it does not process the rest of the records in the FlowFile.
                if (result == FAIL) break;
            }
        }
    }
    return responses;
}
 
Example #2
Source File: RdbEventRecordHandler.java    From DataLink with Apache License 2.0 5 votes vote down vote up
private void checkError(List<OperationResponse> responses) {
    for (OperationResponse response : responses) {
        boolean error = response.hasRowError();
        if (error) {
            throw new DatalinkException(response.getRowError().toString());
        }
    }
}
 
Example #3
Source File: KuduWriter.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
private void checkErrors(OperationResponse response) throws IOException {
    if (response != null && response.hasRowError()) {
        failureHandler.onFailure(Arrays.asList(response.getRowError()));
    } else {
        checkAsyncErrors();
    }
}
 
Example #4
Source File: Tagsets.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to insert the provided tagset and ID. Returns {@code true} if the
 * write was successful, or {@code false} if the write failed due to a tagset
 * with the same ID already existing in the table.
 *
 * @param tagset the tagset to insert
 * @param id     the ID to insert the tagset with
 * @return whether the write succeeded
 */
private Deferred<Boolean> insertTagset(final SerializedTagset tagset, final int id) throws KuduException {
  final class InsertTagsetCB implements Callback<Deferred<Boolean>, OperationResponse> {
    @Override
    public Deferred<Boolean> call(OperationResponse response) {
      if (response.hasRowError()) {
        if (response.getRowError().getErrorStatus().isAlreadyPresent()) {
          LOG.info("Attempted to insert duplicate tagset; id: {}, tagset: {}", id, tagset);
          // TODO: Consider adding a backoff with jitter before attempting
          //       the insert again (if the lookup fails).
          return Deferred.fromResult(false);
        }
        return Deferred.fromError(new RuntimeException(
            String.format("Unable to insert tagset; id: %s, tagset: %s, error: %s",
                          id, tagset, response.getRowError())));
      } else {
        return Deferred.fromResult(true);
      }
    }
    @Override
    public String toString() {
      return MoreObjects.toStringHelper(this).toString();
    }
  }

  LOG.debug("Inserting tagset; id: {}, tags: {}", id, tagset);
  final AsyncKuduSession session = client.newSession();
  try {
    // We don't have to handle PleaseThrottleException because we are only
    // inserting a single row.
    final Insert insert = tagsetsTable.newInsert();
    insert.getRow().addInt(Tables.TAGSETS_ID_INDEX, id);
    insert.getRow().addBinary(Tables.TAGSETS_TAGSET_INDEX, tagset.getBytes());
    return session.apply(insert).addCallbackDeferring(new InsertTagsetCB());
  } finally {
    session.close();
  }
}
 
Example #5
Source File: WriteBatch.java    From kudu-ts with Apache License 2.0 5 votes vote down vote up
public OperationResponse writeDatapoint(final String metric,
                                        SortedMap<String, String> tags,
                                        final long time,
                                        final double value) throws Exception {
  int tagsetID = tagsets.getTagsetID(tags)
                        .joinUninterruptibly(session.getTimeoutMillis());
  return session.apply(metrics.insertDatapoint(metric, tagsetID, time, value));
}
 
Example #6
Source File: KuduInputOperatorCommons.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
public void addTestDataRows(int numRowsInEachPartition) throws Exception
{
  int intRowKeyStepsize = Integer.MAX_VALUE / SPLIT_COUNT_FOR_INT_ROW_KEY;
  int splitBoundaryForIntRowKey = intRowKeyStepsize;
  int[] inputrowkeyPartitionEntries = new int[SPLIT_COUNT_FOR_INT_ROW_KEY + 1];
  // setting the int keys that will fall in the range of all partitions
  for ( int i = 0; i < SPLIT_COUNT_FOR_INT_ROW_KEY; i++) {
    inputrowkeyPartitionEntries[i] = splitBoundaryForIntRowKey + 3; // 3 to fall into the partition next to boundary
    splitBoundaryForIntRowKey += intRowKeyStepsize;
  }
  inputrowkeyPartitionEntries[SPLIT_COUNT_FOR_INT_ROW_KEY] = splitBoundaryForIntRowKey + 3;
  AbstractKuduPartitionScanner<UnitTestTablePojo,InputOperatorControlTuple> scannerForAddingRows =
      unitTestStepwiseScanInputOperator.getScanner();
  ApexKuduConnection aCurrentConnection = scannerForAddingRows.getConnectionPoolForThreads().get(0);
  KuduSession aSessionForInserts = aCurrentConnection.getKuduClient().newSession();
  KuduTable currentTable = aCurrentConnection.getKuduTable();
  long seedValueForTimestampRowKey = 0L; // constant to allow for data landing on first partition for unit tests
  for ( int i = 0; i <= SPLIT_COUNT_FOR_INT_ROW_KEY; i++) { // range key iterator
    int intRowKeyBaseValue = inputrowkeyPartitionEntries[i] + i;
    for ( int k = 0; k < 2; k++) { // hash key iterator . The table defines two hash partitions
      long timestampRowKeyValue = seedValueForTimestampRowKey + k; // to avoid spilling to another tablet
      String stringRowKeyValue = "" + timestampRowKeyValue + k; // to avoid spilling to another tablet randomly
      for ( int y = 0; y < numRowsInEachPartition; y++) {
        Upsert aNewRow = currentTable.newUpsert();
        PartialRow rowValue  = aNewRow.getRow();
        // Start assigning row keys below the current split boundary.
        rowValue.addInt("introwkey",intRowKeyBaseValue - y - 1);
        rowValue.addString("stringrowkey",stringRowKeyValue);
        rowValue.addLong("timestamprowkey",timestampRowKeyValue);
        rowValue.addLong("longdata",(seedValueForTimestampRowKey + y));
        rowValue.addString("stringdata", ("" + seedValueForTimestampRowKey + y));
        OperationResponse response = aSessionForInserts.apply(aNewRow);
      }
    }
  }
  List<OperationResponse> insertResponse = aSessionForInserts.flush();
  aSessionForInserts.close();
  Thread.sleep(2000); // Sleep to allow for scans to complete
}
 
Example #7
Source File: KuduMetadataWriter.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public void write(final GeoWaveMetadata metadata) {
  try {
    final Insert insert = operations.getTable(tableName).newInsert();
    final PartialRow partialRow = insert.getRow();
    final KuduMetadataRow row = new KuduMetadataRow(metadata);
    row.populatePartialRow(partialRow);
    final OperationResponse resp = session.apply(insert);
    if (resp.hasRowError()) {
      LOGGER.error("Encountered error while writing metadata: {}", resp.getRowError());
    }
  } catch (final KuduException e) {
    LOGGER.error("Kudu error when writing metadata", e);
  }
}
 
Example #8
Source File: AbstractKuduProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected void flushKuduSession(final KuduSession kuduSession, boolean close, final List<RowError> rowErrors) throws KuduException {
    final List<OperationResponse> responses = close ? kuduSession.close() : kuduSession.flush();

    if (kuduSession.getFlushMode() == SessionConfiguration.FlushMode.AUTO_FLUSH_BACKGROUND) {
        rowErrors.addAll(Arrays.asList(kuduSession.getPendingErrors().getRowErrors()));
    } else {
        responses.stream()
                .filter(OperationResponse::hasRowError)
                .map(OperationResponse::getRowError)
                .forEach(rowErrors::add);
    }
}
 
Example #9
Source File: Tags.java    From kudu-ts with Apache License 2.0 4 votes vote down vote up
/**
 * Insert a tagset into the {@code tags} table.
 * @param id the tagset ID.
 * @param tagset the tagset.
 * @return The tagset ID.
 */
public Deferred<Integer> insertTagset(final int id, final SortedMap<String, String> tagset)
    throws KuduException {
  if (tagset.isEmpty()) { return Deferred.fromResult(id); }
  LOG.debug("Inserting tags; tagsetID: {}, tags: {}", id, tagset);
  final AsyncKuduSession session = client.newSession();

  class InsertTagsetCB implements Callback<Deferred<Integer>, List<OperationResponse>> {
    @Override
    public Deferred<Integer> call(List<OperationResponse> responses) {
      try {
        for (OperationResponse response : responses) {
          if (response.hasRowError()) {
            return Deferred.fromError(new RuntimeException(
                String.format("Unable to insert tag: %s", response.getRowError())));
          }
        }
        return Deferred.fromResult(id);
      } finally {
        session.close();
      }
    }
    @Override
    public String toString() {
      return MoreObjects.toStringHelper(this)
                        .add("id", id)
                        .add("tags", tagset)
                        .toString();
    }
  }

  if (tagset.size() > 1000) {
    session.setMutationBufferSpace(tagset.size());
  }
  session.setMutationBufferLowWatermark(1.0f);

  // buffer all of the tags into the session, and ensure that we don't get
  // a PleaseThrottleException. In practice the number of tags should be
  // small.
  session.setMutationBufferSpace(tagset.size());
  session.setMutationBufferLowWatermark(1.0f);
  session.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
  for (Map.Entry<String, String> tag : tagset.entrySet()) {
    Insert insert = table.newInsert();
    // TODO: check with JD that if the inserts below fail, the error will
    // also be returned in the flush call.
    insert.getRow().addString(Tables.TAGS_KEY_INDEX, tag.getKey());
    insert.getRow().addString(Tables.TAGS_VALUE_INDEX, tag.getValue());
    insert.getRow().addInt(Tables.TAGS_TAGSET_ID_INDEX, id);
    session.apply(insert);
  }
  return session.flush().addCallbackDeferring(new InsertTagsetCB());
}
 
Example #10
Source File: WriteBatch.java    From kudu-ts with Apache License 2.0 2 votes vote down vote up
/**
 * Blocking call that force flushes this batch's buffers. Data is persisted
 * when this call returns, else it will throw an exception.
 * @return a list of OperationResponse, one per datapoint that was flushed
 * @throws Exception if anything went wrong. If it's an issue with some or all batches,
 * it will be of type DeferredGroupException.
 */
public List<OperationResponse> flush() throws Exception {
  return session.flush();
}
 
Example #11
Source File: WriteBatch.java    From kudu-ts with Apache License 2.0 2 votes vote down vote up
/**
 * Blocking call that flushes the buffers (see {@link #flush} and closes the batch.
 * @return List of OperationResponse, one per datapoint that was flushed
 * @throws Exception if anything went wrong. If it's an issue with some or all batches,
 * it will be of type DeferredGroupException.
 */
public List<OperationResponse> close() throws Exception {
  return session.close();
}