Search in sources :

Example 36 with Context

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

the class InterceptorTestHelper method testIntercept_Event.

public void testIntercept_Event() {
    builder.configure(new Context(context));
    Interceptor interceptor = builder.build();
    assertArrayEquals("Empty Bytes", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("".getBytes())).getBody());
    assertArrayEquals("One Separator", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("\002".getBytes())).getBody());
    assertArrayEquals("Two Separators", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("\002\002".getBytes())).getBody());
    assertArrayEquals("One Field", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("First".getBytes())).getBody());
    assertArrayEquals("Two Fields", "First\001\001\001".getBytes(), interceptor.intercept(new MyEvent("\002First".getBytes())).getBody());
    assertArrayEquals("Two Fields", "\001\001\001".getBytes(), interceptor.intercept(new MyEvent("First\001".getBytes())).getBody());
    assertArrayEquals("Two Fields", "Second\001\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002Second".getBytes())).getBody());
    assertArrayEquals("Three Fields", "Second\001\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002Second\002".getBytes())).getBody());
    assertArrayEquals("Three Fields", "\001Second\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002\002Second".getBytes())).getBody());
    assertArrayEquals("Four Fields", "\001Second\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002\002Second\002".getBytes())).getBody());
    assertArrayEquals("Five Fields", "\001Second\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002\002Second\002\002".getBytes())).getBody());
    assertArrayEquals("Six Fields", "\001Second\001\001".getBytes(), interceptor.intercept(new MyEvent("First\002\002Second\002\002\002".getBytes())).getBody());
}
Also used : Context(org.apache.flume.Context) Interceptor(org.apache.flume.interceptor.Interceptor)

Example 37 with Context

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

the class HDFSStorageTest method getStorage.

private HDFSStorage getStorage(String id, boolean restore) {
    Context ctx = new Context();
    STORAGE_DIRECTORY = testMeta.baseDir;
    ctx.put(HDFSStorage.BASE_DIR_KEY, testMeta.baseDir);
    ctx.put(HDFSStorage.RESTORE_KEY, Boolean.toString(restore));
    ctx.put(HDFSStorage.ID, id);
    ctx.put(HDFSStorage.BLOCKSIZE, "256");
    HDFSStorage lstorage = new HDFSStorage();
    lstorage.configure(ctx);
    lstorage.setup(null);
    return lstorage;
}
Also used : Context(org.apache.flume.Context)

Example 38 with Context

use of org.apache.flume.Context in project rocketmq-externals by apache.

the class RocketMQSinkTest method testEvent.

@Test
public void testEvent() throws MQClientException, InterruptedException, EventDeliveryException, RemotingException, MQBrokerException, UnsupportedEncodingException {
    /*
        start sink
         */
    Context context = new Context();
    context.put(NAME_SERVER_CONFIG, nameServer);
    context.put(TAG_CONFIG, tag);
    RocketMQSink sink = new RocketMQSink();
    Configurables.configure(sink, context);
    MemoryChannel channel = new MemoryChannel();
    Configurables.configure(channel, context);
    sink.setChannel(channel);
    sink.start();
    /*
        mock flume source
         */
    String sendMsg = "\"Hello RocketMQ\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-DD hh:mm:ss");
    Transaction tx = channel.getTransaction();
    tx.begin();
    Event event = EventBuilder.withBody(sendMsg.getBytes(), null);
    channel.put(event);
    tx.commit();
    tx.close();
    log.info("publish message : {}", sendMsg);
    Sink.Status status = sink.process();
    if (status == Sink.Status.BACKOFF) {
        fail("Error");
    }
    sink.stop();
    /*
        consumer message
         */
    consumer = new DefaultMQPullConsumer(consumerGroup);
    consumer.setNamesrvAddr(nameServer);
    consumer.setMessageModel(MessageModel.valueOf("BROADCASTING"));
    consumer.registerMessageQueueListener(TOPIC_DEFAULT, null);
    consumer.start();
    String receiveMsg = null;
    Set<MessageQueue> queues = consumer.fetchSubscribeMessageQueues(TOPIC_DEFAULT);
    for (MessageQueue queue : queues) {
        long offset = getMessageQueueOffset(queue);
        PullResult pullResult = consumer.pull(queue, tag, offset, 32);
        if (pullResult.getPullStatus() == PullStatus.FOUND) {
            for (MessageExt message : pullResult.getMsgFoundList()) {
                byte[] body = message.getBody();
                receiveMsg = new String(body, "UTF-8");
                log.info("receive message : {}", receiveMsg);
            }
            long nextBeginOffset = pullResult.getNextBeginOffset();
            putMessageQueueOffset(queue, nextBeginOffset);
        }
    }
    /*
        wait for processQueueTable init
         */
    Thread.sleep(1000);
    consumer.shutdown();
    assertEquals(sendMsg, receiveMsg);
}
Also used : Context(org.apache.flume.Context) MemoryChannel(org.apache.flume.channel.MemoryChannel) Date(java.util.Date) DefaultMQPullConsumer(org.apache.rocketmq.client.consumer.DefaultMQPullConsumer) PullResult(org.apache.rocketmq.client.consumer.PullResult) MessageExt(org.apache.rocketmq.common.message.MessageExt) Transaction(org.apache.flume.Transaction) Sink(org.apache.flume.Sink) MessageQueue(org.apache.rocketmq.common.message.MessageQueue) Event(org.apache.flume.Event) Test(org.junit.Test)

Example 39 with Context

use of org.apache.flume.Context in project rocketmq-externals by apache.

the class RocketMQSourceTest method testEvent.

@Test
public void testEvent() throws EventDeliveryException, MQBrokerException, MQClientException, InterruptedException, UnsupportedEncodingException {
    // publish test message
    DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
    producer.setNamesrvAddr(nameServer);
    String sendMsg = "\"Hello Flume\"" + "," + DateFormatUtils.format(new Date(), "yyyy-MM-DD hh:mm:ss");
    try {
        producer.start();
        Message msg = new Message(TOPIC_DEFAULT, tag, sendMsg.getBytes("UTF-8"));
        SendResult sendResult = producer.send(msg);
        log.info("publish message : {}, sendResult:{}", sendMsg, sendResult);
    } catch (Exception e) {
        throw new MQClientException("Failed to publish messages", e);
    } finally {
        producer.shutdown();
    }
    // start source
    Context context = new Context();
    context.put(NAME_SERVER_CONFIG, nameServer);
    context.put(TAG_CONFIG, tag);
    Channel channel = new MemoryChannel();
    Configurables.configure(channel, context);
    List<Channel> channels = new ArrayList<>();
    channels.add(channel);
    ChannelSelector channelSelector = new ReplicatingChannelSelector();
    channelSelector.setChannels(channels);
    ChannelProcessor channelProcessor = new ChannelProcessor(channelSelector);
    RocketMQSource source = new RocketMQSource();
    source.setChannelProcessor(channelProcessor);
    Configurables.configure(source, context);
    source.start();
    PollableSource.Status status = source.process();
    if (status == PollableSource.Status.BACKOFF) {
        fail("Error");
    }
    /*
        wait for processQueueTable init
         */
    Thread.sleep(1000);
    source.stop();
    /*
        mock flume sink
         */
    Transaction transaction = channel.getTransaction();
    transaction.begin();
    Event event = channel.take();
    if (event == null) {
        transaction.commit();
        fail("Error");
    }
    byte[] body = event.getBody();
    String receiveMsg = new String(body, "UTF-8");
    log.info("receive message : {}", receiveMsg);
    assertEquals(sendMsg, receiveMsg);
}
Also used : Context(org.apache.flume.Context) MemoryChannel(org.apache.flume.channel.MemoryChannel) Message(org.apache.rocketmq.common.message.Message) MemoryChannel(org.apache.flume.channel.MemoryChannel) Channel(org.apache.flume.Channel) ArrayList(java.util.ArrayList) ChannelProcessor(org.apache.flume.channel.ChannelProcessor) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Date(java.util.Date) MQClientException(org.apache.rocketmq.client.exception.MQClientException) EventDeliveryException(org.apache.flume.EventDeliveryException) MQBrokerException(org.apache.rocketmq.client.exception.MQBrokerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PollableSource(org.apache.flume.PollableSource) ReplicatingChannelSelector(org.apache.flume.channel.ReplicatingChannelSelector) Transaction(org.apache.flume.Transaction) SendResult(org.apache.rocketmq.client.producer.SendResult) Event(org.apache.flume.Event) ReplicatingChannelSelector(org.apache.flume.channel.ReplicatingChannelSelector) ChannelSelector(org.apache.flume.ChannelSelector) MQClientException(org.apache.rocketmq.client.exception.MQClientException) Test(org.junit.Test)

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