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. 手机APP测试技术-整体测试流程框架

    一  手机APP测试基本思路: 测试计划--测试方案--测试用例--执行: 很多小公司都没有具体的需求,项目时间也比较紧,而且流程也不是很严谨,在这样的情况之下,作为测试的我们,该怎样去对项目进行用例 ...

  2. nginx配置及常见问题

    问题 1.openresty请求时,不能解析域名? openresty依赖配置里面的resolver 192.168.1.1; 2.文件上传是报错413 Request Entity Too Larg ...

  3. 七. 多线程编程2.Java线程模型

    Java运行系统在很多方面依赖于线程,所有的类库设计都考虑到多线程.实际上,Java使用线程来使整个环境异步.这有利于通过防止CPU循环的浪费来减少无效部分. 为更好的理解多线程环境的优势可以将它与它 ...

  4. 六. 异常处理8.throws子句

    如果一个方法可以导致一个异常但不处理它,它必须指定这种行为以使方法的调用者可以保护它们自己而不发生异常.做到这点你可以在方法声明中包含一个throws子句.一个 throws 子句列举了一个方法可能抛 ...

  5. Android的数据存储方式概述

    数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 ...

  6. Word交叉引用

    第一种:参考文献,用NE插入. 第二种:交叉引用. 先定义新的编号格式[1](主要解决参考文献格式自动编号的问题),感觉但是没有解决缩进的问题,需要Tab. 但是实验发现,通过谷歌学术引用的参考文献插 ...

  7. jdk7 cpocurrent ForJoinPool

    19. 使用 ForkJoinPool 进行分叉和合并 ForkJoinPool 在 Java 7 中被引入.它和 ExecutorService 很相似,除了一点不同.ForkJoinPool 让我 ...

  8. 能上架App的GooglePlay开发者账号获取流程

    googleplay 开发者账号申请流程 接到公司号召,要让我们的app走向世界,上架GooglePlay,都说天朝的Android 程序员是折翼的天使,猛然发现写了做么多年的Android,竟然不知 ...

  9. nginx服务器设置path_info模式

    1.find / -name nginx.conf找到nginx配置文件 2. ## The default server#server { listen 80; #填写自己的域名 server_na ...

  10. 如何给JQ的ajax方法中的success()传入参数?

    当时在使用JQuery提供的Ajax技术的时候,我有个需求,就是要给它请求成功后调用的success()方法传入参数: 所以,我就直接这样子写了: <script> function ge ...