標籤:key 就會 nbsp 計算 位置 預設 nsa inpu count
目標:Flume即時監控目錄sink到hdfs,再用sparkStreaming監控hdfs的這個目錄,對資料進行計算
1、flume的配置,配置spoolDirSource_hdfsSink.properties,監控本地的一個目錄,上傳到hdfs一個目錄下。
agent1.channels = ch1
agent1.sources = spoolDir-source1
agent1.sinks = hdfs-sink1
# 定義channel
agent1.channels.ch1.type = memory
agent1.channels.ch1.capacity=10000
agent1.channels.ch1.transactionCapacity=1000
# 定義source
agent1.sources.spoolDir-source1.channels = ch1
agent1.sources.spoolDir-source1.type = spooldir
agent1.sources.spoolDir-source1.spoolDir = /home/hadoop/flumeDir
agent1.sources.spoolDir-source1.fileHeader = false
agent1.sources.spoolDir-source1.interceptors=i1 i2
agent1.sources.spoolDir-source1.interceptors.i1.type=timestamp
agent1.sources.spoolDir-source1.interceptors.i2.type=static
agent1.sources.spoolDir-source1.interceptors.i2.key=k
agent1.sources.spoolDir-source1.interceptors.i2.value=v
# 定義sink
agent1.sinks.hdfs-sink1.channel = ch1
agent1.sinks.hdfs-sink1.type = hdfs
agent1.sinks.hdfs-sink1.hdfs.path = hdfs://192.168.1.123:9000/user/hadoop/hdfsSink/%Y-%m-%d
agent1.sinks.hdfs-sink1.fileType = DataStream
agent1.sinks.hdfs-sink1.writeFormat=TEXT
agent1.sinks.hdfs-sink1.filePrefix = flumeHdfs
agent1.sinks.hdfs-sink1.batchSize = 1000
agent1.sinks.hdfs-sink1.rollSize = 10240
agent1.sinks.hdfs-sink1.rollCount = 0
agent1.sinks.hdfs-sink1.rollInterval = 1
agent1.sinks.hdfs-sink1.useLocalTimeStamp = true
2、測試本地目錄中的檔案是否能被監控傳入到hdfs目錄
1>、啟動flume命令:bin/flume-ng agent --conf conf/ --conf-file conf/spoolDirSource_hdfsSink.properties --name agent1 -Dflume.root.logger=INFO,console &
啟動成功!
2>、往/home/hadoop/flumeDir中touch一個檔案,d.txt。
flume會監控到這個目錄裡添加了新檔案,就會把這個檔案收集到hdfs相應目錄下,在hdfs的位置如所示:
運行完成的檔案,flume會把檔案標記為完成,如下所示:
3>、這時候啟動並執行sparkStreaming就會監控到hdfs上的變化,運行必要的邏輯,這裡我們是實現簡單的計數。
結果如下:
4>、sparkStreaming的代碼如下:
package hdfsStreaming
import org.apache.spark.SparkConf
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.StreamingContext._
import org.apache.spark.streaming.Seconds
import org.apache.spark.SparkContext
/**
* 監控HDFS一個目錄下的檔案,有一定的時間間隔,隔一段時間執行一次
* 要等待執行完成
* 離線的批量串流
*/
object HdfsStreaming {
def main(args: Array[String]) {
if(args.length !=1){
println("Usage: <inputPath>");
System.exit(1)
}
//構造設定物件,擷取系統預設的設定物件
val conf=new SparkConf
val sc=new SparkContext(conf)
//構造sparkStreaming內容物件,參數一是配置,參數二是時間間隔30s
val scc=new StreamingContext(sc,Seconds(30))
//指定接收器,參數為hdfs目錄
val datas=scc.textFileStream(args(0))
//商務邏輯
val rs=datas.flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_)
//列印結果集
rs.print
//啟動任務,需要使用內容物件啟動
scc.start
//等待任務完成
scc.awaitTermination
}
}
Flume即時監控目錄sink到hdfs