標籤:flume
最近項目組有需求點擊流日誌需要自己收集,學習了一下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</p><p align="left">
4) 配置conf相關檔案</p><p align="left">
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/> * ---------------------------- * 姓名:毛祥溢 * 郵箱:[email protected] * 網站:www.maoxiangyi.cn */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學習安裝