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

简介: Actionsreduce(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.这个方法会传入两个参数,计算这两个参数返回一个结果。

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.collectres6: 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.collectres6: 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.countres7: Long = 3scala> var data = sc.parallelize(List(("A",1),("B",1)))scala> data.countres8: 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.firstres9: (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 :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 :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 :21//保存为test_data_save文件scala> data.saveAsTextFile("test_data_save")scala> data.saveAsTextFile("test_data_save2",classOf[GzipCodec]):24: error: not found: type GzipCodec data.saveAsTextFile("test_data_save2",classOf[GzipCodec]) ^//引入必要的classscala> import org.apache.hadoop.io.compress.GzipCodecimport org.apache.hadoop.io.compress.GzipCodec//保存为压缩文件scala> data.saveAsTextFile("test_data_save2",classOf[GzipCodec])

查看文件

[xingoo@localhost bin]$ lldrwxrwxr-x. 2 xingoo xingoo 4096 Oct 10 23:07 test_data_savedrwxrwxr-x. 2 xingoo xingoo 4096 Oct 10 23:07 test_data_save2[xingoo@localhost bin]$ cd test_data_save2[xingoo@localhost test_data_save2]$ lltotal 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]$ lltotal 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 baefc

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 :22scala> data.saveAsSequenceFile("kv_test")[xingoo@localhost bin]$ cd kv_test/[xingoo@localhost kv_test]$ lltotal 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 :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 :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 :22//统计个数scala> data.countByKeyres9: 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 :22// 遍历scala> data.foreach(x=>println(x+" hello"))b helloa helloe hellof helloc hello

相关实践学习
简单用户画像分析
本场景主要介绍基于海量日志数据进行简单用户画像分析为背景,如何通过使用DataWorks完成数据采集 、加工数据、配置数据质量监控和数据可视化展现等任务。
SaaS 模式云数据仓库必修课
本课程由阿里云开发者社区和阿里云大数据团队共同出品,是SaaS模式云原生数据仓库领导者MaxCompute核心课程。本课程由阿里云资深产品和技术专家们从概念到方法,从场景到实践,体系化的将阿里巴巴飞天大数据平台10多年的经过验证的方法与实践深入浅出的讲给开发者们。帮助大数据开发者快速了解并掌握SaaS模式的云原生的数据仓库,助力开发者学习了解先进的技术栈,并能在实际业务中敏捷的进行大数据分析,赋能企业业务。 通过本课程可以了解SaaS模式云原生数据仓库领导者MaxCompute核心功能及典型适用场景,可应用MaxCompute实现数仓搭建,快速进行大数据分析。适合大数据工程师、大数据分析师 大量数据需要处理、存储和管理,需要搭建数据仓库?学它! 没有足够人员和经验来运维大数据平台,不想自建IDC买机器,需要免运维的大数据平台?会SQL就等于会大数据?学它! 想知道大数据用得对不对,想用更少的钱得到持续演进的数仓能力?获得极致弹性的计算资源和更好的性能,以及持续保护数据安全的生产环境?学它! 想要获得灵活的分析能力,快速洞察数据规律特征?想要兼得数据湖的灵活性与数据仓库的成长性?学它! 出品人:阿里云大数据产品及研发团队专家 产品 MaxCompute 官网 https://www.aliyun.com/product/odps 
目录
相关文章
|
14天前
|
分布式计算 DataWorks Java
DataWorks操作报错合集之在使用MaxCompute的Java SDK创建函数时,出现找不到文件资源的情况,是BUG吗
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
26 0
|
15天前
|
SQL 分布式计算 大数据
MaxCompute产品使用合集之要增加MaxCompute的自定义资源,该怎么操作
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
|
15天前
|
分布式计算 DataWorks 关系型数据库
MaxCompute产品使用合集之想要maxcompute任务通过事件触发,该怎么操作
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
|
14天前
|
分布式计算 DataWorks Oracle
DataWorks操作报错合集之DataWorks ODPS数据同步后,timesramp遇到时区问题,解决方法是什么
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
27 0
|
14天前
|
分布式计算 DataWorks 数据管理
DataWorks操作报错合集之DataWorks中udf开发完后,本地和在MaxCompute的工作区可以执行函数查询,但是在datawork里报错FAILED: ODPS-0130071:[2,5],是什么原因
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
28 0
|
14天前
|
分布式计算 DataWorks 数据库
DataWorks操作报错合集之DataWorks使用数据集成整库全增量同步oceanbase数据到odps的时候,遇到报错,该怎么处理
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
25 0
|
14天前
|
SQL 分布式计算 DataWorks
DataWorks操作报错合集之在DataWorks数据集成中,但是预览ODPS源数据库为空,是什么导致的
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
23 0
|
14天前
|
SQL 分布式计算 DataWorks
DataWorks操作报错合集之在DataWorks中使用ODPS SQL时遇到"该文件对应引擎实例已失效,请重新选择可用的引擎实例"的错误提示”,是什么导致的
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
34 0
|
15天前
|
分布式计算 DataWorks Java
DataWorks操作报错合集之dataworks 同步es数据到maxcompute 遇到报错:获取表列信息失败如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
23 0
|
15天前
|
SQL 分布式计算 DataWorks
MaxCompute产品使用合集之要查看MaxCompute Studio中的项目中的计算任务代码,我该怎么操作
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。

热门文章

最新文章