Search in sources :

Example 1 with RecordWriter

use of org.apache.hive.hcatalog.streaming.RecordWriter 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)

Aggregations

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 StreamingException (org.apache.hive.hcatalog.streaming.StreamingException)1 TransactionBatch (org.apache.hive.hcatalog.streaming.TransactionBatch)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Before (org.junit.Before)1 Test (org.junit.Test)1