设为首页 加入收藏

TOP

spark streaming整合flume(常用的poll类型)
2018-11-28 18:06:18 】 浏览:57
Tags:spark streaming 整合 flume 常用 poll 类型

在实际生产工作环境中,spark streaming经常和flume或者kafka整合在一起使用,本片文章说下与flume整合过程。

常用的整合方式有两种,一种是push类型,一种是poll类型,在实际分布式生产环境下,我们使用poll类型,也就是由spark streaming向flume拿数据,push类型是flume向streaming 送数据。我们这里只说下poll类型的整合方式。

1,安装flume(不解释,之前文章有详解)

2,将这三个jar包放到安装好的flume的lib目录下。

(链接:https://pan.baidu.com/s/1_zouri5jOGMdk9J3d7nDsQ 密码:v001)

commons-lang3-3.3.2.jar ,scala-library-2.10.5.jar ,spark-streaming-flume-sink_2.10-1.6.1.jar

3,添加flume-poll.conf 到flume的conf目录下

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /export/data/flume
a1.sources.r1.fileHeader = true

# Describe the sink
a1.sinks.k1.type = org.apache.spark.streaming.flume.sink.SparkSink
a1.sinks.k1.hostname = 192.168.2.201
a1.sinks.k1.port = 8888

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

4,启动flume

bin/flume-ng agent -n a1 -c conf -f conf/flume-poll.conf

5, 编写scala spark streaming程序 (wordcount小程序,统计同一字符出现的个数)

package cn.itcast.spark.day5

import java.net.InetSocketAddress

import org.apache.spark.SparkConf
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.flume.FlumeUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}
object FlumePollWordCount {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("FlumePollWordCount").setMaster("local[2]")
    val ssc = new StreamingContext(conf, Seconds(5))
    //从flume中拉取数据(flume的地址)
    val address = Seq(new InetSocketAddress("192.168.2.201", 8888))
    val flumeStream = FlumeUtils.createPollingStream(ssc, address, StorageLevel.MEMORY_AND_DISK)
    val words = flumeStream.flatMap(x => new String(x.event.getBody().array()).split(" ")).map((_,1))
    val results = words.reduceByKey(_+_)
    results.print()
    ssc.start()
    ssc.awaitTermination()
  }
}

6, 启动scala程序

7, 在虚拟机的 /export/data/flume 目录下添加一个文件 word.txt ,里面随便写一下东西

如: a a a a b b b c c d d

8, 查看scala程序后台输出:

9, 完毕

10,集群方式部署启动 (push模式)

flume conf 添加 flume-push.conf 文件

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# source
a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /export/data/flume
a1.sources.r1.fileHeader = true

# Describe the sink
a1.sinks.k1.type = org.apache.spark.streaming.flume.sink.SparkSink
a1.sinks.k1.hostname = 192.168.2.201
a1.sinks.k1.port = 8888

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1


#启动flume
bin/flume-ng agent -n a1 -c conf/ -f conf/flume-push.conf -Dflume.root.logger=WARN,console
#再启动spark-streaming应用程序

package cn.itcast.spark.day5

import org.apache.spark.SparkConf
import org.apache.spark.streaming.flume.FlumeUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * Created by ZX on 2015/6/22.
  */
object FlumePushWordCount {

  def main(args: Array[String]) {
    //集群方式启动
    val host = args(0)
    val port = args(1).toInt
    //LoggerLevels.setStreamingLogLevels()
    val conf = new SparkConf().setAppName("FlumeWordCount")//.setMaster("local[2]")
    val ssc = new StreamingContext(conf, Seconds(5))
    //推送方式: flume向spark发送数据
    val flumeStream = FlumeUtils.createStream(ssc, host, port)
    //flume中的数据通过event.getBody()才能拿到真正的内容
    val words = flumeStream.flatMap(x => new String(x.event.getBody().array()).split(" ")).map((_, 1))

    val results = words.reduceByKey(_ + _)
    results.print()
    ssc.start()
    ssc.awaitTermination()
  }
}

bin/spark-submit --master spark://weekend01:7077 --class cn.itcast.spark.day5.FlumePushWordCount /home/bigdata/streaming-1.0.jar 192.168.2.201 8888

完活 !

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇flume 1.7.0-taildirSource 支.. 下一篇flume多级agent测试中遇到的问题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目