Spark Streaming官方文档学习--上
Spark Streaming是spark api的扩展
# coding: utf-8# In[ ]:from pyspark import SparkContextfrom pyspark.streaming import StreamingContext# In[ ]:#创建两个工作线程,将这两个线程喂给StreamingContext,时间间隔是1秒#这里有个错误Cannot run multiple SparkContexts at once#参考:http://stackoverflow.com/questions/28259756/how-to-create-multiple-sparkcontexts-in-a-console#先要尝试关闭sc才能创建多个SparkContexttry:sc.stop()except:passsc = SparkContext("local[2]", "NetworkWordCount")ssc = StreamingContext(sc, 1)#sc.stop()# In[ ]:#创建一个DStream,从本机的这个端口取数据lines = ssc.socketTextStream("localhost", 9999)# In[ ]:#lines中的数据记录是一行行的文本,下面将文本切割成字words = lines.flatMap(lambda line: line.split(" "))#这里的flatMap是一对多DStream操作,生成一个新的DStream,就是变成了字流了# In[ ]:#下面数一数每一批次的字的个数# Count each word in each batchpairs = words.map(lambda word: (word, 1))wordCounts = pairs.reduceByKey(lambda x, y: x + y)# In[ ]:# 打印DStream中的每个RDD的前十个元素wordCounts.pprint()# In[ ]:ssc.start() # 开始计算ssc.awaitTermination() #等待计算停止# In[ ]:#将这个文件的py脚本提交计算: ./bin/spark-submit examples/src/main/python/streaming/network_wordcount.py localhost 9999#在命令行输入nc -lk 9999 然后模拟输入字符串文本,那么在pyspark命令行会打印出每秒钟输入的数据的统计结果
<dependency><groupId>org.apache.spark</groupId><artifactId>spark-streaming_2.11</artifactId><version>2.0.0</version></dependency>
from pyspark import SparkContextfrom pyspark.streaming import StreamingContextsc = SparkContext(master, appName)ssc = StreamingContext(sc, 1)
streamingContext.textFileStream(dataDirectory)
| map(fun) | 这个函数将输入的DStream的每一个元素传递给func得到一个新的DStream |
| flatMap(func) | 同上,只是每个输入可以map到多个输出项 |
| filter(func) | 选择func返回结果为true的DStream中的记录组成新的DStream |
| reparitition(numPartitions) | 通过改变划分去改变DStream的并行水平 |
| union(otherStream) | 合并 |
| count() | 返回一个新的DStream,是原始的DStream中的每个RDD的元素的数目 |
| reduce(func) | 使用函数func聚合原始数据汇总的每个RDD得到一个新的单一元素RDD组成的DStream |
| countByValue() | 调用类型K的DStream时候返回一个新的DStream有(K,long)对,其中long是k在每个RDD中出现的频率 |
| reduceByKey(func,[numTasks]) | 将(k,v)中的v按照k使用func进行聚合 |
| join(otherStream,[numTasks]) | (k,v)(k,w)得到(k,(v,w)) |
| cogroup(otherStream,[numTasks]) | (k,v)(k,w)得到(k,Seq[V],Seq[W]) |
| tansform(func) | 作用在每个RDD上得到新的RDD组成的DStream |
| updateStateByKey(func) | 每个键都是通过将给定的函数作用在其值上得到的新的DStream |
def updateFunction(newValues, runningCount):if runningCount is None:runningCount = 0return sum(newValues, runningCount) # add the new values with the previous running count to get the new count
runningCounts = pairs.updateStateByKey(updateFunction)
spamInfoRDD = sc.pickleFile(...) # RDD containing spam information# join data stream with spam information to do data cleaningcleanedDStream = wordCounts.transform(lambda rdd: rdd.join(spamInfoRDD).filter(...))
# Reduce last 30 seconds of data, every 10 secondswindowedWordCounts = pairs.reduceByKeyAndWindow(lambda x, y: x + y, lambda x, y: x - y, 30, 10)
| window(长度,间隔) | 原来的DStream按照新的指定窗口进行切分返回新的DStream |
| countByWindow(长度,间隔) | 返回滑动窗口的元素个数 |
| reduceByWindow(func, windowLength, slideInterval) |
读原来的DStream数据进行聚合得到新的DStream |
| reduceByKeyAndWindow(func,长度,间隔,[numtasks]) | (k,v)中的k被函数合并得到新的DStream |
| reduceByKeyAndWindow(func,invFunc,长度,间隔,[numtasks]) | 比上面的更高效,对窗口内的数据增量聚合和逐步移去得到聚合后新的DStream |
| countByValueAndWindow(windowLength, slideInterval, [numTasks]) | 根据窗口计算每个元素的频次 |
stream1 = ...stream2 = ...joinedStream = stream1.join(stream2)
windowedStream1 = stream1.window(20)windowedStream2 = stream2.window(60)joinedStream = windowedStream1.join(windowedStream2)
dataset = ... # some RDDwindowedStream = stream.window(20)joinedStream = windowedStream.transform(lambda rdd: rdd.join(dataset))
| print() |
前十个元素打印出来 |
| saveAsTextFiles(prefix, [suffix]) |
将DStream中的内容以文本方式保存成文件,每次批处理间隔内产生的文件按照prefix-TIME_IN_MS[.suffix]命名 |
| saveAsObjectFiles(prefix, [suffix]) |
将DStream中的内容按对象序列化并且以SequenceFile格式保存,每次批处理间隔文件按照上面的命名 |
| saveAsHadoopFiles(prefix, [suffix]) |
将DStream中的内容按对象序列化并且以hadoop格式保存,每次批处理间隔文件按照上面的命名 |
| foreachRDD(func) |
对每个RDD应用这个函数,将RDD保存在外部文件中 |
def sendRecord(rdd):connection = createNewConnection() # executed at the driverrdd.foreach(lambda record: connection.send(record))connection.close()dstream.foreachRDD(sendRecord)
def sendRecord(record):connection = createNewConnection()connection.send(record)connection.close()dstream.foreachRDD(lambda rdd: rdd.foreach(sendRecord))
def sendPartition(iter):connection = createNewConnection()for record in iter:connection.send(record)connection.close()dstream.foreachRDD(lambda rdd: rdd.foreachPartition(sendPartition))
def sendPartition(iter):# ConnectionPool is a static, lazily initialized pool of connectionsconnection = ConnectionPool.getConnection()for record in iter:connection.send(record)# return to the pool for future reuseConnectionPool.returnConnection(connection)dstream.foreachRDD(lambda rdd: rdd.foreachPartition(sendPartition))
Spark Streaming官方文档学习--上的更多相关文章
- Spark Streaming官方文档学习--下
Accumulators and Broadcast Variables 这些不能从checkpoint重新恢复 如果想启动检查点的时候使用这两个变量,就需要创建这写变量的懒惰的singleton实例 ...
- Spark监控官方文档学习笔记
任务的监控和使用 有几种方式监控spark应用:Web UI,指标和外部方法 Web接口 每个SparkContext都会启动一个web UI,默认是4040端口,用来展示一些信息: 一系列调度的st ...
- Spring 4 官方文档学习(十一)Web MVC 框架
介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...
- Spark SQL 官方文档-中文翻译
Spark SQL 官方文档-中文翻译 Spark版本:Spark 1.5.2 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 Data ...
- Spring 4 官方文档学习(十二)View技术
关键词:view technology.template.template engine.markup.内容较多,按需查用即可. 介绍 Thymeleaf Groovy Markup Template ...
- Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC
内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...
- Spring Data Commons 官方文档学习
Spring Data Commons 官方文档学习 -by LarryZeal Version 1.12.6.Release, 2017-07-27 为知笔记版本在这里,带格式. Table o ...
- Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图
接前面的Spring 4 官方文档学习(十一)Web MVC 框架,那篇太长,故另起一篇. 针对web应用的所有的MVC框架,都会提供一种呈现views的方式.Spring提供了view resolv ...
- Spring Boot 官方文档学习(一)入门及使用
个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念, ...
随机推荐
- linux设备驱动归纳总结(六):2.分享中断号【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-90837.html xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- asp显示多条记录的代码
asp显示多条记录的代码 仅供参考 <%for i=1 to RS.PageSize%> <% if RS.EOF then exit for end if %> <tr ...
- 记录整合sprinmvc+log4j的的过程
简介 由于进一步的学习以及便于自己更好的调试程序中遇到的错误,开始了将log4j整合到web项目中,项目是基于springmvc的,所以就做了一个springmvc和web项目的整合demo,本篇博客 ...
- java JPEGImageEncoder;图像处理
在Eclipse中处理图片,需要引入两个包: import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JP ...
- JavaScript和angularJs语法支持严格模式:”use strict”
如果给JavaScript和angularjs代码标志为“严格模式”,则其中运行的所有代码都必然是严格模式下的.其一:如果在语法检测时发现语法问题,则整个代码块失效,并导致一个语法异常.其二:如果在运 ...
- 关于easyUI在子页面增加显示tabs的一个问题
在父页面点个链接能动态看到子页面的情况太简单,请看easyUI官网:http://www.jeasyui.com/tutorial/layout/tabs2.php 现在说的是在子页面点个按钮也能触发 ...
- ant-环境变量
ant是绿色免安装的,通常习惯放在C盘根目录下,如:C:\apache-ant-1.9.4 那么环境就应该配置为: ANT_HOME=C:\apache-ant-1.9.4(注意:这里不需要加分号)P ...
- fg、bg、jobs、&、ctrl + z
原文地址:fg.bg.jobs.&.ctrl + z 作者:china-yuan http://blog.chinaunix.net/uid-22433093-id-1774026.html ...
- JAVA基础知识之网络编程——-使用MutilcastSocket实现多点广播
IP多点广播原理 设置一组特殊网络地址作为多点广播地址,每一个多点广播地址都被看作一个组,当客户需要发送和接受信息时,加入到该组即可. IP协议为多点广播提供了一批特殊的IP地址,范围是224.0.0 ...
- 2013 Asia Regional Changchun I 题,HDU(4821),Hash
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...