2019独角兽企业重金招聘Python工程师标准>>>

map vs flatMap in Spark

September 24, 2014Big Dataexample, spark

In the previous blogs around Spark examples, RDD.flatMap() has been used. In this blog we will look at the differences between RDD.map() and RDD.flatMap().

map and flatMap are similar, in the sense they take a line from the input RDD and apply a function on it. The way they differ is that the function in map returns only one element, while function in flatMap can return a list of elements (0 or more) as an iterator.

Also, the output of the flatMap is flattened. Although the function in flatMap returns a list of elements, the flatMap returns an RDD which has all the elements from the list in a flat way (not a list).

Sounds a bit confusing. In the below code snippet, on the input lines both map and flatMap are applied and output dumped in HDFS to wordsWithMap and wordsWithFlatMap folder.

from pyspark import SparkContext sc = SparkContext("spark://bigdata-vm:7077", "Map") lines = sc.parallelize(["hello world", "hi"]) wordsWithMap = lines.map(lambda line: line.split(" ")).coalesce(1) wordsWithFlatMap = lines.flatMap(lambda line: line.split(" ")).coalesce(1) wordsWithMap.saveAsTextFile("hdfs://localhost:9000/user/bigdatavm/wordsWithMap") wordsWithFlatMap.saveAsTextFile("hdfs://localhost:9000/user/bigdatavm/wordsWithFlatMap")

1

2

3

4

5

6

7

8

9

10

from pyspark import SparkContext

sc = SparkContext("spark://bigdata-vm:7077", "Map")

lines = sc.parallelize(["hello world", "hi"])

wordsWithMap = lines.map(lambda line: line.split(" ")).coalesce(1)

wordsWithFlatMap = lines.flatMap(lambda line: line.split(" ")).coalesce(1)

wordsWithMap.saveAsTextFile("hdfs://localhost:9000/user/bigdatavm/wordsWithMap")

wordsWithFlatMap.saveAsTextFile("hdfs://localhost:9000/user/bigdatavm/wordsWithFlatMap")

The output of the map function in HDFS
The output of the flatMap function in HDFS

Conclusion

The input function to map returns a single element, while the flatMap returns a list of elements (0 or more). And also, the output of the flatMap is flattened.

In the case of word count, where the input line is split into multiple words, flatMap can be used. Also, in the case of weather data set, the extractData nethod will validate the record and might or might not return a value. In this case also, flatMap can be used.

Share this:

转载于:https://my.oschina.net/fayebrooke/blog/689731

map and flatmap 区别相关推荐

  1. spark RDD 的map与flatmap区别说明(转)

    转载前备注: 下面的实验全部在spark-shell中运行即可 #------------------------------------------------------------------- ...

  2. java8中的map与flatmap区别

    map:只能返回一个值 flatmap:返回多个值 new ArrayList().stream().map(x -> x);//返回一个 new ArrayList().stream().fl ...

  3. java8 flatmap与map_java8中map和flatMap区别

    1.函数定义比较 map注释: /** * Returns a stream consisting of the results of applying the given * function to ...

  4. map 和 flatMap 区别

    区别 这两个在本质上是一样的,都是 map 操作,即对流形式的传入数据进行处理返回一个数据.但是区别方面从字面上就可以体现出来,flatMap 比 map 多了一个 flat 操作,也就是 " ...

  5. Stream中map和flatmap的区别,一看就懂

    在日常开发中,我们经常会使用Stream来处理集合的操作. 其中,map是我们经常用到的api方法,同时呢,Stream也给我们提供了flatmap的方法. 这时候很多小伙伴就会搞不懂,这map和fl ...

  6. stream map方法_Java Stream中map和flatMap方法

    最近看到一篇讲stream语法的文章,学习Java中map()和flatMap()方法之间的区别. 虽然看起来这两种方法都做同样的事情,都是做的映射操作,但实际上差之毫厘谬以千里. 通过演示Demo中 ...

  7. 谈谈 Swift 中的 map 和 flatMap

    map 和 flatMap 是 Swift 中两个常用的函数,它们体现了 Swift 中很多的特性.对于简单的使用来说,它们的接口并不复杂,但它们内部的机制还是非常值得研究的,能够帮助我们够好的理解 ...

  8. Optional 中的 map 和 flatMap

    在面试的时候,面试官问了我一个问题.你使用过 Optional 吗?Optional 的原理是什么?我一听,这还不简单.Optional是一个枚举. public enum Optional<W ...

  9. RxJava 中的map与flatMap

    1.map和flatMap都是接受一个函数作为参数(Func1) 2.map函数只有一个参数,参数一般是Func1,Func1的<I,O>I,O模版分别为输入和输出值的类型,实现Func1 ...

最新文章

  1. Android各种报错问题汇总
  2. html after 只能应用于p标签吗,css after是什么意思?
  3. Map和hashmap
  4. GraphQL:简单开开始一个查询
  5. java jlabel里面加button_在Java Swing中通过JLabel覆盖JButton?
  6. 线性运算和非线性运算
  7. 初学者的React全家桶完整实例
  8. TensorFlow DCNN
  9. 【实践】网易云音乐推荐中用户行为序列深度建模.pdf(附下载链接)
  10. 使用TypeScript开发ReactNative应用的简单示例
  11. 十大排序算法——计数排序(C语言)
  12. 电商项目数据库设计 | 参考京东商城详细讲解商品数据库设计
  13. 在win7中chm打不开的多种解决方法
  14. jsoup模拟登陆合肥工业大学信息门户
  15. php上传禁止php_文件上传限制绕过技巧
  16. 软件测试证述职报告ppt,实验室检测员的述职报告ppt
  17. ant调用cmd命令行
  18. MySQL数据库入门学习教程(mysql基础+高级)
  19. 如何实现同一网络的计算机共享文件,同一个局域网内如何共享文件
  20. HBuilderX的介绍与语法提示

热门文章

  1. Boost:bimap双图property地图的测试程序
  2. Boost:bzip2解压缩器
  3. VTK:结构化网格之SGrid
  4. VTK:图片之CannyEdgeDetector
  5. OpenCV超分辨率基准测试
  6. 使用上下文属性将C ++对象嵌入QML
  7. C++和 C 的区别
  8. 1、MyEclipse插件配置以及通过MyEclipse生成表对应的JPA代码
  9. 2.关于QT中数据库操作,简单数据库连接操作,数据库的增删改查,QSqlTableModel和QTableView,事务操作,关于QItemDelegate 代理
  10. 3.mysql的中文问题,database级操作,表级操作,数据CRUD,分组操作,时间和日期,字符串相关函数,表的约束