Spark Streaming 基本概念及操作

1 Spark Streaming

Spark Streaming is an extension of the core Spark API(spark core的拓展) that enables scalable(高可用),
high-throughput(高吞吐), fault-tolerant(容错的) stream processing of live data streams(实时数据流).

Spark Streaming主要是把不同数据源的数据实时处理以后落地到外部文件系统。

特点:
低延时
能从错误中高效的恢复:fault-tolerant
能够运行在成百上千的节点
能够将批处理、机器学习、图计算等子框架和Spark Streaming综合起来使用

Spark Streaming不需要独立安装 One stack to rule them all

spark-submit的使用

使用spark-submit来提交我们的spark应用程序运行的脚本(生产)

 ./spark-submit --master local[2] \
 --class org.apache.spark.examples.streaming.NetworkWordCount \
 --name NetworkWordCount \
 /home/hadoop/app/spark-2.2.0-bin-2.6.0-cdh5.7.0/examples/jars/spark-examples_2.11-2.2.0.jar hadoop000 9999

如何使用spark-shell来提交(测试)

 ./spark-shell --master local[2] (写成脚本)
/opt/spark/bin/spark-shell --master local[2] (直接运行)

local[2]中为的线程数为2,因为如果使用Receiver本身要占一个线程,所以不能使用local或者local[1]
运行以下代码即可

 import org.apache.spark.streaming.{Seconds, StreamingContext}
 val ssc = new StreamingContext(sc, Seconds(1))
 val lines = ssc.socketTextStream("hadoop000", 9999)
 val words = lines.flatMap(_.split(" "))
 val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
 wordCounts.print()
 ssc.start()
 ssc.awaitTermination()

工作原理:粗粒度
Spark Streaming接收到实时数据流,把数据按照指定的时间段切成一片片小的数据块,
然后把小的数据块传给Spark Engine处理。

2 StreamingContext

核心概念:
StreamingContext

 def this(sparkContext: SparkContext, batchDuration: Duration) = {
     this(sparkContext, null, batchDuration)
 }
 def this(conf: SparkConf, batchDuration: Duration) = {
     this(StreamingContext.createNewSparkContext(conf), null, batchDuration)
 }

batch interval可以根据你的应用程序需求的延迟要求以及集群可用的资源情况来设置

StreamingContext设置好以后,可以做以下事情:
1.Define the input sources by creating input DStreams.通过创建输入的DStreams来定义输入源
2.Define the streaming computations by applying transformation and output operations to DStreams.
通过对DStreams使用转换和输出操作来定义流计算。
3.Start receiving data and processing it using streamingContext.start().
开始接收数据并处理,使用streamingContext.start()。
4.Wait for the processing to be stopped (manually or due to any error) using streamingContext.awaitTermination().
使用streamingcontext.awaitterminate()来等待停止处理(手动或由于任何错误)。
5.The processing can be manually stopped using streamingContext.stop().
可以使用streamingContext.stop()手动停止处理。

注意点:
1.Once a context has been started, no new streaming computations can be set up or added to it.
一旦context启动以后,流计算的的设置就被定义好了,无法添加新的流计算
2.Once a context has been stopped, it cannot be restarted.
一旦context被停止,不能被重启(stop以后不能start,start方法不能写在stop方法之后)
3.Only one StreamingContext can be active in a JVM at the same time.
在同一时段,一个StreamingContext只能存在于一个JVM中
4.stop() on StreamingContext also stops the SparkContext. To stop only the StreamingContext, set the optional parameter of stop() called stopSparkContext to false.
stop方法可以停止sparkContent,如果仅仅想停止streamingContext而不想停止sparkContent,可以设置stop中的一个参数stopSparkContext为false
5.A SparkContext can be re-used to create multiple StreamingContexts, as long as the previous StreamingContext is stopped (without stopping the SparkContext) before the next StreamingContext is created.
一个SparkContext可以创建多个StreamingContext。

Discretized Streams (DStreams)
持续化的数据流(一系列的rdd)
对DStreams操作算子,其实底层会对DStreams中的每个rdd做相同的操作。

Input Dstreams and Receivers
除了文件系统(file stream)以外,每一个input Dstream都需要关联一个Receiver,Receiver用来从源头接收数据存在
file stream可以直接处理,不需要receiver,例如hdfs中的数据,可以直接用api读取。

注意点:
如果使用Receiver,不要使用local或者local[1](1代表一个线程),因为Receiver会占用一个线程,接收了数据以后就无法处理了。所以如果使用本地模式,设置为local[n],且n要大于receiver的数量。

spark Streaming处理socket数据
词频统计测试:
先在终端机输入:
hostname -f
获取hostname
再输入
nc -lk 9999
再在spark-shell中运行以下代码

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

    val sc = new SparkConf()
    val ssc = new StreamingContext(sc, Seconds(20)) //Seconds(1)数据按照1秒钟一个批次一次

//args(0)对应主机名即hostname,args(1)对应端口号
//val lines = ssc.socketTextStream(args(0), args(1).toInt)

    val lines = ssc.socketTextStream("yjd-dn-50-163.meizu.mz", 9999)
    val words = lines.flatMap(_.split(" "))
    val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
    wordCounts.print()
    ssc.start()
    ssc.awaitTermination()

在终端机传入参数:
spark spark aaaaaaa aaaaaa
得到结果:


3.配置依赖

Spark Streaming通常和kafka/Hbase/redis/flume等等一起使用
所以需要整合kafka/flume

    <!-- Kafka 依赖-->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_${scala.version}</artifactId>
        <version>${kafka.version}</version>
    </dependency>
    <!-- HBase 依赖-->
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-client</artifactId>
      <version>${hbase.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hbase</groupId>
      <artifactId>hbase-server</artifactId>
      <version>${hbase.version}</version>
    </dependency>
    <!-- Spark Streaming 依赖-->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-streaming_${scala.version}</artifactId>
      <version>${spark.version}</version>
    </dependency>
      <!-- Spark Streaming整合Flume 依赖-->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-streaming-flume_${scala.version}</artifactId>
      <version>${spark.version}</version>
    </dependency>
    <!-- Spark Streaming整合Kafka 依赖-->
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-streaming-kafka-${scala.version}</artifactId>
      <version>${streaming.kafka.version}</version>
    </dependency>

4.sparkstreaming接收kafka数据存入hive(搬运)

object KafkaToHive{
    def main(args: Array[String]){
        val sparkConf = new SparkConf().setAppName("KafkaToHive")
        val sc = new SparkContext(sparkConf)
        val ssc = new StringContext(sc,Seconds(60))
        // 创建kafka参数
        val kafkaParams = Map[String,Object](
            //ip为kafka集群ip,端口为集群端口
            "bootstrap.servers" -> "ip1:port1,ip2:port2,ip:port3",
            "group.id" -> "KafkaToHive_group1",  //自定义组名称
            "auto.offset.reset" -> "earliest",
            "enable.auto.commit" -> "false")
        val topics = Array("test1")
        val stream = KafkaUtils.createDirectStreaming[String,String](
            ssc,PreferConsistent,
            Subscribe[String,String](topics,kafkaParms)
        stream.foreachRDD(rdd=>{
            if(rdd.count>0){
                val offsetRanges = rdd.asInstanceOf[HasOffsetRanges].offsetRanges
                //TODO 具体处理逻辑
                //写入Hive
                //value为实际操作中的结果集,即是//TODO返回的结果集
                val subRdd = rdd.sparkContext.parallelize(value)
                val sqlContext : SQLContext = new HiveContext(rdd.sparkContext)
                sqlContext.setConf("hive.exec.dynamic.partition.mode","nonstrict")
                sqlContext.setConf("hive.exec.dynamic.partition","true")                                                                                                                           sqlContext.sql("use database1")
                val tempTable = sqlContext
                .read
                .format("json")
                .json(subRdd)
                .select(cols.map(new Column(_)): _*)
                .coalesce(1)
                .write
                .mode(SaveMode.Append)
                .insertInto("task_exec_time")
                //提交offset
               stream.asInstanceOf[CanCommitOffsets].commotAsync(offsetRanges)
        }
    })
}

Reference

https://blog.csdn.net/weixin_44278340/article/details/85260073

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容