Spark2.x学习笔记:16、Spark Streaming入门实例NetworkWordCount

16、 Spark Streaming入门实例NetworkWordCount

16.1 源码解析

在Spark源码的spark-2.1.0-bin-hadoop2.7\examples\src\main\scala\org\apache\spark\examples\streaming目录下即可找到NetworkWordCount.scala源文件。

// scalastyle:off println
package org.apache.spark.examples.streaming

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

object NetworkWordCount {
  def main(args: Array[String]) {
    if (args.length < 2) {
      System.err.println("Usage: NetworkWordCount <hostname> <port>")
      System.exit(1)
    }

    StreamingExamples.setStreamingLogLevels()

    // Create the context with a 1 second batch size
    //创建SparkConf实例
    val sparkConf = new SparkConf().setAppName("NetworkWordCount")
    //每隔1秒钟处理一批数据
    val ssc = new StreamingContext(sparkConf, Seconds(1))

    // Create a socket stream on target ip:port and count the
    // words in input stream of \n delimited text (eg. generated by 'nc')
    // Note that no duplication in storage level only for running locally.
    // Replication necessary in distributed scenario for fault tolerance.
    //创建一个socket流,监听args(0)的args(1)端口输入的数据
    val lines = ssc.socketTextStream(args(0), args(1).toInt, StorageLevel.MEMORY_AND_DISK_SER)
    //flatMap是把将每一行使用空格做分解,words是个集合,存放单词
    val words = lines.flatMap(_.split(" "))
    //单词个数统计
    val wordCounts = words.map(x => (x, 1)).reduceByKey(_ + _)
    wordCounts.print()
    //启动计算作业
    ssc.start()
    //等待结束,什么时候结束作业,即触发什么条件会让作业执行结束
    ssc.awaitTermination()
  }
}
// scalastyle:on println

16.2 测试运行

(1)修改CPU核数 因为我的Spark程序是在虚拟机中运行,如果虚拟机是单核,会导致NetworkWordCount卡住。所以需要修改虚拟机的核心数,这里修改node1节点为2个核即可。

(2)nc nc是netcat的命令。如果没有安装nc软件,可以通过下面命令安装

[root@node1 ~]# yum install nc
[root@node1 ~]# nc --help
...
  -l, --listen               Bind and listen for incoming connections
  -k, --keep-open            Accept multiple connections in listen mode
...

永久监听TCP端口9999

[root@node1 ~]# nc -lk 9999

(3)启动NetworkWordCount

[root@node1 ~]# run-example org.apache.spark.examples.streaming.NetworkWordCount localhost 9999
17/11/01 09:26:38 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
-------------------------------------------
Time: 1509542803000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542804000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542805000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542806000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542807000 ms
-------------------------------------------

(4)流处理

在9999端口不停输入数据

[root@node1 ~]# nc -lk 9999
hi
hello
how do you do!
i am hadron
java
hadoop
spark

可以看到NetworkWordCount 实时处理结果

[root@node1 ~]# run-example org.apache.spark.examples.streaming.NetworkWordCount localhost 9999
17/11/01 09:26:38 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
-------------------------------------------
Time: 1509542803000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542804000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542805000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542806000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542807000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542808000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542809000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542810000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542811000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542812000 ms
-------------------------------------------
(hi,1)

-------------------------------------------
Time: 1509542813000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542814000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542815000 ms
-------------------------------------------
(hello,1)

-------------------------------------------
Time: 1509542816000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542817000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542818000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542819000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542820000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542821000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542822000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542823000 ms
-------------------------------------------
(do!,1)
(how,1)
(you,1)
(do,1)

-------------------------------------------
Time: 1509542824000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542825000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542826000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542827000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542828000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542829000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542830000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542831000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542832000 ms
-------------------------------------------
(hadron,1)
(am,1)
(i,1)

-------------------------------------------
Time: 1509542833000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542834000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542835000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542836000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542837000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542838000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542839000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542840000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542841000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542842000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542843000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542844000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542845000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542846000 ms
-------------------------------------------
(java,1)

-------------------------------------------
Time: 1509542847000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542848000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542849000 ms
-------------------------------------------
(hadoop,1)

-------------------------------------------
Time: 1509542850000 ms
-------------------------------------------

-------------------------------------------
Time: 1509542851000 ms
-------------------------------------------
(spark,1)

-------------------------------------------
Time: 1509542852000 ms

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏ml

hdu----(2848)Repository(trie树变形)

Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K...

3438
来自专栏腾讯BBTeam团队的专栏

sar - Linux 系统监控利器

sar可以从多方面对系统的活动进行报告,包括:文件的读写情况、系统调用的使用情况、磁盘I/O、CPU效率、内存使用状况、进程活动及IPC有关的活动等。

2196
来自专栏蓝天

UNIX和Linux信号

1 ~ 31的信号为传统UNIX支持的信号,是不可靠信号(非实时的),编号为32 ~ 63的信号是后来扩充的,称做可靠信号(实时信号)。不可靠信号和可靠信号的区...

1084
来自专栏程序员的诗和远方

Python报错: Unhandled exception in thread started by Error in sys.excepthook

今天要写个简单脚本,模拟同时50个用户往服务器上传东西。 就简单用 thread.start_new_thread(func, ()) 结果运行的时候报错: ...

33110
来自专栏乐沙弥的世界

Oracle 11g RAC CRS-4535/ORA-15077

    新安装了Oracle 11g rac之后,不知道是什么原因导致第二个节点上的crsd无法启动?其错误消息是CRS-4535: Cannot commun...

623
来自专栏逸鹏说道

1.并发编程~先导篇(上)

并发 :一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行。

853
来自专栏大数据-Hadoop、Spark

Flume + Kafka + Spark Streaming整合

2134
来自专栏乐享123

Udp Packet Receive Errors

2875
来自专栏乐沙弥的世界

Oracle 11g RAC crs_stat 命令结果完整显示

Oracle 11g RAC中crs_stat命令较之前的版本多出了很多新的不同的资源类型,缺省情况下,使用crs_stat -t来查看资源是密密麻麻一大片,看...

1271
来自专栏逸鹏说道

并发编程~先导篇上

并发 :一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行。

1548

扫码关注云+社区