Flume+Kafka+Spark-Streaming的实时流式处理完整流程

基于Flume+Kafka+Spark-Streaming的实时流式处理完整流程

1、环境准备,四台测试服务器

spark集群三台,spark1,spark2,spark3

kafka集群三台,spark1,spark2,spark3

zookeeper集群三台,spark1,spark2,spark3

日志接收服务器, spark1

日志收集服务器,Redis (这台机器用来做redis开发的,现在用来做日志收集的测试,主机名就不改了)

日志收集流程:

日志收集服务器->日志接收服务器->kafka集群->spark集群处理

说明: 日志收集服务器,在实际生产中很有可能是应用系统服务器,日志接收服务器为大数据服务器中一台,日志通过网络传输到日志接收服务器,再入集群处理。

因为,生产环境中,往往网络只是单向开放给某台服务器的某个端口访问的。

Flume版本: apache-flume-1.5.0-cdh5.4.9 ,该版本已经较好地集成了对kafka的支持

2、日志收集服务器(汇总端)

配置flume动态收集特定的日志,collect.conf 配置如下:

# Name the components on this agent  
    a1.sources = tailsource-1  
    a1.sinks = remotesink  
    a1.channels = memoryChnanel-1  

    # Describe/configure the source  
    a1.sources.tailsource-1.type = exec  
    a1.sources.tailsource-1.command = tail -F /opt/modules/tmpdata/logs/1.log  

    a1.sources.tailsource-1.channels = memoryChnanel-1  

    # Describe the sink  
    a1.sinks.k1.type = logger  

    # Use a channel which buffers events in memory  
    a1.channels.memoryChnanel-1.type = memory  
    a1.channels.memoryChnanel-1.keep-alive = 10  
    a1.channels.memoryChnanel-1.capacity = 100000  
    a1.channels.memoryChnanel-1.transactionCapacity = 100000  

    # Bind the source and sink to the channel  
    a1.sinks.remotesink.type = avro  
    a1.sinks.remotesink.hostname = spark1  
    a1.sinks.remotesink.port = 666  
    a1.sinks.remotesink.channel = memoryChnanel-1  

日志实时监控日志后,通过网络avro类型,传输到spark1服务器的666端口上

启动日志收集端脚本:

bin/flume-ng agent --conf conf --conf-file conf/collect.conf --name a1 -Dflume.root.logger=INFO,console  

3、日志接收服务器

配置flume实时接收日志,collect.conf 配置如下:

#agent section    
    producer.sources = s    
    producer.channels = c    
    producer.sinks = r    

    #source section    
    producer.sources.s.type = avro  
    producer.sources.s.bind = spark1  
    producer.sources.s.port = 666  

    producer.sources.s.channels = c    

    # Each sink's type must be defined    
    producer.sinks.r.type = org.apache.flume.sink.kafka.KafkaSink  
    producer.sinks.r.topic = mytopic  
    producer.sinks.r.brokerList = spark1:9092,spark2:9092,spark3:9092  
    producer.sinks.r.requiredAcks = 1  
    producer.sinks.r.batchSize = 20  
    producer.sinks.r.channel = c1  

    #Specify the channel the sink should use    
    producer.sinks.r.channel = c    

    # Each channel's type is defined.    
    producer.channels.c.type   = org.apache.flume.channel.kafka.KafkaChannel  
    producer.channels.c.capacity = 10000  
    producer.channels.c.transactionCapacity = 1000  
    producer.channels.c.brokerList=spark1:9092,spark2:9092,spark3:9092  
    producer.channels.c.topic=channel1  
    producer.channels.c.zookeeperConnect=spark1:2181,spark2:2181,spark3:2181  

关键是指定如源为接收网络端口的666来的数据,并输入kafka的集群,需配置好topic及zk的地址

启动接收端脚本:

bin/flume-ng agent --conf conf --conf-file conf/receive.conf --name producer -Dflume.root.logger=INFO,console  

4、spark集群处理接收数据

    import org.apache.spark.SparkConf  
    import org.apache.spark.SparkContext  
    import org.apache.spark.streaming.kafka.KafkaUtils  
    import org.apache.spark.streaming.Seconds  
    import org.apache.spark.streaming.StreamingContext  
    import kafka.serializer.StringDecoder  
    import scala.collection.immutable.HashMap  
    import org.apache.log4j.Level  
    import org.apache.log4j.Logger  

    /** 
     * @author Administrator 
     */  
    object KafkaDataTest {  
      def main(args: Array[String]): Unit = {  

        Logger.getLogger("org.apache.spark").setLevel(Level.WARN);  
        Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.ERROR);  

        val conf = new SparkConf().setAppName("stocker").setMaster("local[2]")  
        val sc = new SparkContext(conf)  

        val ssc = new StreamingContext(sc, Seconds(1))  

        // Kafka configurations  

        val topics = Set("mytopic")  

        val brokers = "spark1:9092,spark2:9092,spark3:9092"  

        val kafkaParams = Map[String, String]("metadata.broker.list" -> brokers, "serializer.class" -> "kafka.serializer.StringEncoder")  

        // Create a direct stream  
        val kafkaStream = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParams, topics)  

        val urlClickLogPairsDStream = kafkaStream.flatMap(_._2.split(" ")).map((_, 1))  

        val urlClickCountDaysDStream = urlClickLogPairsDStream.reduceByKeyAndWindow(  
          (v1: Int, v2: Int) => {  
            v1 + v2  
          },  
          Seconds(60),  
          Seconds(5));  

        urlClickCountDaysDStream.print();  

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

spark-streaming接收到kafka集群后的数据,每5s计算60s内的wordcount值

5、测试结果

这里写图片描述

往日志中依次追加三次日志

spark-streaming处理结果如下:

(Hive,1)
(spark,2)
(Hadoop,2)
(storm,1)

---------------------------------------

(hive,1)
(spark,3)
(hadoop,3)
(storm,1)

---------------------------------------

(hive,2)
(spark,5)
(hadoop,5)
(storm,2)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值