Flatmap 和map 区别
map将函数作用到数据集的每一个元素上,生成一个新的分布式的数据集(RDD)返回
map函数的源码:
def map(self, f, preservesPartitioning=False):
"""
Return a new RDD by applying a function to each element of this RDD. >>> rdd = sc.parallelize(["b", "a", "c"])
>>> sorted(rdd.map(lambda x: (x, 1)).collect())
[('a', 1), ('b', 1), ('c', 1)]
"""
def func(_, iterator):
return map(fail_on_stopiteration(f), iterator)
return self.mapPartitionsWithIndex(func, preservesPartitioning)
map将每一条输入执行func操作并对应返回一个对象,形成一个新的rdd,如源码中的rdd.map(lambda x: (x, 1) --> [('a', 1), ('b', 1), ('c', 1)]
flatMap会先执行map的操作,再将所有对象合并为一个对象,返回值是一个Sequence
flatMap源码:
def flatMap(self, f, preservesPartitioning=False):
"""
>>> rdd = sc.parallelize([2, 3, 4])
>>> sorted(rdd.flatMap(lambda x: range(1, x)).collect())
[1, 1, 1, 2, 2, 3]
>>> sorted(rdd.flatMap(lambda x: [(x, x), (x, x)]).collect())
[(2, 2), (2, 2), (3, 3), (3, 3), (4, 4), (4, 4)]
"""
def func(s, iterator):
return chain.from_iterable(map(fail_on_stopiteration(f), iterator))
return self.mapPartitionsWithIndex(func, preservesPartitioning)
注意:flatMap将输入执行func操作时,对象必须是可迭代的
map与flatMap的区别:
1 from pyspark import SparkConf, SparkContext
2
3 conf = SparkConf()
4 sc = SparkContext(conf=conf)
5
6
7 def func_map():
8 data = ["hello world", "hello fly"]
9 data_rdd = sc.parallelize(data)
10 map_rdd = data_rdd.map(lambda s: s.split(" "))
11 print("map print:{}".format(map_rdd.collect()))
12
13
14 def func_flat_map():
15 data = ["hello world", "hello fly"]
16 data_rdd = sc.parallelize(data)
17 flat_rdd = data_rdd.flatMap(lambda s: s.split(" "))
18 print("flatMap print:{}".format(flat_rdd.collect()))
19
20
21 func_map()
22 func_flat_map()
23 sc.stop()
执行结果:
map print:[['hello', 'world'], ['hello', 'fly']]
flatMap print:['hello', 'world', 'hello', 'fly']
可以看出,map对 "hello world", "hello fly"这两个对象分别映射为['hello', 'world'], ['hello', 'fly'],而flatMap在map的基础上做了一个合并操作,将这两个对象合并为一个['hello', 'world', 'hello', 'fly'],这就造就了flatMap在词频统计方面的优势。
Flatmap 和map 区别的更多相关文章
- spark的flatMap和map区别
map()是将函数用于RDD中的每个元素,将返回值构成新的RDD. flatmap()是将函数应用于RDD中的每个元素,将返回的迭代器的所有内容构成新的RDD,这样就得到了一个由各列表中的元素组成的R ...
- Java流中的map算子和flatMap算子的区别
map算子和flatMap算子 map和flatMap都是映射(转换),那么他们之间究竟有什么区别呢? 1.我们先简单了解下map算子: @org.junit.Test public void tes ...
- $.each()、$.map()区别浅谈
遍历应该是各种语言中常会用到的操作了,实现的方法也很多,例如使用for.while等循环语句就可以很轻松的做到对数组或对象的遍历,今天想讲的不是它们,而是简单方便的遍历方法. 大致的整理了一下,经常用 ...
- list set map区别及适用场景
list与Set.Map区别及适用场景 1.List,Set都是继承自Collection接口,Map则不是 2.List特点:元素有放入顺序,元素可重复 ,Set特点:元素无放入顺序,元素不可重 ...
- java 常用集合list与Set、Map区别及适用场景总结
转载请备注出自于:http://blog.csdn.net/qq_22118507/article/details/51576319 list与Set.Map区别及 ...
- Set&Map区别Array
Set&Map区别Array 在Set内部,两个NaN是相等.两个对象总是不相等的.可以用length来检测 四个操作方法: add(value):添加某个值,返回Set结构本身. delet ...
- 一、基础篇--1.2Java集合-List、Set、Map区别
List.Set.Map区别 三者关系如下: 结构特点 1.List和Set是存储单列数据集合,Map是存储键值对这样的双列数据集合: 2.List中存储的数据都是有序的,并且允许重复:Map中存储 ...
- Spark入门1(以WordCount为例讲解flatmap和map之间的区别)
package com.test import org.apache.spark.{SparkConf, SparkContext} object WordCount { def main(args: ...
- 一眼看穿flatMap和map的区别
背景 map和flatmap,从字面意思或者官网介绍,可能会给一些人在理解上造成困扰[包括本人],所以今天专门花时间来分析,现整理如下: 首先做一下名词解释---------------------- ...
随机推荐
- go 序列化
序列化 package main import ( "encoding/json" "fmt" ) //结构体 type Monster struct { Na ...
- H5_0021:判断平台和微信
1,跳网站: <script>eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c ...
- 聊聊SNMP协议
注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 本文源链接:https://www.cnblogs.com/chloneda/p/snmp-protoco ...
- 在eclipse中JS页面创建后<%@ page此处就马上就报错
修改路径:右键点击创建的jsp页面--->Build Path--->Configure Build Path---> Libraries--->Add Libraries-- ...
- 简单记账本APP开发一
在对Android的一些基础的知识有了一定了解,以及对于AndroidStudio的如何使用有了 一定的熟悉后,决定做一个简单的记账本APP 开发流程 1.记账本的页面 2.可以添加新的账目 (一)页 ...
- 与第三方系统对接,生成Cloud出入库单据
案例: Cloud的采购订单同步到第三方系统,第三方系统入库后同步生成Cloud采购入库单. 解决方案:调用采购订单的下推API,先生成保存状态的采购入库单(采购入库单中的仓库是必填项,可以在采购订单 ...
- JS 百度地图路书---动态路线
JS 百度地图路书---动态路线 <!DOCTYPE html> <head> <meta http-equiv="Content-Type" con ...
- npm 基础命令
npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.npm 从5.2版开始,增加了 ...
- 归并排序 ALDS1_5_B:Merge Sort
Merge Sort Write a program of a Merge Sort algorithm implemented by the following pseudocode. You sh ...
- C语言 goto
C语言 goto 功能:无条件跳转.不推荐使用 案例 #include <stdio.h> int main() { // 函数跳转.循环跳转 // 创建标志位开始 // 无条件跳转到En ...