RDDs基本操作之Transformations
逐元素Transformation
map()
map()接收函数,把函数应用到RDD的每个元素,返回新的RDD
举例:
val lines = sc.parallelize(Array("hello", "spark", "hello", "world")
val lines2 = lines.map(word => (word,1))
打印出来
lines2.foreach(println)
hello,1
spark,1
hello,1
world,1
filter()
filter接收函数,返回只包含满足filter()函数的元素的新RDD
val lines3 = lines.filter(word=>word.contains("hello"))
lines3.foreach(println)
hello
hello
flatMap()
对每个输入元素,输出多个输出元素。
flat是压扁的意思,将RDD中元素压扁后返回一个新的RDD。
举例:
spark.txt文件中的内容

val inputs = sc.textFile("spark.txt")
val lines = inputs.flatMap(line => line.split(" "))
lines.foreach(print)
输入如下:
hello!helloworldhellospark
集合运算
RDDs支持数字集合的计算,例如并集,交集计算。
举例:
val rdd1 = sc.parallelize(Array("hello", "spark", "hello", "world")
val rdd2= sc.parallelize(Array("hello", "spark", "hi")
运算
1、去重
val rdd_distinct=rdd1.distinct()
2、并集
val rdd_union = rdd1.union(rdd2)
3、交集
val rdd_inter = rdd1.intersection(rdd2)
4、特别的
val rdd_sub=rdd1.subtract(rdd2)
RDDs基本操作之Transformations的更多相关文章
- RDDs基本操作、RDDs特性、KeyValue对RDDs、RDD依赖
摘要:RDD是Spark中极为重要的数据抽象,这里总结RDD的概念,基本操作Transformation(转换)与Action,RDDs的特性,KeyValue对RDDs的Transformation ...
- Spark快速入门 - Spark 1.6.0
Spark快速入门 - Spark 1.6.0 转载请注明出处:http://www.cnblogs.com/BYRans/ 快速入门(Quick Start) 本文简单介绍了Spark的使用方式.首 ...
- <Spark><Running on a Cluster>
Introduction 之前学习的时候都是通过使用spark-shell或者是在local模式运行spark 这边我们首先介绍Spark分布式应用的架构,然后讨论在分布式clusters中运行Spa ...
- [Spark]What's the difference between spark.sql.shuffle.partitions and spark.default.parallelism?
From the answer here, spark.sql.shuffle.partitions configures the number of partitions that are used ...
- 大数据入门第二十四天——SparkStreaming(一)入门与示例
一.概述 1.什么是spark streaming Spark Streaming is an extension of the core Spark API that enables scalabl ...
- Spark记录-官网学习配置篇(一)
参考http://spark.apache.org/docs/latest/configuration.html Spark提供三个位置来配置系统: Spark属性控制大多数应用程序参数,可以使用Sp ...
- Parallelism , Partitioner
转:spark通过合理设置spark.default.parallelism参数提高执行效率 spark中有partition的概念(和slice是同一个概念,在spark1.2中官网已经做出了说明) ...
- Spark Streaming原理简析
执行流程 数据的接收 StreamingContext实例化的时候,需要传入一个SparkContext,然后指定要连接的spark matser url,即连接一个spark engine,用于获得 ...
- <译>Spark Sreaming 编程指南
Spark Streaming 编程指南 Overview A Quick Example Basic Concepts Linking Initializing StreamingContext D ...
随机推荐
- 深度学习环境搭建部署(DeepLearning 神经网络)
工作环境 系统:Ubuntu LTS 显卡:GPU NVIDIA驱动:410.93 CUDA:10.0 Python:.x CUDA以及NVIDIA驱动安装,详见https://www.cnblogs ...
- Scala 系列(十二)—— 类型参数
一.泛型 Scala 支持类型参数化,使得我们能够编写泛型程序. 1.1 泛型类 Java 中使用 <> 符号来包含定义的类型参数,Scala 则使用 []. class Pair[T, ...
- Java生成二维码(Java程序都可以使用)
工具类,链接:https://pan.baidu.com/s/18U399fTH5wBJPnL97pAekg 提取码:bmw7 注:里面的corejar包是使用的zxing的代码,我只是将其导出的ja ...
- IDEA-Maven项目的jdk版本设置
在 Intellij IDEA 中,我们需要设置 Settings 中的 Java Compiler 和 Project Structure 中的 Language Level 中的 jdk 版本为自 ...
- [python]错误检测及异常处理try-except
1. 简介 要给代码添加错误检测及异常处理,只需要将其封装在try-except中. try:通常的代码 except:处理错误和异常的代码 2. 示例 import os try: path = ' ...
- ABC133F - Colorful Tree
ABC133FColorful Tree 题意 给定一颗边有颜色和权值的树,多次询问,每次询问,首先更改颜色为x的边的权值为y,然后输出u到v的距离. 数据都是1e5量级的. 思路 我自己一开始用树链 ...
- UVA10330拆点最大流
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #i ...
- CF1027D Mouse Hunt 思维
Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- codeforces 245 D. Restoring Table(位运算+思维)
题目链接:http://codeforces.com/contest/245/problem/D 题意:给出一个矩阵b,b[i][j]=a[i]&a[j],b[i][i]=-1.然后求a[i] ...
- kick start 2019 round D T2题解
题目大意:由N个房子围成一个环,G个人分别顺时针/逆时针在房子上走,一共走M分钟,每分钟结束,每个人顺/逆时针走到相邻的房子.对于每个房子都会记录最后时刻到达的人(可能是一群人).最终输出每个人会被几 ...