Consumer代码

import org.apache.spark.SparkConf
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.Seconds
import org.apache.spark.storage.StorageLevel
object NetWorkStream {
def main(args: Array[String]): Unit = {
//创建sparkConf对象
var conf=new SparkConf().setMaster("spark://192.168.177.120:7077").setAppName("netWorkStream");
//创建streamingContext:是所有数据流的一个主入口
//Seconds(1)代表每一秒,批量执行一次结果
var ssc=new StreamingContext(conf,Seconds());
//从192.168.99.143接受到输入数据
var lines= ssc.socketTextStream("192.168.99.143", );
//计算出传入单词的个数
var words=lines.flatMap { line => line.split(" ")}
var wordCount= words.map { w => (w,) }.reduceByKey(_+_);
//打印结果
wordCount.print();
ssc.start();//启动进程
ssc.awaitTermination();//等待计算终止
}
在另一台机器上出入
nc -lk
zhang xing sheng zhang
 
消费者终端会显示消费结果
// :: INFO scheduler.TaskSetManager: Finished task 0.0 in stage 128.0 (TID ) in  ms on 192.168.177.120 (/)
// :: INFO scheduler.TaskSchedulerImpl: Removed TaskSet 128.0, whose tasks have all completed, from pool
// :: INFO scheduler.DAGScheduler: ResultStage (print at NetWorkStream.scala:) finished in 0.031 s
// :: INFO scheduler.DAGScheduler: Job finished: print at NetWorkStream.scala:, took 0.080836 s
// :: INFO spark.SparkContext: Starting job: print at NetWorkStream.scala:
// :: INFO scheduler.DAGScheduler: Got job (print at NetWorkStream.scala:) with output partitions
// :: INFO scheduler.DAGScheduler: Final stage: ResultStage (print at NetWorkStream.scala:)
// :: INFO scheduler.DAGScheduler: Parents of final stage: List(ShuffleMapStage )
// :: INFO scheduler.DAGScheduler: Missing parents: List()
// :: INFO scheduler.DAGScheduler: Submitting ResultStage (ShuffledRDD[] at reduceByKey at NetWorkStream.scala:), which has no missing parents
// :: INFO memory.MemoryStore: Block broadcast_67 stored as values in memory (estimated size 2.8 KB, free 366.2 MB)
// :: INFO memory.MemoryStore: Block broadcast_67_piece0 stored as bytes in memory (estimated size 1711.0 B, free 366.2 MB)
// :: INFO storage.BlockManagerInfo: Added broadcast_67_piece0 in memory on 192.168.177.120: (size: 1711.0 B, free: 366.3 MB)
// :: INFO spark.SparkContext: Created broadcast from broadcast at DAGScheduler.scala:
// :: INFO scheduler.DAGScheduler: Submitting missing tasks from ResultStage (ShuffledRDD[] at reduceByKey at NetWorkStream.scala:)
// :: INFO scheduler.TaskSchedulerImpl: Adding task set 130.0 with tasks
// :: INFO scheduler.TaskSetManager: Starting task 0.0 in stage 130.0 (TID , 192.168.177.120, partition , NODE_LOCAL, bytes)
// :: INFO cluster.CoarseGrainedSchedulerBackend$DriverEndpoint: Launching task on executor id: hostname: 192.168.177.120.
// :: INFO storage.BlockManagerInfo: Added broadcast_67_piece0 in memory on 192.168.177.120: (size: 1711.0 B, free: 366.3 MB)
// :: INFO scheduler.TaskSetManager: Finished task 0.0 in stage 130.0 (TID ) in ms on 192.168.177.120 (/)
// :: INFO scheduler.TaskSchedulerImpl: Removed TaskSet 130.0, whose tasks have all completed, from pool
// :: INFO scheduler.DAGScheduler: ResultStage (print at NetWorkStream.scala:) finished in 0.014 s
// :: INFO scheduler.DAGScheduler: Job finished: print at NetWorkStream.scala:, took 0.022658 s
-------------------------------------------
Time: ms
-------------------------------------------
(xing,)
(zhang,)
(sheng,)
 
备注:
var conf=new SparkConfig();
new StreamingContext(conf,Seconds(1));//创建context
  1. 定义上下文之后,你应该做下面事情
  2. After a context is defined, you have to do the following.
  3. 根据创建DStream定义输入数据源
  4. Define the input sources by creating input DStreams.
  5. 定义计算方式DStream转换和输出
  6. Define the streaming computations by applying transformation and output operations to DStreams.
  7. 使用streamingContext.start()启动接受数据的进程
  8. Start receiving data and processing it using streamingContext.start().
  9. 等待进程结束
  10. Wait for the processing to be stopped (manually or due to any error) using streamingContext.awaitTermination().
  11. 手动关闭进程
  12. The processing can be manually stopped using streamingContext.stop().
要点
  1. 一旦一个上下文启动,不能在这个上下文中设置新计算或者添加
  2. Once a context has been started, no new streaming computations can be set up or added to it.
  3. 一旦一个上下文停止,就不能在重启
  4. Once a context has been stopped, it cannot be restarted.
  5. 在同一时间一个jvm只能有一个StreamingContext 在活动
  6. Only one StreamingContext can be active in a JVM at the same time.//ssc.stop(false)
  7. 在StreamingContext 上使用stop函数,同事也会停止sparkContext,仅仅停止StreamingContext,在调用stopSparkContext设置参数为false
  8. stop() on StreamingContext also stops the SparkContext. To stop only the StreamingContext, set the optional parameter of stop() called stopSparkContext to false.
  9. 一个SparkContext 可以创建多个streamingContext和重用,只要在上一个StreamingContext停止前创建下一个StreamingContext
  10. 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.

sparkstreaming+socket workCount 小案例的更多相关文章

  1. C# Socket通信 小案例

    本文将编写2个控制台应用程序,一个是服务器端(server),一个是客户端(client), 通过server的监听,有新的client连接后,接收client发出的信息. server代码如下: u ...

  2. MVC 小案例 -- 信息管理

    前几次更新博客都是每次周日晚上到周一,这次是周一晚上开始写,肯定也是有原因的!那就是我的 Tomact 忽然报错,无法启动,错误信息如下!同时我的 win10 也崩了,重启之后连 WIFI 的标志也不 ...

  3. Python:通过一个小案例深入理解IO多路复用

    通过一个小案例深入理解IO多路复用 假如我们现在有这样一个普通的需求,写一个简单的爬虫来爬取校花网的主页 import requests import time start = time.time() ...

  4. 机械表小案例之transform的应用

    这个小案例主要是对transform的应用. 时钟的3个表针分别是3个png图片,通过setInterval来让图片转动.时,分,秒的转动角度分别是30,6,6度. 首先,通过new Date函数获取 ...

  5. shell讲解-小案例

    shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...

  6. [jQuery学习系列六]6-jQuery实际操作小案例

    前言最后在这里po上jQuery的几个小案例. Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> &l ...

  7. 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:

  8. React.js入门小案例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  9. SqlDependency缓存数据库表小案例

    SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...

随机推荐

  1. ESXI和vSphere的安装配置-实现一台电脑硬件虚拟化为两台

    本篇文章包含以下几个部分: 1EXSI软件和vSphere软件的安装 2在vSphere上安装虚拟系统 3对虚拟系统通过配置实现硬件虚拟化,实现硬件直通 1.EXSI安装 通过网上下载EXSI ISO ...

  2. python实现无重复字符串的最长子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...

  3. [Contest20180418]数学竞赛

    题意:初始时$x=0$(长度),当$x$为长度时,你可以把$x$变成$\sin^{-1}x,\cos^{-1}x,\tan^{-1}x$之一($x$变为角度),若$x$为角度,你可以把$x$变成$\s ...

  4. Map泛型集合-输入名字输出成绩

    package collection; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import ...

  5. [置顶] kubernetes资源类型--Volume

    在Docker的设计实现中,容器中的数据是临时的,即当容器被销毁时,其中的数据将会丢失.如果需要持久化数据,需要使用Docker数据卷挂载宿主机上的文件或者目录到容器中.在K8S中,当Pod重建的时候 ...

  6. haproxy + rabbitmq + keepalived的高可用环境搭建

    一.rabbitmq的搭建:参考rabbimq的安装及集群设置 二.安装和配置haproxy 1.安装haproxyyum install haproxy 2.安装rsysloga. 检查rsyslo ...

  7. 如何使用CodeSmith批量生成代码

    在上一篇我们已经用PowerDesigner创建好了需要的测试数据库,下面就可以开始用它完成批量代码生成的工作啦. 下面我会一步步的解释如何用CodeSmith实现预期的结果的,事先声明一下,在此只做 ...

  8. [Other] An Overview of Arrays and Memory

    One integer takes 32bit in memory, 1 byte = 8bits, therefore one integer takes 4 bytes. Now let's as ...

  9. JMeter接口测试中文乱码问题总结

    在测试过程中遇到了请求json串中文乱码,所以查看了这篇文章,将字符集修改后,乱码问题已经处理. 转载http://blog.csdn.net/qing_java/article/details/69 ...

  10. goruntine

    一.出让时间片 runtime.Gosched() 二.同步锁 Go语言包中的sync包提供了两种锁类型:sync.Mutex和sync.RWMutex.Mutex是最简单的一种锁类型,同时也比较暴力 ...