Search in sources :

Example 1 with BatchWritable

use of co.cask.cdap.api.data.batch.BatchWritable in project cdap by caskdata.

the class BasicMapReduceTaskContext method getBatchWritable.

/**
   * Returns a {@link CloseableBatchWritable} that writes data to the given dataset.
   */
<K, V> CloseableBatchWritable<K, V> getBatchWritable(String namespace, String datasetName, Map<String, String> datasetArgs) {
    Dataset dataset = getDataset(namespace, datasetName, datasetArgs, AccessType.WRITE);
    // Must be BatchWritable.
    Preconditions.checkArgument(dataset instanceof BatchWritable, "Dataset '%s:%s' is not a BatchWritable.", namespace, datasetName);
    @SuppressWarnings("unchecked") final BatchWritable<K, V> delegate = (BatchWritable<K, V>) dataset;
    return new CloseableBatchWritable<K, V>() {

        @Override
        public void write(K k, V v) {
            delegate.write(k, v);
        }

        @Override
        public void close() throws IOException {
            try {
                flushOperations();
            } catch (Exception e) {
                Throwables.propagateIfInstanceOf(e, IOException.class);
                throw new IOException(e);
            }
        }
    };
}
Also used : Dataset(co.cask.cdap.api.dataset.Dataset) IOException(java.io.IOException) CloseableBatchWritable(co.cask.cdap.internal.app.runtime.batch.dataset.CloseableBatchWritable) DatasetInstantiationException(co.cask.cdap.api.data.DatasetInstantiationException) FileNotFoundException(java.io.FileNotFoundException) TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) IOException(java.io.IOException) CloseableBatchWritable(co.cask.cdap.internal.app.runtime.batch.dataset.CloseableBatchWritable) BatchWritable(co.cask.cdap.api.data.batch.BatchWritable)

Aggregations

DatasetInstantiationException (co.cask.cdap.api.data.DatasetInstantiationException)1 BatchWritable (co.cask.cdap.api.data.batch.BatchWritable)1 Dataset (co.cask.cdap.api.dataset.Dataset)1 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)1 CloseableBatchWritable (co.cask.cdap.internal.app.runtime.batch.dataset.CloseableBatchWritable)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1