#常用Transformation(即转换,延迟加载)
#通过并行化scala集合创建RDD
val rdd1 = sc.parallelize(Array(1,2,3,4,5,6,7,8))
#查看该rdd的分区数量
rdd1.partitions.length
 
 
val rdd1 = sc.parallelize(List(5,6,4,7,3,8,2,9,1,10))
val rdd2 = sc.parallelize(List(5,6,4,7,3,8,2,9,1,10)).map(_*2).sortBy(x=>x,true)
val rdd3 = rdd2.filter(_>10)
val rdd2 = sc.parallelize(List(5,6,4,7,3,8,2,9,1,10)).map(_*2).sortBy(x=>x+"",true)
val rdd2 = sc.parallelize(List(5,6,4,7,3,8,2,9,1,10)).map(_*2).sortBy(x=>x.toString,true)
 
 
val rdd4 = sc.parallelize(Array("a b c", "d e f", "h i j"))
rdd4.flatMap(_.split(' ')).collect
 
val rdd5 = sc.parallelize(List(List("a b c", "a b b"),List("e f g", "a f g"), List("h i j", "a a b")))
 
 
List("a b c", "a b b") =List("a","b",))
 
 
 
 
 
 
rdd5.flatMap(_.flatMap(_.split(" "))).collect
=============================================================
#union求并集,注意类型要一致
val rdd6 = sc.parallelize(List(5,6,4,7))
val rdd7 = sc.parallelize(List(1,2,3,4))
 
val rdd8 = rdd6.union(rdd7)
//Array[Int] = Array(5, 6, 4, 7, 1, 2, 3, 4)
 
rdd8.distinct.sortBy(x=>x).collect
//Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
 
#intersection求交集
val rdd9 = rdd6.intersection(rdd7)
// Array[Int] = Array(4)
=============================================================
val rdd1 = sc.parallelize(List(("tom", 1), ("jerry", 2), ("kitty", 3)))
val rdd2 = sc.parallelize(List(("jerry", 9), ("tom", 8), ("shuke", 7)))
 
#join(内联)
val rdd3 = rdd1.join(rdd2)
//Array[(String, (Int, Int))] = Array((tom,(1,8)), (jerry,(2,9)))
 
 

#leftOuterJoin(左联)

 
val rdd3 = rdd1.leftOuterJoin(rdd2)
//Array[(String, (Int, Option[Int]))] = Array((tom,(1,Some(8))), (jerry,(2,Some(9))), (kitty,(3,None)))
 
 

#rightOuterJoin(右连)

 
val rdd3 = rdd1.rightOuterJoin(rdd2)
//Array[(String, (Option[Int], Int))] = Array((tom,(Some(1),8)), (jerry,(Some(2),9)), (shuke,(None,7)))
 
#groupByKey
val rdd3 = rdd1 union rdd2
// Array[(String, Int)] = Array((tom,1), (jerry,2), (kitty,3), (jerry,9), (tom,8), (shuke,7))
 
rdd3.groupByKey
// Array[(String, Iterable[Int])] = Array((tom,CompactBuffer(1, 8)), (jerry,CompactBuffer(2, 9)), (shuke,CompactBuffer(7)), (kitty,CompactBuffer(3)))
 
rdd3.groupByKey.map(x=>(x._1,x._2.sum))
//Array[(String, Int)] = Array((tom,9), (jerry,11), (shuke,7), (kitty,3))
 
#WordCount
sc.textFile("/root/words.txt").flatMap(x=>x.split(" ")).map((_,1)).reduceByKey(_+_).sortBy(_._2,false).collect
sc.textFile("/root/words.txt").flatMap(x=>x.split(" ")).map((_,1)).groupByKey.map(t=>(t._1, t._2.sum)).collect
// Array[(String, Int)] = Array((hello,5), (tom,2), (world,2), (myson,1), (ketty,1), (hell,1))
 
#cogroup
val rdd1 = sc.parallelize(List(("tom", 1), ("tom", 2), ("jerry", 3), ("kitty", 2)))
val rdd2 = sc.parallelize(List(("jerry", 2), ("tom", 1), ("shuke", 2)))
 
val rdd3 = rdd1.cogroup(rdd2)
//Array[(String, (Iterable[Int], Iterable[Int]))] = Array((tom,(CompactBuffer(1, 2),CompactBuffer(1))), (jerry,(CompactBuffer(3),CompactBuffer(2))), (shuke,(CompactBuffer(),CompactBuffer(2))), (kitty,(CompactBuffer(2),CompactBuffer())))
 
val rdd4 = rdd3.map(t=>(t._1, t._2._1.sum + t._2._2.sum))
// Array[(String, Int)] = Array((tom,4), (jerry,5), (shuke,2), (kitty,2))
 
#cartesian笛卡尔积
val rdd1 = sc.parallelize(List("tom", "jerry"))
val rdd2 = sc.parallelize(List("tom", "kitty", "shuke"))
val rdd3 = rdd1.cartesian(rdd2)
// Array[(String, String)] = Array((tom,tom), (tom,kitty), (tom,shuke), (jerry,tom), (jerry,kitty), (jerry,shuke))
 
###################################################################################################
 
#spark action
val rdd1 = sc.parallelize(List(1,2,3,4,5), 2)
 
#collect
rdd1.collect
// Array[Int] = Array(1, 2, 3, 4, 5)
 
#reduce
val rdd2 = rdd1.reduce(_+_)
//rdd2: Int = 15
 
#count
rdd1.count
//res7: Long = 5
 
#top(从大到小排序,然后去头部两个)
rdd1.top(2)
//res8: Array[Int] = Array(5, 4)
 
#take(取两个元素)
rdd1.take(2)
// Array[Int] = Array(1, 2)
 
 
#first(similer to take(1))
rdd1.first
// Int = 1
 
#takeOrdered
rdd1.takeOrdered(3)
//Array[Int] = Array(1, 2, 3)
#
 
map(func)                                                   Return a new distributed dataset formed by passing each element of the source through a function func.
filter(func)                                               Return a new dataset formed by selecting those elements of the source on which func returns true.
flatMap(func)                                               Similar to map, but each input item can be mapped to 0 or more output items (so func should return a Seq rather than a single item).
mapPartitions(func)                                           Similar to map, but runs separately on each partition (block) of the RDD, so func must be of type Iterator<T> => Iterator<U> when running on an RDD of type T.
mapPartitionsWithIndex(func)                               Similar to mapPartitions, but also provides func with an integer value representing the index of the partition, so func must be of type (Int, Iterator<T>) => Iterator<U> when running on an RDD of type T.
sample(withReplacement, fraction, seed)                    Sample a fraction fraction of the data, with or without replacement, using a given random number generator seed.
union(otherDataset)                                        Return a new dataset that contains the union of the elements in the source dataset and the argument.
intersection(otherDataset)                                Return a new RDD that contains the intersection of elements in the source dataset and the argument.
distinct([numTasks]))                                    Return a new dataset that contains the distinct elements of the source dataset.
groupByKey([numTasks])                                    When called on a dataset of (K, V) pairs, returns a dataset of (K, Iterable<V>) pairs. 
reduceByKey(func, [numTasks])                            When called on a dataset of (K, V) pairs, returns a dataset of (K, V) pairs where the values for each key are aggregated using the given reduce function func, which must be of type (V,V) => V. Like in groupByKey, the number of reduce tasks is configurable through an optional second argument.
aggregateByKey(zeroValue)(seqOp, combOp, [numTasks])    When called on a dataset of (K, V) pairs, returns a dataset of (K, U) pairs where the values for each key are aggregated using the given combine functions and a neutral "zero" value. Allows an aggregated value type that is different than the input value type, while avoiding unnecessary allocations. Like in groupByKey, the number of reduce tasks is configurable through an optional second argument.
sortByKey([ascending], [numTasks])                        When called on a dataset of (K, V) pairs where K implements Ordered, returns a dataset of (K, V) pairs sorted by keys in ascending or descending order, as specified in the boolean ascending argument.
join(otherDataset, [numTasks])                            When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (V, W)) pairs with all pairs of elements for each key. Outer joins are supported through leftOuterJoin, rightOuterJoin, and fullOuterJoin.
cogroup(otherDataset, [numTasks])                        When called on datasets of type (K, V) and (K, W), returns a dataset of (K, (Iterable<V>, Iterable<W>)) tuples. This operation is also called groupWith.
cartesian(otherDataset)                                    When called on datasets of types T and U, returns a dataset of (T, U) pairs (all pairs of elements).
pipe(command, [envVars])                                Pipe each partition of the RDD through a shell command, e.g. a Perl or bash script. RDD elements are written to the process's stdin and lines output to its stdout are returned as an RDD of strings.
coalesce(numPartitions)                                    Decrease the number of partitions in the RDD to numPartitions. Useful for running operations more efficiently after filtering down a large dataset.
repartition(numPartitions)                                Reshuffle the data in the RDD randomly to create either more or fewer partitions and balance it across them. This always shuffles all data over the network.
repartitionAndSortWithinPartitions(partitioner)            Repartition the RDD according to the given partitioner and, within each resulting partition, sort records by their keys. This is more efficient than calling repartition and then sorting within each partition because it can push the sorting down into the shuffle machinery.
 
 (K,(Iterable<V>,Iterable<W>))
 

Spark 初级算子的更多相关文章

  1. Spark RDD概念学习系列之Spark的算子的分类(十一)

    Spark的算子的分类 从大方向来说,Spark 算子大致可以分为以下两类: 1)Transformation 变换/转换算子:这种变换并不触发提交作业,完成作业中间过程处理. Transformat ...

  2. Spark RDD概念学习系列之Spark的算子的作用(十四)

    Spark的算子的作用 首先,关于spark算子的分类,详细见 http://www.cnblogs.com/zlslch/p/5723857.html 1.Transformation 变换/转换算 ...

  3. Spark操作算子本质-RDD的容错

    Spark操作算子本质-RDD的容错spark模式1.standalone master 资源调度 worker2.yarn resourcemanager 资源调度 nodemanager在一个集群 ...

  4. Spark RDD算子介绍

    Spark学习笔记总结 01. Spark基础 1. 介绍 Spark可以用于批处理.交互式查询(Spark SQL).实时流处理(Spark Streaming).机器学习(Spark MLlib) ...

  5. 列举spark所有算子

    一.RDD概述      1.什么是RDD           RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可 ...

  6. Spark常用算子-KeyValue数据类型的算子

    package com.test; import java.util.ArrayList; import java.util.List; import java.util.Map; import or ...

  7. Spark常用算子-value数据类型的算子

    package com.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; im ...

  8. spark常用算子总结

    算子分为value-transform, key-value-transform, action三种.f是输入给算子的函数,比如lambda x: x**2 常用算子: keys: 取pair rdd ...

  9. spark过滤算子+StringIndexer算子出发的一个逻辑bug

    问题描述: 在一段spark机器学习的程序中,同时用到了Filter算子和StringIndexer算子,其中StringIndexer在前,filter在后,并且filter是对stringinde ...

随机推荐

  1. java开发的web下载大数据时的异常处理

    同事用java开发了一个系统,其中有一个功能是下载大约10万笔数据到Excel中.当上线后,很多用户反映下载数据量大的时候就不能成功,但有时可以,所以结论就是系统不稳定,这个问题拖了很久没有解决. 在 ...

  2. position:fixed定位时 “高度坍塌” 问题的解决

    问题:对于固定定位的元素,固定住高度,后面紧跟的模块会当做前面的固定元素不存在似的,这给布局带来了困扰 解决方法: 1.给第二个模块div设置margin-top的值,margin-top的值设为大于 ...

  3. error BC31019 无法写入输出文件 未指定错误

    今天获取项目最后版本后,编译突然出现错误 error BC31019 无法写入输出文件 "xxx目录" 未指定错误 试着调整当前用户对这个文件的读写权限等各种方法,都未能解决该问题 ...

  4. DataSet ,DataTable,DataRow 之间的关系与使用

    关系   DataSet 包含多个DataTable,DataTable包含多行DataRow. 使用情况:   有时候GridView等控件需要将数据源动态绑定到DataSet中:将多个DataSe ...

  5. sealed 密封类,不能被其他类继承,但可以继承其他类

    public sealed class Person:继承类名 { }

  6. Fedora22没有i18n文件

    今天想修改Fedora22的系统语言,发现/etc/sysconfig/没有i18n 解决方法: dnf install system-config-language.noarch -y system ...

  7. 14--物理引擎Box2D

    物理模拟引擎专注于模拟现实世界中物体以及物体之间的基本运动规律.在游戏中引入物理引擎能提高游戏的真实性和可玩性,如<愤怒的小鸟>中小鸟弹出的抛物线运动.<割绳子>中割后的运动等 ...

  8. php 之 分页查询的使用方法及其类的封装

    一.分页的使用: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  9. IIS自定义404错误页显示“系统找不到指定的文件”解决方法

    在IIS站点属性里面设置了自定义的404错误页面为一个文件之后,有时一直不生效,总是提示这样一句话:“系统找不到指定的文件”. 其实这种错误也只是在某些网站程序中出现,其实解决办法很简单.这是由于II ...

  10. 利用css3-animation来制作逐帧动画

    前言 趁着还没有元旦之前先码一篇文章,不然到时候估计又被各种虐了,所以趁现在还有力气先来一篇.今天来聊聊css3中的动画属性animation,对这个属性懵懂是在很早的时候有前辈用这个 animati ...