zoukankan      html  css  js  c++  java
  • spark与flume整合

    spark-streaming与flume整合  push

    package cn.my.sparkStream
    
    import org.apache.spark.SparkConf
    import org.apache.spark.storage.StorageLevel
    import org.apache.spark.streaming._
    import org.apache.spark.streaming.flume._
    
    
    /**
     
      */
    object SparkFlumePush {
      def main(args: Array[String]) {
        if (args.length < 2) {
          System.err.println(
            "Usage: FlumeEventCount <host> <port>")
          System.exit(1)
        }
        LogLevel.setStreamingLogLevels()
        val Array(host, port) = args
        val batchInterval = Milliseconds(2000)
        // Create the context and set the batch size
        val sparkConf = new SparkConf().setAppName("FlumeEventCount").setMaster("local[2]")
        val ssc = new StreamingContext(sparkConf, batchInterval)
        // Create a flume stream
        val stream = FlumeUtils.createStream(ssc, host, port.toInt, StorageLevel.MEMORY_ONLY_SER_2)
        // Print out the count of events received from this server in each batch
        stream.count().map(cnt => "Received " + cnt + " flume events.").print()
        //拿到消息中的event,从event中拿出body,body是真正的消息体
        stream.flatMap(t=>{new String(t.event.getBody.array()).split(" ")}).map((_,1)).reduceByKey(_+_).print
    
        ssc.start()
        ssc.awaitTermination()
      }
    }
    
    
    package cn.my.sparkStream

    import java.net.InetSocketAddress

    import org.apache.spark.SparkConf
    import org.apache.spark.storage.StorageLevel
    import org.apache.spark.streaming._
    import org.apache.spark.streaming.flume._


    /**
    *
    */
    object SparkFlumePull {
    def main(args: Array[String]) {
    if (args.length < 2) {
    System.err.println(
    "Usage: FlumeEventCount <host> <port>")
    System.exit(1)
    }
    LogLevel.setStreamingLogLevels()
    val Array(host, port) = args
    val batchInterval = Milliseconds(2000)
    // Create the context and set the batch size
    val sparkConf = new SparkConf().setAppName("FlumeEventCount").setMaster("local[2]")
    val ssc = new StreamingContext(sparkConf, batchInterval)
    // Create a flume stream

    //val stream = FlumeUtils.createStream(ssc, host, port.toInt, StorageLevel.MEMORY_ONLY_SER_2)
    // val flumeStream = FlumeUtils.createPollingStream(ssc, host, port.toInt)
    /*
    def createPollingStream(
    jssc: JavaStreamingContext,
    addresses: Array[InetSocketAddress],
    storageLevel: StorageLevel
    ):
    */
    //当sink有多个的时候
    val flumesinklist = Array[InetSocketAddress](new InetSocketAddress("mini1", 8888))
    val flumeStream = FlumeUtils.createPollingStream(ssc, flumesinklist, StorageLevel.MEMORY_ONLY_2)

    flumeStream.count().map(cnt => "Received " + cnt + " flume events.").print()
    flumeStream.flatMap(t => {
    new String(t.event.getBody.array()).split(" ")
    }).map((_, 1)).reduceByKey(_ + _).print()


    // Print out the count of events received from this server in each batch
    //stream.count().map(cnt => "Received " + cnt + " flume events.").print()
    //拿到消息中的event,从event中拿出body,body是真正的消息体
    //stream.flatMap(t=>{new String(t.event.getBody.array()).split(" ")}).map((_,1)).reduceByKey(_+_).print

    ssc.start()
    ssc.awaitTermination()
    }
    }
     

    http://spark.apache.org/docs/1.6.3/streaming-flume-integration.html

  • 相关阅读:
    PAT 1025. 反转链表 (25)
    PAT 1024. 科学计数法 (20)
    PAT 1076. Forwards on Weibo (30)
    C++——cout输出小数点后指定位数
    PTA 06-图3 六度空间 (30分)
    PTA 06-图2 Saving James Bond
    PTA
    浙大PTA
    浙大PTA
    随机密码生成
  • 原文地址:https://www.cnblogs.com/rocky-AGE-24/p/7357368.html
Copyright © 2011-2022 走看看