標籤:ati 使用者名稱 pch events header action 注意 變數 bug
本文主要介紹使用Flume傳輸資料到MongoDB的過程,內容涉及環境部署和注意事項。
一、環境搭建
1、flune-ng:http://www.apache.org/dyn/closer.cgi/flume/1.5.2/apache-flume-1.5.2-bin.tar.gz
2、mongodb java driver jar包:https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongo-java-driver/2.13.0/mongo-java-driver-2.13.0.jar
3、flume-ng-mongodb-sink 源碼:https://github.com/leonlee/flume-ng-mongodb-sink
flume-ng-mongodb-sink 需要自己編譯jar包,從github上下載代碼,解壓之後執行mvn package,即可產生。需要先安裝maven用於編譯jar包
二、Flume配置
1、env配置
將mongo-java-driver和flume-ng-mongodb-sink兩個jar包放到flume\lib目錄下,並將路徑加入到flume-env.sh檔案的FLUME_CLASSPATH變數中;
JAVA_OPTS變數: 加上-Dflume.monitoring.type=http -Dflume.monitoring.port=xxxx,可以在[hostname:xxxx]/metrics 上看到監控資訊; -Xms指定JVM初始記憶體,-Xmx指定JVM最大記憶體
FLUME_HOME變數: 設定FLUME根目錄
JAVA_HOME變數: 設定JAVA根目錄
2、 log配置
在調試時,將日誌設定為debug並打到檔案:flume.root.logger=DEBUG,LOGFILE
3、 傳輸配置
採用 Exec Source、file-channel、flume-ng-mongodb-sink
my_agent.sources.my_source_1.channels = my_channel_1my_agent.sources.my_source_1.type = execmy_agent.sources.my_source_1.command = python xxx.pymy_agent.sources.my_source_1.shell = /bin/bash -cmy_agent.sources.my_source_1.restartThrottle = 10000my_agent.sources.my_source_1.restart = truemy_agent.sources.my_source_1.logStdErr = truemy_agent.sources.my_source_1.batchSize = 1000my_agent.sources.my_source_1.interceptors = i1 i2 i3my_agent.sources.my_source_1.interceptors.i1.type = staticmy_agent.sources.my_source_1.interceptors.i1.key = dbmy_agent.sources.my_source_1.interceptors.i1.value = cswuyg_testmy_agent.sources.my_source_1.interceptors.i2.type = staticmy_agent.sources.my_source_1.interceptors.i2.key = collectionmy_agent.sources.my_source_1.interceptors.i2.value = cswuyg_testmy_agent.sources.my_source_1.interceptors.i3.type = staticmy_agent.sources.my_source_1.interceptors.i3.key = opmy_agent.sources.my_source_1.interceptors.i3.value = upsert
欄位說明:採用exec source,指定執行命令列為python xxx.py,在xxx.py代碼中處理日誌,並按照跟flume-ng-mongodb-sink的約定,print出json格式的資料,如果update類操作必須帶著_id欄位,print出來的日誌被當作Event的Body,我再通過interceptors給它加上自訂Event Header;
static interceptors用於為Event Header添加資訊,這裡我為它加上了db=cswuyg_test、collection=cswuyg_test、op=upsert,這三個key是跟flume-ng-mongodb-sink 約定的,用於指定mongodb中的db、collection名以及操作類型為update。
my_agent.channels.my_channel_1.type = filemy_agent.channels.my_channel_1.checkpointDir = /home/work/flume/file-channel/my_channel_1/checkPointmy_agent.channels.my_channel_1.useDualCheckpoints = truemy_agent.channels.my_channel_1.backupCheckpointDir = /home/work/flume/file-channel/my_channel_1/checkPoint2my_agent.channels.my_channel_1.dataDirs = /home/work/flume/file-channel/my_channel_1/datamy_agent.channels.my_channel_1.transactionCapacity = 10000my_agent.channels.my_channel_1.checkpointInterval = 30000my_agent.channels.my_channel_1.maxFileSize = 4292870142my_agent.channels.my_channel_1.minimumRequiredSpace = 524288000my_agent.channels.my_channel_1.capacity = 100000
sink配置:
my_agent.sinks.my_mongo_1.type = org.riderzen.flume.sink.MongoSinkmy_agent.sinks.my_mongo_1.host = xxxhostmy_agent.sinks.my_mongo_1.port = yyyportmy_agent.sinks.my_mongo_1.model = DYNAMIC/SINGLE ---查看源碼僅支援此二種方式,並且必須大小
my_agent.sinks.my_mongo_1.db = XXX --mongo表名,預設名稱為events
my_agent.sinks.my_mongo_1.username = XXX --mongo使用者名稱
my_agent.sinks.my_mongo_1.password = YYY --mongo密碼
my_agent.sinks.my_mongo_1.collecion = logmy_agent.sinks.my_mongo_1.batch = 10my_agent.sinks.my_mongo_1.channel = my_channel_1my_agent.sinks.my_mongo_1.timestampField = _S
參見:http://www.cnblogs.com/cswuyg/p/4498804.html
Flume-ng-mongodb-sink