1. 程式人生 > >windows下安裝執行flume 1.8

windows下安裝執行flume 1.8

一、安裝環境和軟體

1、flume 執行需要java環境,所以執行flume之前必須安裝並配置java環境(如果安裝了jdk,但是沒有配置jdk環境,執行 flume 時會丟擲找不見 java.exe 的錯誤)。

2、flume 1.8 需要 java執行時環境java 1.8或更高。

3、下載 flume 1.8 ,下載地址:http://www.apache.org/dyn/closer.lua/flume/1.8.0/apache-flume-1.8.0-bin.tar.gz;
因為下載flume 是 tar.gz,直接解壓就可以了。
4、配置FLUME_HOME環境變數
二、執行

1、在flume的conf目錄下建立配置檔案:example.conf

#flume-to-spark-push.conf: A single-node Flume configuration
#Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#Describe/configure the source
#把Flume Source類別設定為netcat,繫結到node3的33333埠
#可以通過“telnet node3 33333”命令向Flume Source傳送訊息
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 33333

#Describe the sink
#Flume Sink類別設定為avro,繫結到node2的44444埠
#Flume Source把採集到的訊息彙集到Flume Sink以後,Sink會把訊息推送給node2的44444埠
#Spark Streaming程式一直在監聽node2的44444埠,一旦有訊息到達,就會被Spark Streaming應用程式取走進行處理
a1.sinks.k1.type = avro
a1.sinks.k1.hostname = localhost
a1.sinks.k1.port = 44444
#Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000000
a1.channels.c1.transactionCapacity = 1000000

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
2、在powershell中執行命令:

flume-ng agent -conf-file %FLUME_HOME%\conf\example.conf -name a1 

注意:
與Linux中命令的區別 --要換成- 且-Dflume.root.logger=INFO,console在window下不支援
若想像Linux中列印資訊 則需更換為: -property “flume.root.logger=INFO,console”

如果無法識別%FLUME_HOME%則切換成物理路徑
遇到的問題:
在執行此命令時首先會報無法執行.ps1檔案的錯誤
此時執行 set-executionpolicy -executionpolicy unrestricted 可降低系統安全性即能夠執行.ps1檔案
詳情請參見:https://www.cnblogs.com/bonelee/p/8043421.html
執行成功如下圖:
在這裡插入圖片描述

三、測試
執行cmd:
telnet localhost 33333 後,按Ctrl+]鍵 ,再按回車,然後輸入就有正常顯示了。
資料的接收方可以編寫sparkstreaming程式進行驗證:

    package sparkstreaming
    
    import org.apache.spark.SparkConf
    import org.apache.spark.storage.StorageLevel
    import org.apache.spark.streaming._
    import org.apache.spark.streaming.flume._
    
    object FlumeEventCount {
      def main(args: Array[String]) {
        val host = "localhost"
        val port = 44444
    
        // Create the context and set the batch size
        val conf = new SparkConf().setAppName("FlumeEventCount").setMaster("local[2]")
        val ssc = new StreamingContext(conf, Seconds(10))
        // 減少終端的輸出資訊。設定為ERROR時,由於flume沒有啟動,仍有大量的輸出資訊
        ssc.sparkContext.setLogLevel("ERROR")
    
        // Create a flume stream
        val stream = FlumeUtils.createStream(ssc, host, port, StorageLevel.MEMORY_ONLY_SER_2)
    
        // Print out the count of events received from this server in each batch
        stream.map(x=>new String(x.event.getBody.array())
        ).flatMap(_.split("\\s+")).map((_, 1)).reduceByKey(_+_).print()
        ssc.start()
        ssc.awaitTermination()
      }
    }