当前位置: 首页 > 编程日记 > 正文

[大数据之Spark]——Actions算子操作入门实例

Actions

reduce(func)

Aggregate the elements of the dataset using a function func (which takes two arguments and returns one). The function should be commutative and associative so that it can be computed correctly in parallel.

这个方法会传入两个参数,计算这两个参数返回一个结果。返回的结果与下一个参数一起当做参数继续进行计算。

比如,计算一个数组的和。

//创建数据集
scala> var data = sc.parallelize(1 to 3,1)scala> data.collect
res6: Array[Int] = Array(1, 2, 3)//collect计算
scala> data.reduce((x,y)=>x+y)
res5: Int = 6

collect()

Return all the elements of the dataset as an array at the driver program. This is usually useful after a filter or other operation that returns a sufficiently small subset of the data.

返回数据集的所有元素,通常是在使用filter或者其他操作的时候,返回的数据量比较少时使用。

比如,显示刚刚定义的数据集内容。

//创建数据集
scala> var data = sc.parallelize(1 to 3,1)scala> data.collect
res6: Array[Int] = Array(1, 2, 3)

count()

Return the number of elements in the dataset.

计算数据集的数据个数,一般都是统计内部元素的个数。

//创建数据集
scala> var data = sc.parallelize(1 to 3,1)//统计个数
scala> data.count
res7: Long = 3scala> var data = sc.parallelize(List(("A",1),("B",1)))
scala> data.count
res8: Long = 2

first()

Return the first element of the dataset (similar to take(1)).

返回数据集的第一个元素,类似take(1)

//创建数据集
scala> var data = sc.parallelize(List(("A",1),("B",1)))//获取第一条元素
scala> data.first
res9: (String, Int) = (A,1)

take(n)

Return an array with the first n elements of the dataset.

返回数组的头n个元素

//创建数据集
scala> var data = sc.parallelize(List(("A",1),("B",1)))scala> data.take(1)
res10: Array[(String, Int)] = Array((A,1))//如果n大于总数,则会返回所有的数据
scala> data.take(8)
res12: Array[(String, Int)] = Array((A,1), (B,1))//如果n小于等于0,会返回空数组
scala> data.take(-1)
res13: Array[(String, Int)] = Array()scala> data.take(0)
res14: Array[(String, Int)] = Array()

takeSample(withReplacement, num, [seed])

Return an array with a random sample of num elements of the dataset, with or without replacement, optionally pre-specifying a random number generator seed.

这个方法与sample还是有一些不同的,主要表现在:

  • 返回具体个数的样本(第二个参数指定)
  • 直接返回array而不是RDD
  • 内部会将返回结果随机打散
//创建数据集
scala> var data = sc.parallelize(List(1,3,5,7))
data: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[0] at parallelize at <console>:21//随机2个数据
scala> data.takeSample(true,2,1)
res0: Array[Int] = Array(7, 1)//随机4个数据,注意随机的数据可能是重复的
scala> data.takeSample(true,4,1)
res1: Array[Int] = Array(7, 7, 3, 7)//第一个参数是是否重复
scala> data.takeSample(false,4,1)
res2: Array[Int] = Array(3, 5, 7, 1)scala> data.takeSample(false,5,1)
res3: Array[Int] = Array(3, 5, 7, 1)

takeOrdered(n, [ordering])

Return the first n elements of the RDD using either their natural order or a custom comparator.

基于内置的排序规则或者自定义的排序规则排序,返回前n个元素

//创建数据集
scala> var data = sc.parallelize(List("b","a","e","f","c"))
data: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[3] at parallelize at <console>:21//返回排序数据
scala> data.takeOrdered(3)
res4: Array[String] = Array(a, b, c)

saveAsTextFile(path)

Write the elements of the dataset as a text file (or set of text files) in a given directory in the local filesystem, HDFS or any other Hadoop-supported file system. Spark will call toString on each element to convert it to a line of text in the file.

将数据集作为文本文件保存到指定的文件系统、hdfs、或者hadoop支持的其他文件系统中。

//创建数据集
scala> var data = sc.parallelize(List("b","a","e","f","c"))
data: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[3] at parallelize at <console>:21//保存为test_data_save文件
scala> data.saveAsTextFile("test_data_save")scala> data.saveAsTextFile("test_data_save2",classOf[GzipCodec])
<console>:24: error: not found: type GzipCodecdata.saveAsTextFile("test_data_save2",classOf[GzipCodec])^
//引入必要的class
scala> import org.apache.hadoop.io.compress.GzipCodec
import org.apache.hadoop.io.compress.GzipCodec//保存为压缩文件
scala> data.saveAsTextFile("test_data_save2",classOf[GzipCodec])

查看文件

[xingoo@localhost bin]$ ll
drwxrwxr-x. 2 xingoo xingoo 4096 Oct 10 23:07 test_data_save
drwxrwxr-x. 2 xingoo xingoo 4096 Oct 10 23:07 test_data_save2
[xingoo@localhost bin]$ cd test_data_save2
[xingoo@localhost test_data_save2]$ ll
total 4
-rw-r--r--. 1 xingoo xingoo 30 Oct 10 23:07 part-00000.gz
-rw-r--r--. 1 xingoo xingoo  0 Oct 10 23:07 _SUCCESS
[xingoo@localhost test_data_save2]$ cd ..
[xingoo@localhost bin]$ cd test_data_save
[xingoo@localhost test_data_save]$ ll
total 4
-rw-r--r--. 1 xingoo xingoo 10 Oct 10 23:07 part-00000
-rw-r--r--. 1 xingoo xingoo  0 Oct 10 23:07 _SUCCESS
[xingoo@localhost test_data_save]$ cat part-00000 
b
a
e
f
c

saveAsSequenceFile(path)

Write the elements of the dataset as a Hadoop SequenceFile in a given path in the local filesystem, HDFS or any other Hadoop-supported file system. This is available on RDDs of key-value pairs that implement Hadoop's Writable interface. In Scala, it is also available on types that are implicitly convertible to Writable (Spark includes conversions for basic types like Int, Double, String, etc).

保存为sequence文件

scala> var data = sc.parallelize(List(("A",1),("A",2),("B",1)),3)
data: org.apache.spark.rdd.RDD[(String, Int)] = ParallelCollectionRDD[21] at parallelize at <console>:22scala> data.saveAsSequenceFile("kv_test")[xingoo@localhost bin]$ cd kv_test/
[xingoo@localhost kv_test]$ ll
total 12
-rw-r--r--. 1 xingoo xingoo 99 Oct 10 23:25 part-00000
-rw-r--r--. 1 xingoo xingoo 99 Oct 10 23:25 part-00001
-rw-r--r--. 1 xingoo xingoo 99 Oct 10 23:25 part-00002
-rw-r--r--. 1 xingoo xingoo  0 Oct 10 23:25 _SUCCESS

saveAsObjectFile(path)

Write the elements of the dataset in a simple format using Java serialization, which can then be loaded using SparkContext.objectFile().

基于Java序列化保存文件

scala> var data = sc.parallelize(List("a","b","c"))
data: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[16] at parallelize at <console>:22scala> data.saveAsObjectFile("str_test")scala> var data2 = sc.objectFile[Array[String]]("str_test")
data2: org.apache.spark.rdd.RDD[Array[String]] = MapPartitionsRDD[20] at objectFile at <console>:22scala> data2.collect

countByKey()

Only available on RDDs of type (K, V). Returns a hashmap of (K, Int) pairs with the count of each key.

统计KV中,相同K的V的个数

//创建数据集
scala> var data = sc.parallelize(List(("A",1),("A",2),("B",1)))
data: org.apache.spark.rdd.RDD[(String, Int)] = ParallelCollectionRDD[7] at parallelize at <console>:22//统计个数
scala> data.countByKey
res9: scala.collection.Map[String,Long] = Map(B -> 1, A -> 2)

foreach(func)

Run a function func on each element of the dataset. This is usually done for side effects such as updating an Accumulator or interacting with external storage systems.

Note: modifying variables other than Accumulators outside of the foreach() may result in undefined behavior. See Understanding closures for more details.

针对每个参数执行,通常在更新互斥或者与外部存储系统交互的时候使用

// 创建数据集
scala> var data = sc.parallelize(List("b","a","e","f","c"))
data: org.apache.spark.rdd.RDD[String] = ParallelCollectionRDD[10] at parallelize at <console>:22// 遍历
scala> data.foreach(x=>println(x+" hello"))
b hello
a hello
e hello
f hello
c hello

转载于:https://www.cnblogs.com/xing901022/p/5947706.html

相关文章:

Runloop总结

1.什么是Runloop Runloop字面上翻译就是运行循环&#xff0c;也就是一直在转圈圈运行着&#xff0c;类似于do…while()。我们思考一个问题&#xff1a; 一个线程执行完成后就会退出&#xff0c;当我们启动一个iOS APP时&#xff0c;系统会调用main.m文件的main()函数: int m…

Android拷贝工程不覆盖原工程的配置方法

http://www.2cto.com/kf/201203/125131.html 在Eclipse中改包名的时候选择refactor-->rename,勾选Rename subpackages,这样就不需要一个个修改每个类中导入的包名了转载于:https://www.cnblogs.com/leihupqrst/p/3670224.html

顺F速运国际版,你的密码漏点了

“ 对顺F旗下各APP顺藤摸瓜分析——顺F速运国际版。”前文《顺F速运&#xff0c;你被爱加M坑了》提到&#xff0c;顺F速运APP使用爱加密加壳&#xff0c;流量中传输内容被加密并BASE64编码了&#xff0c;只是安全性不够&#xff0c;壳没有将顺丰的加密算法及密钥保护好。秉承避…

利用Injection插件加快Xcode编译速度

我们在调试iOS原生代码时&#xff0c;每次修改都需要CommandR来重新编译运行。当项目代码量很大&#xff0c;编译时间就会很漫长。因此对于开发中来说&#xff0c;如果能加快编译速度&#xff0c;能大大提高生产效率。如果我们能像Swift Playground、小程序或网页那样修改代码后…

存储过程的优缺点 (转载)

为什么要用存储过程 几个去 IBM 面试的兄弟回来抱怨&#xff1a;去了好几个不同的 IBM 项目组&#xff0c;几乎每个面试官问到数据库的时候都要问用没用过存储过程&#xff0c;烦人不&#xff1f;大家去面的程序员&#xff0c;又不是 DBA&#xff0c;以前的项目都没有用到存储…

计算机知识的学习

我不是计算机科班出生&#xff01; 大学里喜欢跟医电系的人混在一起&#xff0c;受到他们影响较多&#xff0c;开始喜欢上计算机&#xff01;win 98 Office 97 他们有的擅长C、有的擅长Flash、还有哥们喜欢硬件&#xff01; 西安的东六路是图书批发市场&#xff0c;我几乎每周…

Wireshark小技巧:将IP显示为域名

“ 本文介绍如何使Wireshark报文窗口的Source栏及Destination内的IP直接显示为域名&#xff0c;提升报文分析效率。” 一个典型的Wireshark界面如下&#xff1a; 从这个图里&#xff0c;能看到源IP及目的IP&#xff0c;在流量不大&#xff0c;数据不多的情况下&#xff0c;我…

个人学习某个系统或平台的3问式的整理和细化指引

i:三问&#xff1a;是什么&#xff1f;为什么&#xff1f;怎么样&#xff1f; ii:详细化问题指引&#xff1a;是什么的目的在于确定系统的大致范围&#xff0c;明确目标&#xff1a;->平台的主要功能是什么&#xff1f;业务流程是怎样的&#xff1f;业务范围有多大&#xff…

给iOS开发者的React Native入门使用教程

目录一. 原生iOS项目集成React Native二. 原生跳转RN页面三. 显示豆瓣热门电影列表四. 改为导航五.完整源代码一. 原生iOS项目集成React Native 创建一个新的文件夹&#xff0c;如RNProject&#xff0c;然后新建一个/ios的子文件夹&#xff0c;将已有的iOS项目全部文件复制进去…

PHP Memcached应用实现代码

肖理达 (KrazyNio AT hotmail.com), 2006.04. 06, 转载请注明出处 一、memcached 简介 在很多场合&#xff0c;我们都会听到 memcached 这个名字&#xff0c;但很多同学只是听过&#xff0c;并没有用过或实际了解过&#xff0c;只知道它是一个很不错的东东。这里简单介绍一下&a…

顺F分享,你是在裸奔吗?

“ 对顺F旗下各APP顺藤摸瓜分析——顺F分享。”前文对顺F速运和顺F速运国际版进行了分析&#xff0c;二者使用同一套接口&#xff0c;虽然保护强度不高&#xff0c;但对代码和数据的保护却区别对待&#xff0c;实在让人诧异。秉承避免浪费的原则&#xff0c;我们将持续对顺F旗下…

静态链接库与动态链接库 (二)动态链接库的编译与使用

上一篇文章里大概描述linux下静态链接库的编译与使用&#xff0c;下面讲动态链接库的编译与使用方法。 1. 什么是动态链接库 所谓动态链接库&#xff0c;是指编译的时候不会把程序引用到的库插入到执行程序里&#xff0c;而是在执行时候才会去加载相关的库&#xff0c;所有用到…

【React Native】react-navigation导航使用方法

目录集成react-navigation使用react-navigation上一篇介绍了如何在已有iOS项目中集成React Native。这一篇我们把上一篇的demo做下拓展&#xff0c;添加点击电影跳转到详情页。页面跳转使用React Native推荐的第三方导航控件&#xff1a;react-navigation 集成react-navigatio…

请说明在.net中常用的几种页面间传递参数的方法,并说出他们的优缺点。

QueryString 传递一个或多个安全性要求不高或是结构简单的数值。但是对于传递数组或对象的话&#xff0c;就不能用这个方法了 session(viewstate) 简单&#xff0c;但易丢失 作用于用户个人,过量的存储会导致服务器内存资源的耗尽。 application 对象的作用范围是整个全局&am…

邮Z速递物流,让用户密码在网络中遨游

“ 最近分析快递行业的APP上瘾了&#xff0c;求解救。”邮政作为快递行业一个傻大黑的存在&#xff0c;一直很奇怪&#xff0c;我一直在纳闷&#xff0c;邮政和EMS到底是不是一家&#xff0c;在很多网点&#xff0c;它们是一体的存在&#xff0c;但很多东西&#xff0c;又是各自…

servlet response 中文乱码

先&#xff0c;response返回有两种&#xff0c;一种是字节流outputstream&#xff0c;一种是字符流printwrite。 申明&#xff1a;这里为了方便起见&#xff0c;所有输出都统一用UTF-8编码。 先说字节流&#xff0c;要输出“中国"&#xff0c;给输出流的必须是转换为utf-8…

【React Native】iOS原生导航跳转RN页面

上一篇介绍了React Native使用react-navigation进行导航跳转页面&#xff0c;现在我们介绍下原生iOS中怎么导航进一个新的React Native页面。 一、原生跳转React Native 创建HYReactNativeManager管理类. 在HYReactNativeManager.h中声明实现声明RCTBridgeDelegate协议&…

mac 常用指令

苹果公司生产的Mac搭载OS x 系统&#xff0c;OS x基于Unix&#xff0c;所以很多指令都和linux大同小异。 以下是一些常用指令&#xff0c;一点点自己记录下&#xff0c;方便自己和他人。这篇文应该是长期更新的。 1.ls [选项] [目录名] 第一个当然是list 指令 列出目录的文件列…

如何高效地爬取链家的房源信息(一)

“Python实现的链家网站的爬虫第一部分。”在之前的文章&#xff0c;以链家成都站为例&#xff0c;分析过链家网站数据的爬取&#xff0c;文章如下&#xff1a;干货&#xff01;链家二手房数据抓取及内容解析要点但是&#xff0c;当时没有根据分析&#xff0c;将爬取实现。本系…

HDU5886 Tower Defence 【两遍树形dp】【最长链预处理】

题意&#xff1a;N个点的一棵带权树。切掉某条边的价值为切后两树直径中的最大值。求各个边切掉后的价值和&#xff08;共N-1项&#xff09;。 解法一&#xff1a; 强行两遍dp&#xff0c;思路繁琐&#xff0c;维护东西较多&#xff1a; dis表示以i为根的子树的直径&#xff0c…

NPOI读取Excel数据应用

NPOI 是 POI 项目的 .NET 版本。使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写。NPOI是构建在POI 3.x版本之上的&#xff0c;它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。 需求&#xff1a;根据excel表格提供的SVN相…

pod setup慢的解决方法

最近使用pod setup更新CocoaPods本地检索库&#xff0c;无奈只有10几k&#xff0c;还中途报错。最终通过以下步骤&#xff0c;完成更新。 1.手动下载Specs检索库 执行pod setup后&#xff0c;实质是从github上clone检索库&#xff08;https://github.com/CocoaPods/Specs&…

如何高效地爬取链家的房源信息(二)

“Python实现的链家网站的爬虫第二部分。”本系列文将以链家南京站为例&#xff0c;使用Python实现链家二手房源信息的爬虫&#xff0c;将数据爬取&#xff0c;并存入数据库中&#xff0c;以便使用。本系列第一部分&#xff1a;如何高效地爬取链家的房源信息&#xff08;一&…

C#实现HttpPost提交文件

先建立一个WebApplication Web.config <?xml version"1.0" encoding"utf-8"?><configuration><system.web><!--<globalization requestEncoding"gb2312" responseEncoding"gb2312" fileEncoding"gb231…

16年10月18号2th运算符与流程结构

---恢复内容开始--- 2th: 一&#xff1a;运算符 算数运算符 - * / %取余 9%30 自增 --自减 关系运算符 < < > > 全等于 !不等于 逻辑运算符 & | &#xff01;非 ^异或 &&短路与 || 短路或 赋值…

通用的排序按钮

排序按钮&#xff0c;使用Core Graphic绘制&#xff0c;可以指定颜色、大小、字体等&#xff1a; 使用场景如下&#xff1a; 1.使用方法 下载demo代码。将HYRankView.h和HYRankView.m代码拖入工程。 然后使用如下代码&#xff0c;即可快速添加一个名称为价格的排序按钮 HYR…

如何高效地爬取链家的房源信息(三)

“Python实现的链家网站的爬虫第三部分。”本系列文将以链家南京站为例&#xff0c;使用Python实现链家二手房源信息的爬虫&#xff0c;将数据爬取&#xff0c;并存入数据库中&#xff0c;以便使用。本系列第一部分为基础&#xff1a;如何高效地爬取链家的房源信息&#xff08;…

Swift学习总结【持续更新】

1、 try、try?、try!的区别&#xff1a; try&#xff1a;需要用catch捕捉异常&#xff0c;如&#xff1a; do {let data try encoder.encode(item) try data.write(to: dataFilePath(), options: .atomic)} catch {print("Error encoding item array:\(error.localize…

svn清理失败且乱码 问题解决(转)

由于昨天在网络不好的状态下频繁尝试svn更新&#xff0c;导致今天svn更新时出现&#xff1a;清理失败且乱码的情况如下&#xff1a; 以下是解决方案&#xff1a;1.下载sqlite3.exe ,地址为&#xff1a;http://download.csdn.net/detail/whyzzj/63465292.在D盘建立文件夹 tools …

UI学习第二篇 (控件)

UIbutton 也是一个控件&#xff0c;它属于UIControl 用的最多的就是事件响应 1. //创建按钮对象 UIButton * _botton [UIButton buttonWithType:UIButtonTypeCustom]; //设置标题 [_botton setTitle:"按住说话" forstate:UIControlStateNormal]; [_botton setTitle:…