Search in sources :

Example 1 with TransactionBatch

use of org.apache.hive.hcatalog.streaming.TransactionBatch in project nifi by apache.

the class HiveWriter method nextTxnBatch.

protected TransactionBatch nextTxnBatch(final RecordWriter recordWriter) throws InterruptedException, TxnBatchFailure {
    LOG.debug("Fetching new Txn Batch for {}", endPoint);
    TransactionBatch batch = null;
    try {
        batch = callWithTimeout(() -> {
            // could block
            return connection.fetchTransactionBatch(txnsPerBatch, recordWriter);
        });
        batch.beginNextTransaction();
        LOG.debug("Acquired {}. Switching to first txn", batch);
    } catch (TimeoutException | StreamingException e) {
        throw new TxnBatchFailure(endPoint, e);
    }
    return batch;
}
Also used : StreamingException(org.apache.hive.hcatalog.streaming.StreamingException) TransactionBatch(org.apache.hive.hcatalog.streaming.TransactionBatch) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with TransactionBatch

use of org.apache.hive.hcatalog.streaming.TransactionBatch in project nifi by apache.

the class HiveWriterTest method setup.

@Before
public void setup() throws Exception {
    hiveEndPoint = mock(HiveEndPoint.class);
    txnsPerBatch = 100;
    autoCreatePartitions = true;
    callTimeout = 0;
    executorService = mock(ExecutorService.class);
    streamingConnection = mock(StreamingConnection.class);
    transactionBatch = mock(TransactionBatch.class);
    userGroupInformation = mock(UserGroupInformation.class);
    hiveConf = mock(HiveConf.class);
    recordWriter = mock(RecordWriter.class);
    recordWriterCallable = mock(Callable.class);
    when(recordWriterCallable.call()).thenReturn(recordWriter);
    when(hiveEndPoint.newConnection(autoCreatePartitions, hiveConf, userGroupInformation)).thenReturn(streamingConnection);
    when(streamingConnection.fetchTransactionBatch(txnsPerBatch, recordWriter)).thenReturn(transactionBatch);
    when(executorService.submit(isA(Callable.class))).thenAnswer(invocation -> {
        Future future = mock(Future.class);
        Answer<Object> answer = i -> ((Callable) invocation.getArguments()[0]).call();
        when(future.get()).thenAnswer(answer);
        when(future.get(anyLong(), any(TimeUnit.class))).thenAnswer(answer);
        return future;
    });
    when(userGroupInformation.doAs(isA(PrivilegedExceptionAction.class))).thenAnswer(invocation -> {
        try {
            try {
                return ((PrivilegedExceptionAction) invocation.getArguments()[0]).run();
            } catch (UncheckedExecutionException e) {
                // Creation of strict json writer will fail due to external deps, this gives us chance to catch it
                for (StackTraceElement stackTraceElement : e.getStackTrace()) {
                    if (stackTraceElement.toString().startsWith("org.apache.hive.hcatalog.streaming.StrictJsonWriter.<init>(")) {
                        return recordWriterCallable.call();
                    }
                }
                throw e;
            }
        } catch (IOException | Error | RuntimeException | InterruptedException e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    });
    initWriter();
}
Also used : Callable(java.util.concurrent.Callable) StreamingException(org.apache.hive.hcatalog.streaming.StreamingException) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint) Answer(org.mockito.stubbing.Answer) Future(java.util.concurrent.Future) RecordWriter(org.apache.hive.hcatalog.streaming.RecordWriter) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) Matchers.anyLong(org.mockito.Matchers.anyLong) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) StreamingConnection(org.apache.hive.hcatalog.streaming.StreamingConnection) Matchers.isA(org.mockito.Matchers.isA) Assert.assertNotNull(org.junit.Assert.assertNotNull) TransactionBatch(org.apache.hive.hcatalog.streaming.TransactionBatch) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) TimeUnit(java.util.concurrent.TimeUnit) Matchers.any(org.mockito.Matchers.any) InvalidTable(org.apache.hive.hcatalog.streaming.InvalidTable) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TransactionBatch(org.apache.hive.hcatalog.streaming.TransactionBatch) StreamingConnection(org.apache.hive.hcatalog.streaming.StreamingConnection) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) RecordWriter(org.apache.hive.hcatalog.streaming.RecordWriter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ExecutorService(java.util.concurrent.ExecutorService) HiveEndPoint(org.apache.hive.hcatalog.streaming.HiveEndPoint) Future(java.util.concurrent.Future) TimeUnit(java.util.concurrent.TimeUnit) HiveConf(org.apache.hadoop.hive.conf.HiveConf) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) Before(org.junit.Before)

Example 3 with TransactionBatch

use of org.apache.hive.hcatalog.streaming.TransactionBatch in project hive by apache.

the class TestCompactor method writeBatch.

private void writeBatch(StreamingConnection connection, DelimitedInputWriter writer, boolean closeEarly) throws InterruptedException, StreamingException {
    TransactionBatch txnBatch = connection.fetchTransactionBatch(2, writer);
    txnBatch.beginNextTransaction();
    txnBatch.write("50,Kiev".getBytes());
    txnBatch.write("51,St. Petersburg".getBytes());
    txnBatch.write("44,Boston".getBytes());
    txnBatch.commit();
    if (!closeEarly) {
        txnBatch.beginNextTransaction();
        txnBatch.write("52,Tel Aviv".getBytes());
        txnBatch.write("53,Atlantis".getBytes());
        txnBatch.write("53,Boston".getBytes());
        txnBatch.commit();
        txnBatch.close();
    }
}
Also used : TransactionBatch(org.apache.hive.hcatalog.streaming.TransactionBatch)

Example 4 with TransactionBatch

use of org.apache.hive.hcatalog.streaming.TransactionBatch in project storm by apache.

the class HiveWriter method nextTxnBatch.

private TransactionBatch nextTxnBatch(final RecordWriter recordWriter) throws InterruptedException, TxnBatchFailure {
    LOG.debug("Fetching new Txn Batch for {}", endPoint);
    TransactionBatch batch = null;
    try {
        batch = callWithTimeout(new CallRunner<TransactionBatch>() {

            @Override
            public TransactionBatch call() throws Exception {
                // could block
                return connection.fetchTransactionBatch(txnsPerBatch, recordWriter);
            }
        });
        batch.beginNextTransaction();
        LOG.debug("Acquired {}. Switching to first txn", batch);
    } catch (TimeoutException e) {
        throw new TxnBatchFailure(endPoint, e);
    } catch (StreamingException e) {
        throw new TxnBatchFailure(endPoint, e);
    }
    return batch;
}
Also used : StreamingException(org.apache.hive.hcatalog.streaming.StreamingException) TransactionBatch(org.apache.hive.hcatalog.streaming.TransactionBatch) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

TransactionBatch (org.apache.hive.hcatalog.streaming.TransactionBatch)4 StreamingException (org.apache.hive.hcatalog.streaming.StreamingException)3 TimeoutException (java.util.concurrent.TimeoutException)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1 HiveConf (org.apache.hadoop.hive.conf.HiveConf)1 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)1 HiveEndPoint (org.apache.hive.hcatalog.streaming.HiveEndPoint)1 InvalidTable (org.apache.hive.hcatalog.streaming.InvalidTable)1 RecordWriter (org.apache.hive.hcatalog.streaming.RecordWriter)1 StreamingConnection (org.apache.hive.hcatalog.streaming.StreamingConnection)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Before (org.junit.Before)1