github 参考地址:https://github.com/beyondj2ee/flumeng-kafka-plugin/tree/master/flumeng-kafka-plugin

/*
* Copyright (c) 2013.09.06 BeyondJ2EE.
* * All right reserved.
* * http://beyondj2ee.github.com
* * This software is the confidential and proprietary information of BeyondJ2EE
* * , Inc. You shall not disclose such Confidential Information and
* * shall use it only in accordance with the terms of the license agreement
* * you entered into with BeyondJ2EE.
* *
* * Revision History
* * Author Date Description
* * =============== ================ ======================================
* * beyondj2ee
*
*/

package org.apache.flume.plugins;

/**
* KAFKA Flume Sink (Kafka 0.8 Beta, Flume 1.4).
* User: beyondj2ee
* Date: 13. 9. 4
* Time: PM 4:32
*/

import java.util.Properties;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

import org.apache.commons.lang.StringUtils;
import org.apache.flume.*;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.EventHelper;
import org.apache.flume.sink.AbstractSink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;

/**
* kafka sink.
*/
public class KafkaSink extends AbstractSink implements Configurable {
// - [ constant fields ] ----------------------------------------

/**
* The constant logger.
*/
private static final Logger LOGGER = LoggerFactory
.getLogger(KafkaSink.class);

// - [ variable fields ] ----------------------------------------
/**
* The Parameters.
*/
private Properties parameters;
/**
* The Producer.
*/
private Producer<String, String> producer;
/**
* The Context.
*/
private Context context;

private int i = 100;

// - [ interface methods ] ------------------------------------

/**
* Configure void.
*
* @param context
* the context
*/
@Override
public void configure(Context context) {

this.context = context;
ImmutableMap<String, String> props = context.getParameters();

parameters = new Properties();
for (String key : props.keySet()) {
String value = props.get(key);
this.parameters.put(key, value);

LOGGER.info("key is " + key + " value is " + value);
}
}

/**
* Start void.
*/
@Override
public synchronized void start() {
super.start();
ProducerConfig config = new ProducerConfig(this.parameters);
this.producer = new Producer<String, String>(config);
}

/**
* Process status.
*
* @return the status
* @throws EventDeliveryException
* the event delivery exception
*/
@Override
public Status process() throws EventDeliveryException {
Status status = null;

// Start transaction
Channel ch = getChannel();
Transaction txn = ch.getTransaction();
txn.begin();
try {
// This try clause includes whatever Channel operations you want to
// do
Event event = ch.take();

String partitionKey = (String) parameters
.get(KafkaFlumeConstans.PARTITION_KEY_NAME);
String encoding = StringUtils.defaultIfEmpty(
(String) this.parameters
.get(KafkaFlumeConstans.ENCODING_KEY_NAME),
KafkaFlumeConstans.DEFAULT_ENCODING);
String topic = Preconditions.checkNotNull((String) this.parameters
.get(KafkaFlumeConstans.CUSTOME_TOPIC_KEY_NAME),
"custom.topic.name is required");

String eventData = new String(event.getBody(), encoding);

KeyedMessage<String, String> data;

// if partition key does'nt exist
if (StringUtils.isEmpty(partitionKey)) {
data = new KeyedMessage<String, String>(topic, eventData);
} else {
data = new KeyedMessage<String, String>(topic, partitionKey,
eventData);
}

// if (LOGGER.isInfoEnabled()) {
// LOGGER.info("Send Message to Kafka *************************");
// }
if (i == 0) {
LOGGER.info("100 message send ");
i = 100;
}
i = i - 1;
producer.send(data);
txn.commit();
status = Status.READY;
} catch (Throwable t) {
txn.rollback();
status = Status.BACKOFF;
// re-throw all Errors
if (t instanceof Error) {
LOGGER.info("send data error ",t);
throw (Error) t;
}
} finally {
txn.close();
}
return status;
}

/**
* Stop void.
*/
@Override
public void stop() {
producer.close();
}
// - [ protected methods ] --------------------------------------
// - [ public methods ] -----------------------------------------
// - [ private methods ] ----------------------------------------
// - [ static methods ] -----------------------------------------
// - [ getter/setter methods ] ----------------------------------
// - [ main methods ] -------------------------------------------
}

flumeng-kafka-plugin的更多相关文章

  1. ​Installing the Ranger Kafka Plug-in

    This section describes how to install and enable the Ranger Kafka plug-in. The Ranger Kafka plug-in ...

  2. IBM developer:Setting up the Kafka plugin for Ranger

    Follow these steps to enable and configure the Kafka plugin for Ranger. Before you begin The default ...

  3. Flume-ng+Kafka+storm的学习笔记

    Flume-ng Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统. Flume的文档可以看http://flume.apache.org/FlumeUserGuide.html ...

  4. flume-ng+Kafka+Storm+HDFS 实时系统搭建

    转自:http://www.tuicool.com/articles/mMrQnu7 一 直以来都想接触Storm实时计算这块的东西,最近在群里看到上海一哥们罗宝写的Flume+Kafka+Storm ...

  5. 大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合

    http://www.aboutyun.com/thread-6855-1-1.html 个人观点:大数据我们都知道hadoop,但并不都是hadoop.我们该如何构建大数据库项目.对于离线处理,ha ...

  6. [转]flume-ng+Kafka+Storm+HDFS 实时系统搭建

    http://blog.csdn.net/weijonathan/article/details/18301321 一直以来都想接触Storm实时计算这块的东西,最近在群里看到上海一哥们罗宝写的Flu ...

  7. 转:大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合

    虽然比较久,但是这套架构已经很成熟了,记录一下 一般数据流向,从“数据采集--数据接入--流失计算--数据输出/存储”<ignore_js_op> 1).数据采集 负责从各节点上实时采集数 ...

  8. flume-ng+Kafka+Storm+HDFS 实时系统组合

    http://www.aboutyun.com/thread-6855-1-1.html

  9. flume和kafka整合(转)

    原文链接:Kafka flume 整合 前提 前提是要先把flume和kafka独立的部分先搭建好. 下载插件包 下载flume-kafka-plus:https://github.com/beyon ...

  10. Kafka Ecosystem(Kafka生态)

    http://kafka.apache.org/documentation/#ecosystem https://cwiki.apache.org/confluence/display/KAFKA/E ...

随机推荐

  1. iOS 事件处理机制与图像渲染过程(转)

    iOS 事件处理机制与图像渲染过程 iOS RunLoop都干了什么 iOS 为什么必须在主线程中操作UI 事件响应 CALayer CADisplayLink 和 NSTimer iOS 渲染过程 ...

  2. ios进行打包

    原文转载:http://blog.csdn.net/azhou_hui/article/details/9058677   公司刚搞了个299美刀的仅提供真机测试的企业账号,这个不需要添加设备ID,而 ...

  3. IOS开发效率之为Xcode添加常用的代码片段

    IOS开发效率之为Xcode添加常用的代码片段 原文地址:http://blog.csdn.net/pingchangtan367/article/details/30041285 tableview ...

  4. 静态方法块 static 以及对象属性&类属性的用法

    使用静态块的好处:只要在类被加载时,static块就会被调用,整个过程就调用这么一次,不会在后面的对象处又不断的调用.如果不使用它,就会出现如下问题:new一个对象,我就要调用一次所需的这些内容,重复 ...

  5. OPENGL 地形

    用OPNEGL弄了好久,终于有个地形的样子了! 看起来还是很糟糕....

  6. mysql基础操作整理(一)

    显示当前数据库 mysql> select database(); +------------+ | database() | +------------+ | test | +-------- ...

  7. nuc970连接jlink进行单步调试的设置

    在 USB mode 下, 先跟 NuWriter 接上, 然后用以下的设定. 按 Keil 的 debug (不是 download to flash)就可以接上了.

  8. 五分钟看懂js关键字this

    this是js里面很常用的关键字,而灵活的js也赋予了这个关键字无穷的生命力,相信你也有被它糊弄的时候,我总结了一个6字原则,大部分场合都能清醒分辨this到底指向who,跟大家分享一下,欢迎指正. ...

  9. PHP框架_Smarty_实现登录功能

    1.项目框架 |--mvc |--data 数据 |--cache 缓存 |--template_c 模板生成目录 |--framework |--function |--function.php 功 ...

  10. 关于点击空白关闭弹窗的js写法推荐?

    $(document).mouseup(function(e){ var _con = $(' 目标区域 '); // 设置目标区域 ){ // Mark 1 some code... // 功能代码 ...