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. [转]Delphi中ShellExecute的妙用

    Delphi中ShellExecute的妙用       ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制.   ...

  2. SGU 191.Exhibition(模拟)

    时间限制:0.25s 空间限制:4M 题意: 有两个公司A.B,他们要展览物品,但是A公司的展柜要放B公司的物品,B公司的展柜要放A公司物品.最开始只有一个空柜台,从指定的一个公司开始,轮流进行操作, ...

  3. underscorejs-size学习

    2.24 size 2.24.1 语法: _.size(list) 2.24.2 说明: 返回列表的长度. 示例一:返回数组.对象.字符串的长度 //取数组的长度 var length length ...

  4. java 全角、半角字符串转换

    转自:http://www.cnblogs.com/modou/articles/2679815.html     加入了空字符串的验证 半角转全角的方法: /** * @Title: ToSBC * ...

  5. int*-------int

    a=(int)((int*)0 + 4)求a是多少 大家看图应该明白了  十六进制0x00000010转换为十进制就是16

  6. 【Windows核心编程】Windows常见数据类型

    一,常见数据类型 WORD:               16位无符号整形数据 DWORD:             32位无符号整型数据(DWORD32) DWORD64:         64位无 ...

  7. Shell 控制并发

    方法1: #!/bin/bash c=0 for i in `seq -w 18 31`;do while [ $c -ge 3 ];do c=$(jobs -p |wc -w) sleep 1s d ...

  8. 通过搭建一个精简的C语言开发环境了解一个C程序的执行过程

    一.如何搭建一个精简的C语言开发环境 准备:下载TC2.0,并解压,比如说“d:\tc2.0\tc”目录 1.在C盘建立一个目录minic c:\ md minic 2.从解压的目录中将以下文件拷贝到 ...

  9. sql 使用 FOR XML PATH实现字符串拼接

    sql中经常需要把多个行数据合成一行下面是利用 FOR XML PATH来实现的简单介绍. 1,把图一的转换为图二: SELECT articleID, (),tagID)+',' FROM arti ...

  10. bzoj2096: [Poi2010]Pilots

    Description Tz又耍畸形了!!他要当飞行员,他拿到了一个飞行员测试难度序列,他设定了一个难度差的最大值,在序列中他想找到一个最长的子串,任意两个难度差不会超过他设定的最大值.耍畸形一个人是 ...