Search in sources :

Example 1 with ChannelProcessor

use of org.apache.flume.channel.ChannelProcessor in project nifi by apache.

the class ExecuteFlumeSource method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) {
    try {
        source = SOURCE_FACTORY.create(context.getProperty(SOURCE_NAME).getValue(), context.getProperty(SOURCE_TYPE).getValue());
        String flumeConfig = context.getProperty(FLUME_CONFIG).getValue();
        String agentName = context.getProperty(AGENT_NAME).getValue();
        String sourceName = context.getProperty(SOURCE_NAME).getValue();
        Configurables.configure(source, getFlumeSourceContext(flumeConfig, agentName, sourceName));
        if (source instanceof PollableSource) {
            source.setChannelProcessor(new ChannelProcessor(new NifiChannelSelector(pollableSourceChannel)));
            source.start();
        }
    } catch (Throwable th) {
        getLogger().error("Error creating source", th);
        throw Throwables.propagate(th);
    }
}
Also used : ChannelProcessor(org.apache.flume.channel.ChannelProcessor) PollableSource(org.apache.flume.PollableSource) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 2 with ChannelProcessor

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

the class HdfsTestSource method start.

@Override
public void start() {
    super.start();
    emitTimer = new Timer();
    final ChannelProcessor channelProcessor = getChannelProcessor();
    emitTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            int lineCount = 0;
            events.clear();
            try {
                while (lineCount < rate && !finished) {
                    String line = br.readLine();
                    if (line == null) {
                        logger.debug("completed file {}", currentFile);
                        br.close();
                        currentFile++;
                        if (currentFile == dataFiles.size()) {
                            logger.info("finished all files");
                            finished = true;
                            break;
                        }
                        Path filePath = new Path(dataFiles.get(currentFile));
                        br = new BufferedReader(new InputStreamReader(new GzipCompressorInputStream(fs.open(filePath))));
                        logger.info("opening file {}. {}", currentFile, filePath);
                        continue;
                    }
                    lineCount++;
                    Event flumeEvent = EventBuilder.withBody(line.getBytes());
                    events.add(flumeEvent);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (events.size() > 0) {
                channelProcessor.processEventBatch(events);
            }
            if (finished) {
                emitTimer.cancel();
            }
        }
    }, 0, 1000);
}
Also used : Path(org.apache.hadoop.fs.Path) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) Timer(java.util.Timer) TimerTask(java.util.TimerTask) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Event(org.apache.flume.Event) IOException(java.io.IOException) ChannelProcessor(org.apache.flume.channel.ChannelProcessor)

Example 3 with ChannelProcessor

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

the class TestSource method start.

@Override
public void start() {
    super.start();
    emitTimer = new Timer();
    final ChannelProcessor channel = getChannelProcessor();
    final int cacheSize = cache.size();
    emitTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            int lastIndex = startIndex + rate;
            if (lastIndex > cacheSize) {
                lastIndex -= cacheSize;
                processBatch(channel, cache.subList(startIndex, cacheSize));
                startIndex = 0;
                while (lastIndex > cacheSize) {
                    processBatch(channel, cache);
                    lastIndex -= cacheSize;
                }
                processBatch(channel, cache.subList(0, lastIndex));
            } else {
                processBatch(channel, cache.subList(startIndex, lastIndex));
            }
            startIndex = lastIndex;
        }
    }, 0, 1000);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) ChannelProcessor(org.apache.flume.channel.ChannelProcessor)

Example 4 with ChannelProcessor

use of org.apache.flume.channel.ChannelProcessor 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)

Example 5 with ChannelProcessor

use of org.apache.flume.channel.ChannelProcessor in project logging-log4j2 by apache.

the class FlumeAppenderTest method setUp.

@Before
public void setUp() throws Exception {
    eventSource = new AvroSource();
    channel = new MemoryChannel();
    Configurables.configure(channel, new Context());
    avroLogger = (Logger) LogManager.getLogger("avrologger");
    /*
         * Clear out all other appenders associated with this logger to ensure
         * we're only hitting the Avro appender.
         */
    removeAppenders(avroLogger);
    final Context context = new Context();
    testPort = String.valueOf(AvailablePortFinder.getNextAvailable());
    context.put("port", testPort);
    context.put("bind", "0.0.0.0");
    Configurables.configure(eventSource, context);
    final List<Channel> channels = new ArrayList<>();
    channels.add(channel);
    final ChannelSelector cs = new ReplicatingChannelSelector();
    cs.setChannels(channels);
    eventSource.setChannelProcessor(new ChannelProcessor(cs));
    eventSource.start();
    Assert.assertTrue("Reached start or error", LifecycleController.waitForOneOf(eventSource, LifecycleState.START_OR_ERROR));
    Assert.assertEquals("Server is started", LifecycleState.START, eventSource.getLifecycleState());
}
Also used : ThreadContext(org.apache.logging.log4j.ThreadContext) Context(org.apache.flume.Context) MemoryChannel(org.apache.flume.channel.MemoryChannel) AvroSource(org.apache.flume.source.AvroSource) ReplicatingChannelSelector(org.apache.flume.channel.ReplicatingChannelSelector) MemoryChannel(org.apache.flume.channel.MemoryChannel) Channel(org.apache.flume.Channel) ArrayList(java.util.ArrayList) ReplicatingChannelSelector(org.apache.flume.channel.ReplicatingChannelSelector) ChannelSelector(org.apache.flume.ChannelSelector) ChannelProcessor(org.apache.flume.channel.ChannelProcessor) Before(org.junit.Before)

Aggregations

ChannelProcessor (org.apache.flume.channel.ChannelProcessor)7 Channel (org.apache.flume.Channel)4 ChannelSelector (org.apache.flume.ChannelSelector)4 Context (org.apache.flume.Context)4 MemoryChannel (org.apache.flume.channel.MemoryChannel)4 ReplicatingChannelSelector (org.apache.flume.channel.ReplicatingChannelSelector)4 ArrayList (java.util.ArrayList)3 Event (org.apache.flume.Event)3 PollableSource (org.apache.flume.PollableSource)3 Test (org.junit.Test)3 Date (java.util.Date)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 Transaction (org.apache.flume.Transaction)2 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)2 SendResult (org.apache.rocketmq.client.producer.SendResult)2 Message (org.apache.rocketmq.common.message.Message)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1