sparkstreaming+socket workCount 小案例
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,)
- 定义上下文之后,你应该做下面事情
After a context is defined, you have to do the following.- 根据创建DStream定义输入数据源
- Define the input sources by creating input DStreams.
- 定义计算方式DStream转换和输出
Define the streaming computations by applying transformation and output operations to DStreams.- 使用streamingContext.start()启动接受数据的进程
Start receiving data and processing it using streamingContext.start().- 等待进程结束
Wait for the processing to be stopped (manually or due to any error) using streamingContext.awaitTermination().- 手动关闭进程
The processing can be manually stopped using streamingContext.stop().
- 一旦一个上下文启动,不能在这个上下文中设置新计算或者添加
Once a context has been started, no new streaming computations can be set up or added to it.- 一旦一个上下文停止,就不能在重启
Once a context has been stopped, it cannot be restarted.- 在同一时间一个jvm只能有一个StreamingContext 在活动
Only one StreamingContext can be active in a JVM at the same time.//ssc.stop(false)- 在StreamingContext 上使用stop函数,同事也会停止sparkContext,仅仅停止StreamingContext,在调用stopSparkContext设置参数为false
stop() on StreamingContext also stops the SparkContext. To stop only the StreamingContext, set the optional parameter of stop() called stopSparkContext to false.- 一个SparkContext 可以创建多个streamingContext和重用,只要在上一个StreamingContext停止前创建下一个StreamingContext
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 小案例的更多相关文章
- C# Socket通信 小案例
本文将编写2个控制台应用程序,一个是服务器端(server),一个是客户端(client), 通过server的监听,有新的client连接后,接收client发出的信息. server代码如下: u ...
- MVC 小案例 -- 信息管理
前几次更新博客都是每次周日晚上到周一,这次是周一晚上开始写,肯定也是有原因的!那就是我的 Tomact 忽然报错,无法启动,错误信息如下!同时我的 win10 也崩了,重启之后连 WIFI 的标志也不 ...
- Python:通过一个小案例深入理解IO多路复用
通过一个小案例深入理解IO多路复用 假如我们现在有这样一个普通的需求,写一个简单的爬虫来爬取校花网的主页 import requests import time start = time.time() ...
- 机械表小案例之transform的应用
这个小案例主要是对transform的应用. 时钟的3个表针分别是3个png图片,通过setInterval来让图片转动.时,分,秒的转动角度分别是30,6,6度. 首先,通过new Date函数获取 ...
- shell讲解-小案例
shell讲解-小案例 一.文件拷贝输出检查 下面测试文件拷贝是否正常,如果cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息.注意错误信息中basename $0打印脚本名.如 ...
- [jQuery学习系列六]6-jQuery实际操作小案例
前言最后在这里po上jQuery的几个小案例. Jquery例子1_占位符使用需求: 点击第一个按钮后 自动去check 后面是否有按钮没有选中, 如有则提示错误消息. <html> &l ...
- 02SpringMvc_springmvc快速入门小案例(XML版本)
这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC. 先给出整个项目的结构图:
- React.js入门小案例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- SqlDependency缓存数据库表小案例
SqlDependency的简介: SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决 ...
随机推荐
- (2)OLEDB数据库操作
1.首先要引入 System.Data.OracleClient.dll 2.引入命名空间 using System.Data.OleDb; OleDb类 https://msdn.microsof ...
- 注册表数据提取工具RegRipper
注册表数据提取工具RegRipper 注册表是Windows操作系统一个数据库,用来存储系统和应用程序设置信息.注册表信息分别保存在操作系统中的6个Hive文件中.获取这几个文件,就可以从中提取注 ...
- Count Primes -- LeetCodes (primality test)
Description: Count the number of prime numbers less than a non-negative number, n. 思路:这题第一种思路是写一个is_ ...
- [Git]Git 常用的操作命令
创建本地仓库 git init 获取远程仓库 git clone [url] 例如:git clone https://github.com/you/yourpro.git 创建远程仓库 添加一个新的 ...
- VMware报错“原因: 未能锁定文件”,打开失败
原文:http://jingyan.baidu.com/article/425e69e6bf64dbbe15fc16fe.html VMware打开复制的虚拟机,报错“原因: 未能锁定文件”,打开失败 ...
- eclipse和maven生成web项目的war包的操作方法
一.eclipse中,在需要打包的项目名上右击,然后把鼠标光标指向弹出框中的“run as”: 二.之后会看到在这个弹出框的右侧会出现一个悬浮窗,如下: 三.在上边的第二个悬浮窗鼠标点击“maven ...
- windows和linux之间上传下载文件的两种方法
一 用rz和sz http://blog.csdn.net/register_man/article/details/53860774 http://www.jb51.net/article/7 ...
- JAVA之继承的必要性
//说明继承的必要性package com.test; public class test { /** * @param args */ public static void m ...
- as well as
一.as well 用法: 1.as well常用作状语,作“又:也”解,相当于too或also,常位于句末,无须用逗号与句子分开.如: I am going to London and my sis ...
- 初步了解更新锁(U)与排它锁(X)
一直没有认真了解UPDATE操作的锁.近期在MSDN论坛上看到一个问题,询问堆表更新的死锁问题,问题非常easy,有相似这种表及数据: CREATE TABLE dbo.tb( c1 int, ...