Search in sources :

Example 31 with Context

use of org.apache.flume.Context in project apex-malhar by apache.

the class ColumnFilteringInterceptorTest method testInterceptEventWithColumnZero.

@Test
public void testInterceptEventWithColumnZero() {
    HashMap<String, String> contextMap = new HashMap<String, String>();
    contextMap.put(ColumnFilteringInterceptor.Constants.DST_SEPARATOR, Byte.toString((byte) 1));
    contextMap.put(ColumnFilteringInterceptor.Constants.SRC_SEPARATOR, Byte.toString((byte) 2));
    contextMap.put(ColumnFilteringInterceptor.Constants.COLUMNS, "0");
    ColumnFilteringInterceptor.Builder builder = new ColumnFilteringInterceptor.Builder();
    builder.configure(new Context(contextMap));
    Interceptor interceptor = builder.build();
    assertArrayEquals("Empty Bytes", "\001".getBytes(), interceptor.intercept(new InterceptorTestHelper.MyEvent("".getBytes())).getBody());
    assertArrayEquals("One Field", "First\001".getBytes(), interceptor.intercept(new InterceptorTestHelper.MyEvent("First".getBytes())).getBody());
    assertArrayEquals("Two Fields", "\001".getBytes(), interceptor.intercept(new InterceptorTestHelper.MyEvent("\002First".getBytes())).getBody());
}
Also used : Context(org.apache.flume.Context) HashMap(java.util.HashMap) Interceptor(org.apache.flume.interceptor.Interceptor) Test(org.junit.Test)

Example 32 with Context

use of org.apache.flume.Context in project apex-malhar by apache.

the class ColumnFilteringFormattingInterceptorTest method testInterceptEventWithColumnZero.

@Test
public void testInterceptEventWithColumnZero() {
    HashMap<String, String> contextMap = new HashMap<String, String>();
    contextMap.put(ColumnFilteringInterceptor.Constants.SRC_SEPARATOR, Byte.toString((byte) 2));
    contextMap.put(ColumnFilteringFormattingInterceptor.Constants.COLUMNS_FORMATTER, "{0}\001");
    ColumnFilteringFormattingInterceptor.Builder builder = new ColumnFilteringFormattingInterceptor.Builder();
    builder.configure(new Context(contextMap));
    Interceptor interceptor = builder.build();
    assertArrayEquals("Empty Bytes", "\001".getBytes(), interceptor.intercept(new InterceptorTestHelper.MyEvent("".getBytes())).getBody());
    assertArrayEquals("One Field", "First\001".getBytes(), interceptor.intercept(new InterceptorTestHelper.MyEvent("First".getBytes())).getBody());
    assertArrayEquals("Two Fields", "\001".getBytes(), interceptor.intercept(new InterceptorTestHelper.MyEvent("\002First".getBytes())).getBody());
}
Also used : Context(org.apache.flume.Context) HashMap(java.util.HashMap) Interceptor(org.apache.flume.interceptor.Interceptor) Test(org.junit.Test)

Example 33 with Context

use of org.apache.flume.Context in project apex-malhar by apache.

the class ColumnFilteringFormattingInterceptorTest method testInterceptEventWithTerminatingSeparator.

@Test
public void testInterceptEventWithTerminatingSeparator() {
    HashMap<String, String> contextMap = new HashMap<String, String>();
    contextMap.put(ColumnFilteringInterceptor.Constants.SRC_SEPARATOR, Byte.toString((byte) 2));
    contextMap.put(ColumnFilteringFormattingInterceptor.Constants.COLUMNS_FORMATTER, "a{1}bc{2}def{3}");
    ColumnFilteringFormattingInterceptor.Builder builder = new ColumnFilteringFormattingInterceptor.Builder();
    builder.configure(new Context(contextMap));
    Interceptor interceptor = builder.build();
    byte[] body = interceptor.intercept(new InterceptorTestHelper.MyEvent("First\002\002Second\002\002\002".getBytes())).getBody();
    assertArrayEquals("Six Fields, " + new String(body), "abcSeconddef".getBytes(), body);
}
Also used : Context(org.apache.flume.Context) HashMap(java.util.HashMap) Interceptor(org.apache.flume.interceptor.Interceptor) Test(org.junit.Test)

Example 34 with Context

use of org.apache.flume.Context in project apex-malhar by apache.

the class ColumnFilteringFormattingInterceptorTest method testInterceptEventWithLongSeparator.

@Test
public void testInterceptEventWithLongSeparator() {
    HashMap<String, String> contextMap = new HashMap<String, String>();
    contextMap.put(ColumnFilteringInterceptor.Constants.SRC_SEPARATOR, Byte.toString((byte) 2));
    contextMap.put(ColumnFilteringFormattingInterceptor.Constants.COLUMNS_FORMATTER, "a{1}bc{2}def{3}ghi");
    ColumnFilteringFormattingInterceptor.Builder builder = new ColumnFilteringFormattingInterceptor.Builder();
    builder.configure(new Context(contextMap));
    Interceptor interceptor = builder.build();
    byte[] body = interceptor.intercept(new InterceptorTestHelper.MyEvent("First\002\002Second\002\002\002".getBytes())).getBody();
    assertArrayEquals("Six Fields, " + new String(body), "abcSeconddefghi".getBytes(), body);
}
Also used : Context(org.apache.flume.Context) HashMap(java.util.HashMap) Interceptor(org.apache.flume.interceptor.Interceptor) Test(org.junit.Test)

Example 35 with Context

use of org.apache.flume.Context in project apex-malhar by apache.

the class InterceptorTestHelper method testFiles.

public void testFiles() throws IOException, URISyntaxException {
    Properties properties = new Properties();
    properties.load(getClass().getResourceAsStream("/flume/conf/flume-conf.properties"));
    String interceptor = null;
    for (Entry<Object, Object> entry : properties.entrySet()) {
        logger.debug("{} => {}", entry.getKey(), entry.getValue());
        if (builder.getClass().getName().equals(entry.getValue().toString())) {
            String key = entry.getKey().toString();
            if (key.endsWith(".type")) {
                interceptor = key.substring(0, key.length() - "type".length());
                break;
            }
        }
    }
    assertNotNull(builder.getClass().getName(), interceptor);
    @SuppressWarnings({ "null", "ConstantConditions" }) final int interceptorLength = interceptor.length();
    HashMap<String, String> map = new HashMap<String, String>();
    for (Entry<Object, Object> entry : properties.entrySet()) {
        String key = entry.getKey().toString();
        if (key.startsWith(interceptor)) {
            map.put(key.substring(interceptorLength), entry.getValue().toString());
        }
    }
    builder.configure(new Context(map));
    Interceptor interceptorInstance = builder.build();
    URL url = getClass().getResource("/test_data/gentxns/");
    assertNotNull("Generated Transactions", url);
    int records = 0;
    File dir = new File(url.toURI());
    for (File file : dir.listFiles()) {
        records += processFile(file, interceptorInstance);
    }
    Assert.assertEquals("Total Records", 2200, records);
}
Also used : Context(org.apache.flume.Context) HashMap(java.util.HashMap) Properties(java.util.Properties) Interceptor(org.apache.flume.interceptor.Interceptor) File(java.io.File) URL(java.net.URL)

Aggregations

Context (org.apache.flume.Context)39 Test (org.junit.Test)24 MemoryChannel (org.apache.flume.channel.MemoryChannel)19 Channel (org.apache.flume.Channel)16 PhoenixSink (org.apache.phoenix.flume.sink.PhoenixSink)12 Transaction (org.apache.flume.Transaction)11 Event (org.apache.flume.Event)9 HashMap (java.util.HashMap)8 NullPhoenixSink (org.apache.phoenix.flume.sink.NullPhoenixSink)8 Interceptor (org.apache.flume.interceptor.Interceptor)7 Properties (java.util.Properties)6 Connection (java.sql.Connection)5 ResultSet (java.sql.ResultSet)5 Sink (org.apache.flume.Sink)5 Date (java.util.Date)3 ChannelException (org.apache.flume.ChannelException)3 ChannelSelector (org.apache.flume.ChannelSelector)3 ChannelProcessor (org.apache.flume.channel.ChannelProcessor)3 ReplicatingChannelSelector (org.apache.flume.channel.ReplicatingChannelSelector)3 ThreadContext (org.apache.logging.log4j.ThreadContext)3