Spark Streaming介绍以及简单使用

一、Spark Streaming介绍

Spark Streaming is an extension of the core Spark API that enables scalable, high-throughput, fault-tolerant stream processing of live data streams.
Spark Streaming是Spark core的扩展,有以下特点:高可扩展、高吞吐量的、容错的流式程序作用在数据流上
Data can be ingested from many sources like Kafka, Flume, Kinesis, or TCP sockets, and can be processed using complex algorithms
expressed with high-level functions like map, reduce, join and window.
数据能够被采集,从Kafka, Flume, Kinesis, or TCP sockets中,并且可以被处理,使用复杂的算法通过高级别得方法表达,比如:map, reduce, join and window
Finally, processed data can be pushed out to filesystems, databases, and live dashboards.
最终,处理过的数据能够被推到文件系统、数据库以及在线的dashboards(仪表板)。
In fact, you can apply Spark’s machine learning and graph processing algorithms on data streams.
事实上,你可以在实时的数据流上应用Spark的机器学习和图形处理算法
在这里插入图片描述

1、总结:

将不同数据源的数据经过Spark Streaming处理之后将结果输出到外部文件系统

2、特点:

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

3、工作原理:
粗粒度

数据流进入,Spark Streaming接收到实时数据流,把数据按照指定的时间段切成一片一片小的数据块,然后把小的数据块传给 Spark Engine处理后批次输出
在这里插入图片描述

二、Spark Streaming使用

1、Github

https://github.com/apache/spark/

2、启动nc
yum install -y nc
nc -lk 9999
3、spark-submit使用
$SPARK_HOME/bin/spark-submit --master local[2] \
--class org.apache.spark.examples.streaming.NetworkWordCount \
--name NetworkWordCount \
/app/cdh/spark-2.3.0-bin-hadoop2.6/examples/jars/spark-examples_2.11-2.3.0.jar dev2 9999

–class 使用包名org.apache.spark.examples.streaming + 类名
在这里插入图片描述

4、如何使用spark-shell测试
$SPARK_HOME/bin/spark-shell --master local[2]

#由于sc在spark-shell中已经自带,我们直接使用就可以了

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

#StorageLevel.MEMORY_AND_DISK_SER这个参数是设置spark处理数据的存储方式,如果数据量少,我们一般使用内存

猜你喜欢

转载自blog.csdn.net/u013429010/article/details/83004829
今日推荐