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 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

3248
来自专栏hbbliyong

WPF Trigger for IsSelected in a DataTemplate for ListBox items

<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}"> <vw:HeaderSlug...

4214
来自专栏跟着阿笨一起玩NET

c#实现打印功能

3562
来自专栏魂祭心

原 canvas绘制clock

4994
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

2810
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

8348
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

2997
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2965
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3605
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.4K7

扫码关注云+社区