Flume
優點:
- 可以和任意存儲進程集成。
- 輸入的的數據速率大於寫入目的存儲的速率,flume會進行緩衝,減小hdfs的壓力。
- flume中的事務基於channel,使用了兩個事務模型(sender + receiver),確保消息被可靠發送。
Flume使用兩個獨立的事務分別負責從soucrce到channel,以及從channel到sink的事件傳遞。一旦事務中所有的數據全部成功提交到channel,那麼source才認爲該數據讀取完成。同理,只有成功被sink寫出去的數據,纔會從channel中移除。
參數說明:
–conf conf/ :表示配置文件存儲在conf/目錄
–name a1 :表示給agent起名爲a1
–conf-file job/flume-netcat.conf :flume本次啓動讀取的配置文件是在job文件夾下的flume-telnet.conf文件。
-Dflume.root.logger==INFO,console :-D表示flume運行時動態修改flume.root.logger參數屬性值,並將控制檯日誌打印級別設置爲INFO級別。日誌級別包括:log、info、warn、error。
a3.sources = s #定義source
a3.sinks = k #定義sink
a3.channels = c3 #定義channel
Describe/configure the source
a3.sources.r3.type = spooldir #定義source類型爲目錄
a3.sources.r3.spoolDir = /opt/module/flume/upload #定義監控目錄
a3.sources.r3.fileSuffix = .COMPLETED #定義文件上傳完後綴
a3.sources.r3.fileHeader = true #是否有文件頭
a3.sources.r3.ignorePattern = ([^ ]*.tmp) #忽略所有以.tmp結尾的文件
Describe the sink
a3.sinks.k3.type = hdfs #sink類型爲hdfs
a3.sinks.k3.hdfs.path = hdfs://hadoop102:9000/flume/upload/%Y%m%d/%H #文件上傳的路徑
a3.sinks.k3.hdfs.filePrefix = upload- #上傳文件的到hdfs的前綴
a3.sinks.k3.hdfs.round = true #是否按時間滾動文件
a3.sinks.k3.hdfs.roundValue = 1 #多長時間
a3.sinks.k3.hdfs.roundUnit = hour #時間單位
a3.sinks.k3.hdfs.useLocalTimeStamp = true #是否使用本地時間戳
a3.sinks.k3.hdfs.batchSize = 100 #積攢多少個event才刷新到hdfs一次
a3.sinks.k3.hdfs.fileType = DataStream #文件類型,可支持壓縮
#多久生成一個新的文件
a2.sinks.k2.hdfs.rollInterval = 60
#設置每個文件的滾動大小
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滾動與Event數量無關
a2.sinks.k2.hdfs.rollCount = 0
Source是負責接收數據到Flume Agent的組件。Source組件可以處理各種類型、各種格式的日誌數據,包括avro、thrift、exec、jms、spooling directory、netcat、sequence generator、syslog、http、legacy。官方提供的source類型已經很多,但是有時候並不能滿足實際開發當中的需求,此時我們就需要根據實際需求自定義某些source。
如:實時監控MySQL,從MySQL中獲取數據傳輸到HDFS或者其他存儲框架,所以此時需要我們自己實現MySQLSource。
官方也提供了自定義source的接口:
官網說明:https://flume.apache.org/FlumeDeveloperGuide.html#source
根據官方說明自定義mysqlsource需要繼承AbstractSource類並實現Configurable和PollableSource接口。
實現相應方法:
getBackOffSleepIncrement()//暫不用
getMaxBackOffSleepInterval()//暫不用
configure(Context context)//初始化context
process()//獲取數據(從mysql獲取數據,業務處理比較複雜,所以我們定義一個專門的類——SQLSourceHelper來處理跟mysql的交互),封裝成event並寫入channel,這個方法被循環調用
stop()//關閉相關的資源
package com.qf;
import org.apache.flume.Context;
import org.apache.flume.EventDeliveryException;
import org.apache.flume.PollableSource;
import org.apache.flume.conf.Configurable;
import org.apache.flume.event.SimpleEvent;
import org.apache.flume.source.AbstractSource;
import java.util.HashMap;
public class MySource extends AbstractSource implements Configurable, PollableSource {
//定義配置文件將來要讀取的字段
private Long delay;
private String field;
//初始化配置信息
@Override
public void configure(Context context) {
delay = context.getLong("delay");
field = context.getString("field", "Hello!");
}
@Override
public Status process() throws EventDeliveryException {
try {
//創建事件頭信息
HashMap<String, String> hearderMap = new HashMap<>();
//創建事件
SimpleEvent event = new SimpleEvent();
//循環封裝事件
for (int i = 0; i < 5; i++) {
//給事件設置頭信息
event.setHeaders(hearderMap);
//給事件設置內容
event.setBody((field + i).getBytes());
//將事件寫入channel
getChannelProcessor().processEvent(event);
Thread.sleep(delay);
}
} catch (Exception e) {
e.printStackTrace();
return Status.BACKOFF;
}
return Status.READY;
}
@Override
public long getBackOffSleepIncrement() {
return 0;
}
@Override
public long getMaxBackOffSleepInterval() {
return 0;
}
}
Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
Describe/configure the source
a1.sources.r1.type = com.qf.MySource
a1.sources.r1.delay = 1000
#a1.sources.r1.field = qf
Describe the sink
a1.sinks.k1.type = logger
Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
flume-ng agent -c conf/ -f job/mysource.conf -n a1 -Dflume.root.logger=INFO,console