在实际生产工作环境中,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
完活 !