Pair RDD转化操作

val rdd = sc.parallelize(List((1,2),(3,4),(3,6)))

//reduceByKey,通过key来做合并
val r1 = rdd.reduceByKey((x,y)=>x+y).collect()
val r1 = rdd.reduceByKey(_+_).collect()
res0: Array[(Int, Int)] = Array((1,2), (3,10))

val r1 = rdd.reduceByKey((x,y)=>(x max y)).collect()
r1: Array[(Int, Int)] = Array((1,2), (3,6))

//groupByKey,通过key来做分组
val r2=rdd.groupByKey().collect()

r2: Array[(Int, Iterable[Int])] = Array((1,CompactBuffer(2)), (3,CompactBuffer(4, 6)))

//mapValues,对每个值应用函数,不改变键
val r3=rdd.mapValues(x => x+10 ).collect()

r3: Array[(Int, Int)] = Array((1,12), (3,14), (3,16))

//flatMapValues,对每个值应用一个返回迭代器函数,每个元素生成一个键值对
val r4=rdd.flatMapValues(x => x to 10 ).collect()

r4: Array[(Int, Int)] = Array((1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,10), (3,4), (3,5), (3,6), (3,7), (3,8), (3,9), (3,10), (3,6), (3,7), (3,8), (3,9), (3,10))

//keys,返回键
val r5=rdd.keys.collect()

//values,仅返回值
val r5=rdd.values.collect()

r5: Array[Int] = Array(2, 4, 6)

//sortByKey 排序后返回
scala> val r6=rdd.sortBy(x => x, false).collect()
r6: Array[(Int, Int)] = Array((3,6), (3,4), (1,2))

scala> val r6=rdd.sortBy(x => x, true).collect()
r6: Array[(Int, Int)] = Array((1,2), (3,4), (3,6))

需要注意的是,Pair RDD也是RDD,也可以使用RDD函数
比如
scala> val r7=rdd.filter{case (key,value)=>value<20}.collect()
r7: Array[(Int, Int)] = Array((1,2), (3,4), (3,6))

scala> val r7=rdd.filter{case (key,value)=>value<5}.collect()
r7: Array[(Int, Int)] = Array((1,2), (3,4))

scala> val r7=rdd.filter{case (k,v)=>v<20}.collect()
r7: Array[(Int, Int)] = Array((1,2), (3,4), (3,6))

统计单词重复的次数

方法1:
val input = sc.textFile("file:///usr/local/spark/README.md")
val words = input.flatMap(x => x.split(" "))
val count = words.countByValue()

方法2:用传统的map-reduce
//我们会发现x =>(x,1)是把每一个单值,转换成了一个数组,数组的值都是1,非常精妙
val count1 = words.map(x =>(x,1)).collect()
count1: Array[(String, Int)] = Array((#,1), (Apache,1), (Spark,1), ("",1), (Spark,1), (is,1), (a,1), (fast,1), (and,1), (general,1), (cluster,1), (computing,1), (system,1), (for,1), (Big,1), (Data.,1), (It,1), (provides,1), (high-level,1), (APIs,1), (in,1), (Scala,,1), (Java,,1), (Python,,1), (and,1), (R,,1), (and,1), (an,1), (optimized,1), (engine,1), (that,1), (supports,1), (general,1), (computation,1), (graphs,1), (for,1), (data,1), (analysis.,1), (It,1), (also,1), (supports,1), (a,1), (rich,1), (set,1), (of,1), (higher-level,1), (tools,1), (including,1), (Spark,1), (SQL,1), (for,1), (SQL,1), (and,1), (DataFrames,,1), (MLlib,1), (for,1), (machine,1), (learning,,1), (GraphX,1), (for,1), (graph,1), (processing,,1), (and,1), (Spark,1), (Streaming,1), (for,1), (stream,1), (processing.,1)...
//reduceByKey的作用是把上一步做的数组按照key来合并累加
val count2 = words.map(x =>(x,1)).reduceByKey((x,y)=>x+y).collect
res1: Array[(String, Int)] = Array((package,1), (this,1), (Version"](http://spark.apache.org/docs/latest/building-spark.html#specifying-the-hadoop-version),1), (Because,1), (Python,2), (page](http://spark.apache.org/documentation.html).,1), (cluster.,1), (its,1), ([run,1), (general,3), (have,1), (pre-built,1), (YARN,,1), ([http://spark.apache.org/developer-tools.html](the,1), (changed,1), (locally,2), (sc.parallelize(1,1), (only,1), (locally.,1), (several,1), (This,2), (basic,1), (Configuration,1), (learning,,1), (documentation,3), (first,1), (graph,1), (Hive,2), (info,1), (["Specifying,1), ("yarn",1), ([params]`.,1), ([project,1), (prefer,1), (SparkPi,2), (<http://spark.apache.org/>,1), (engine,1), (version,1), (file,1), (documentation,,1), (MASTER,1), (example,3), (["Parallel,1), (are...

//如果是统计单词数:
scala> val count1 = words.map(x =>(x,1))
count1: org.apache.spark.rdd.RDD[(String, Int)] = MapPartitionsRDD[55] at map at <console>:28

scala> count1.count
res3: Long = 568

Spark基础脚本入门实践3:Pair RDD开发的更多相关文章

  1. Spark基础脚本入门实践2:基础开发

    1.最基本的Map用法 val data = Array(1, 2, 3, 4, 5)val distData = sc.parallelize(data)val result = distData. ...

  2. Spark基础脚本入门实践1

    1.创建数据框架 Creating DataFrames val df = spark.read.json("file:///usr/local/spark/examples/src/mai ...

  3. 【原创 Hadoop&Spark 动手实践 5】Spark 基础入门,集群搭建以及Spark Shell

    Spark 基础入门,集群搭建以及Spark Shell 主要借助Spark基础的PPT,再加上实际的动手操作来加强概念的理解和实践. Spark 安装部署 理论已经了解的差不多了,接下来是实际动手实 ...

  4. Spark基础入门(01)—RDD

    1,基本概念 RDD(Resilient Distributed Dataset) :弹性分布式数据集 它是Spark中最基本的数据抽象,是编写Spark程序的基础.简单的来讲,一个Spark程序可以 ...

  5. spark Pair RDD 基础操作

    下面是Pair RDD的API讲解 转化操作 reduceByKey:合并具有相同键的值: groupByKey:对具有相同键的值进行分组: keys:返回一个仅包含键值的RDD: values:返回 ...

  6. spark中的pair rdd,看这一篇就够了

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是spark专题的第四篇文章,我们一起来看下Pair RDD. 定义 在之前的文章当中,我们已经熟悉了RDD的相关概念,也了解了RDD基 ...

  7. Spark 基础及RDD基本操作

    什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的集合.RDD具有数据 ...

  8. 0.Python 爬虫之Scrapy入门实践指南(Scrapy基础知识)

    目录 0.0.Scrapy基础 0.1.Scrapy 框架图 0.2.Scrapy主要包括了以下组件: 0.3.Scrapy简单示例如下: 0.4.Scrapy运行流程如下: 0.5.还有什么? 0. ...

  9. Spark学习之键值对(pair RDD)操作(3)

    Spark学习之键值对(pair RDD)操作(3) 1. 我们通常从一个RDD中提取某些字段(如代表事件时间.用户ID或者其他标识符的字段),并使用这些字段为pair RDD操作中的键. 2. 创建 ...

随机推荐

  1. vue 关键词模糊查询

    页面html,绑定的列表数据为datas,关键词为 select_words,如下图 其中d.accounts和d.roleName是需要进行搜索的字段,也可以进行大小写都可以

  2. Codeforces Round #508 (Div. 2)

    Codeforces Round #508 (Div. 2) http://codeforces.com/contest/1038 A #include<bits/stdc++.h> us ...

  3. [剑指Offer]38-字符串的全排列

    链接 https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7?tpId=13&tqId=11180&tPa ...

  4. MYSQL(Mariadb)

    CentOS7下安装MariaDB 添加 MariaDB yum 仓库(官网的,也可以直接用aliyun云的) vi /etc/yum.repos.d/MariaDB.repo [mariadb] n ...

  5. java_14 Date

    1.Date类的构造方法 Date是表示时间的类 空参构造 public Date() public class Demo { public static void main(String[] arg ...

  6. redis安全删key脚本(模糊匹配,长list,大set等)

    两种情况: 1.删除指定前缀开头的rediskey ,扫描和删除过程中对线上无感知 2.删除一个大的list,set,zset,hash,这种得分批次减少大小,一直缩到0再删 第一种情况:只要知道线上 ...

  7. linux就该这么学,第十天了

    今天老师主要让要考试的提前预习课程了,提前预习, 今天讲了,防火墙,iptable.firewall-config,firewall-cmd   防火墙和网卡的配置方法,四种,1配置文件方法,主要开启 ...

  8. zookeeper 服务挂掉重启后,dubbo 服务是不会自动重新注册上的

    今天遇到一个问题: 系统初始有两个dubbo 服务 , A 和 B , 都是正常注册到zookeeper 上的, 但是zookeeper 服务机房 断电导致 服务宕机, 那就重启吧. 一切正常. 但是 ...

  9. ABP框架系列之十六:(Dapper-Integration-Dapper集成)

    Introduction Dapper is an object-relational mapper (ORM) for .NET. Abp.Dapper package simply integra ...

  10. apache、nginx、tomcat配置方法

    https://www.cnblogs.com/chenmh/p/5121830.html