近期项目组有需求点击流日志须要自己收集,学习了一下flume而且成功安装了。相关信息记录一下。

1)下载flume1.5版本号 

wget http://www.apache.org/dyn/closer.cgi/flume/1.5.0.1/apache-flume-1.5.0.1-bin.tar.gz

2) 解压flume1.5

tar -zxvf apache-flume-1.5.0.1-bin.tar.gz

3) 配置环境变量

jdk已装

export FLUME_HOME=/XXX/XX/apache-flume-1.5.0.1-bin

export PATH=$FLUME_HOME/bin:$PATH

4) 配置conf相关文件

4.1) 配置flume-env.sh 主要设置一下JAVA_HOME

4.2) 配置log4j.properties  

假设是測试环境凝视掉flume.root.logger=INFO,LOGFILE选择flume.root.logger=DEBUG,console把日志打印到控制台

4.3) 配置flume-conf.properties 这个文件名称能够随便改 执行命令时指定你自己创建的属性文件就可以

#set agent 名字为a1  sources名字为r1   sinks名字为k1    channels名字为c1

a1.sources = r1

a1.sinks = k1

a1.channels = c1

sources组件类型为exec 运行linux命令

a1.sources.r1.type = exec

a1.sources.r1.command = tail -F /home/hadoop/flume/flume/conf/source.txt (大小tail -f有非常大差别,攻克了我们一个大问题)





sinks组件类型为logger

a1.sinks.k1.type = logger

channels组件类型为内存

a1.channels.c1.type = memory

a1.channels.c1.capacity = 1000

a1.channels.c1.transactionCapacity = 100





把sources、sinks与管道连通起来

a1.sources.r1.channels = c1

a1.sinks.k1.channel = c1





5) 在flume文件夹下执行命令

bin/flume-ng agent -n a1 -f test/source-tail-sink-logger.properties --conf conf





初步的样例完毕了。眼下我们生产环境是有两个节点往metaq里面生产数据。 metaq自己定义一个sink(自己定义sink见后面代码)

记得把metaq相关jar放到flume/lib下 gecko-1.1.4.jar metamorphosis-client-1.4.6.2.jar metamorphosis-commons-1.4.6.2.jar zkclient-0.3.jar zookeeper-3.4.3.jar





a1.sources = r1

a1.sinks = k1

a1.channels = c1





a1.sources.r1.type = exec

a1.sources.r1.command = tail -F /home/hadoop/flume/flume/conf/source.txt

a1.sinks.k1.type = com.XX.flume.sink.MetaQSink

a1.sinks.k1.sink.zkConnect = 0.0.0.0:2181,0.0.0.0:2181,0.0.0.0:2181

a1.sinks.k1.sink.zkRoot = /meta(此文件夹必须写死)

a1.sinks.k1.sink.topic = XXXX

a1.sinks.k1.sink.batchSize = 20000

#a1.channels.c1.type = memory

#a1.channels.c1.capacity = 1000000

#a1.channels.c1.transactionCapacity = 100000

a1.channels.c1.type = file

a1.channels.c1.checkpointDir = /home/hadoop/flume/flume/channel/checkpoint 

a1.channels.c1.dataDirs = /home/hadoop/flume/flume/channel/data





a1.sources.r1.channels = c1

a1.sinks.k1.channel = c1

自己定义sink代码

</pre><pre name="code" class="java">package com.jd.flume.sink;
import com.taobao.metamorphosis.Message;
import com.taobao.metamorphosis.client.MessageSessionFactory;
import com.taobao.metamorphosis.client.MetaClientConfig;
import com.taobao.metamorphosis.client.MetaMessageSessionFactory;
import com.taobao.metamorphosis.client.producer.MessageProducer;
import com.taobao.metamorphosis.client.producer.SendResult;
import com.taobao.metamorphosis.exception.MetaClientException;
import com.taobao.metamorphosis.utils.ZkUtils;
import org.apache.flume.*;
import org.apache.flume.conf.Configurable;
import org.apache.flume.sink.AbstractSink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger; /**
* 功能描写叙述:
* <p/>
* 这个类主要是将flume收集的数据发送到metaq消息队列中
* <p/>
* ----------------------------
*/
public class MetaQSink extends AbstractSink implements Configurable {
private static final Logger logger = LoggerFactory.getLogger(MetaQSink.class);
private MessageSessionFactory sessionFactory;
private MessageProducer producer; private String zkConnect;
private String zkRoot;
private String topic;
private int batchSize;
private int threadNum;
private ExecutorService executor; public MetaQSink() {
} @Override
public void configure(Context context) {
this.zkConnect = context.getString("sink.zkConnect");
this.zkRoot = context.getString("sink.zkRoot");
this.topic = context.getString("sink.topic");
this.batchSize = context.getInteger("sink.batchSize", 10000);
this.threadNum = context.getInteger("sink.threadNum", 50);
executor = Executors.newCachedThreadPool(); MetaClientConfig metaClientConfig = new MetaClientConfig();
ZkUtils.ZKConfig zkConfig = new ZkUtils.ZKConfig();
zkConfig.zkConnect = zkConnect;
zkConfig.zkRoot = zkRoot;
metaClientConfig.setZkConfig(zkConfig);
try {
sessionFactory = new MetaMessageSessionFactory(metaClientConfig);
} catch (MetaClientException e) {
e.printStackTrace();
logger.error("", e);
throw new RuntimeException("init error");
}
producer = sessionFactory.createProducer();
logger.info("zkConnect:" + zkConnect + ", zkRoot:" + zkRoot
+ ", topic:" + topic);
} @Override
public Status process() throws EventDeliveryException {
long start = System.currentTimeMillis();
producer.publish(topic);
Status result = Status.READY;
final Channel channel = getChannel();
final AtomicInteger al = new AtomicInteger(0);
final CountDownLatch cdl = new CountDownLatch(threadNum);
for (int t = 0; t < threadNum; t++) {
executor.execute(new Runnable() {
@Override
public void run() { Event event = null;
Transaction transaction = null;
int i = 0;
try {
transaction = channel.getTransaction();
transaction.begin();
boolean startTransaction = false;
for (i = 0; i < batchSize; i++) {
event = channel.take();
if (event != null) {
if (i == 0) {
producer.beginTransaction();
startTransaction = true;
}
final SendResult sendResult = producer
.sendMessage(new Message(topic, event
.getBody()));
// check result
if (!sendResult.isSuccess()) {
logger.error("Send message failed,error message:"
+ sendResult.getErrorMessage());
throw new RuntimeException(
"Send message failed,error message:"
+ sendResult
.getErrorMessage());
} else {
logger.debug("Send message successfully,sent to "
+ sendResult.getPartition());
}
} else {
// No event found, request back-off semantics
// from the sink
// runner
// result = Status.BACKOFF;
break;
} }
if (startTransaction) {
producer.commit();
}
al.addAndGet(i);
transaction.commit();
} catch (Exception ex) {
logger.error("error while rollback:", ex);
try {
producer.rollback();
} catch (Exception e) {
e.printStackTrace();
}
transaction.rollback();
} finally {
cdl.countDown();
transaction.close();
}
}
});
}
try {
cdl.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (al.get() == 0) {
result = Status.BACKOFF;
} logger.info("metaqSink_new,process:{},time:{},queue_size:{}",
new Object[] { al.get(), System.currentTimeMillis() - start });
return result;
}
}

flume学习安装的更多相关文章

  1. 日志采集框架Flume以及Flume的安装部署(一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统)

    Flume支持众多的source和sink类型,详细手册可参考官方文档,更多source和sink组件 http://flume.apache.org/FlumeUserGuide.html Flum ...

  2. Flume学习总结

    Flume学习总结 flume是一个用来采集数据的软件,它可以从数据源采集数据到一个集中存放的地方. 最常用flume的数据采集场景是对日志的采集,不过,lume也可以用来采集其他的各种各样的数据,因 ...

  3. flume学习(三):flume将log4j日志数据写入到hdfs(转)

    原文链接:flume学习(三):flume将log4j日志数据写入到hdfs 在第一篇文章中我们是将log4j的日志输出到了agent的日志文件当中.配置文件如下: tier1.sources=sou ...

  4. Flume学习应用:Java写日志数据到MongoDB

    概述 Windows平台:Java写日志到Flume,Flume最终把日志写到MongoDB. 系统环境 操作系统:win7 64 JDK:1.6.0_43 资源下载 Maven:3.3.3下载.安装 ...

  5. Flume的安装与配置

    Flume的安装与配置 一.       资源下载 资源地址:http://flume.apache.org/download.html 程序地址:http://apache.fayea.com/fl ...

  6. Flume学习之路 (一)Flume的基础介绍

    一.背景 Hadoop业务的整体开发流程: 从Hadoop的业务开发流程图中可以看出,在大数据的业务处理过程中,对于数据的采集是十分重要的一步,也是不可避免的一步. 许多公司的平台每天会产生大量的日志 ...

  7. 日志收集框架flume的安装及简单使用

    flume介绍 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统. Flume可以采集文件,socket数据包等各种形式源数据,又可以将采集到的数据输出到HDFS.hbase.h ...

  8. Flume(3)-安装部署

    一. 下载 Flume官网地址 http://flume.apache.org/ 文档查看地址 http://flume.apache.org/FlumeUserGuide.html 下载地址 htt ...

  9. linux自学(九)之开始centos学习,安装数据库MariaDB

    上一篇:linux自学(八)之开始centos学习,安装tomcat 数据库我们不安装mysql,我网上看了好多资料发现mysql安装比较麻烦,我们这里安装同一个父亲的产品MariaDB.驱动,端口等 ...

随机推荐

  1. mac+eclipse+svn+maven经验总结(转)

    1.Eclipse for Mac:http://mirrors.ustc.edu.cn/eclipse/technology/epp/downloads/release/indigo/SR2/ecl ...

  2. CFUUIDRef和CFStringRef-生成唯一标识符

    - (NSString *)createCUID:(NSString *)prefix{ NSString *  result; CFUUIDRef   uuid; CFStringRef uuidS ...

  3. ireport常见问题

    $V{PAGE_NUMBER} 表示当前是第几页 ,在text field 的 选项evaluation time选report是共几页,now表是当前页.页码可在ireport里直接设置. &quo ...

  4. java + spring (jython\python\script) Error:SyntaxError: no viable alternative at character '\n'

    使用Jython结合java和Python开发功能时,要是遇到如下情况: 2016-03-10 16:16:49 DEBUG [com.freedom.orion.configs.JyhtonConf ...

  5. 转-[Python 学习]2.5版yield之学习心得

    在 shhgs 发布了关于< Py 2.5 what’s new 之 yield>之后,原来我不是特别关注 yield 的用法,因为对于2.3中加入的yield相对来说功能简单,它是作为一 ...

  6. h.264并行解码算法分析

    并行算法类型可以分为两类 Function-level Decomposition,按照功能模块进行并行 Data-level Decomposition,按照数据划分进行并行 Function-le ...

  7. 使用lombok

    Lombok是一种JavaArchive(JAR)文件,可用来消除Java代码的冗长.通过在开发环境中实现Lombok,开发人 员可以节省构建诸如hashCode()和equals()这样的方法以及以 ...

  8. jdk1.5 jdk1.6 jdk1.7 jdk1.8 下载地址

    是不是有很多朋友在oracle找不到历史版本的下载地址哈.... 下载我亲情奉献,有人的捧个人场..... 嘻嘻 jdk1.5updatex所有版本下载地址: http://www.oracle.co ...

  9. bzoj2466,poj1222

    都是简单的异或高斯消元 由于bzoj2466要求解得最小和,所以我们最后还要穷举自由元取最优解 type node=record        po,next:longint;      end; . ...

  10. WordPress ‘get_allowed_mime_types’函数安全漏洞

    漏洞名称: WordPress ‘get_allowed_mime_types’函数安全漏洞 CNNVD编号: CNNVD-201309-170 发布时间: 2013-09-13 更新时间: 2013 ...