Spark action算子案例
来源:互联网 发布:中航led控制软件 编辑:程序博客网 时间:2024/06/06 03:14
在上篇文章中,我们对Spark中几种常用的transformation算子通过Java和Scala两种代码分别进行了案例演示,Spark transformation算子案例
而在本文中,我们将继续对Spark的另一种RDD操作action进行讲解。对常用的action算子,使用Java和Scala两种代码进行简单的案例演示。
action常用算子介绍
Java版本
@SuppressWarnings("unused")public class ActionOperation { public static void main(String[] args) {// reduce();// collect();// count();// take();// saveAsTextFile(); countByKey(); } private static void reduce(){ // 创建SparkConf和JavaSparkContext SparkConf conf = new SparkConf() .setAppName("reduce") .setMaster("local"); JavaSparkContext sc = new JavaSparkContext(conf); // 有一个集合,里面有1到10,10个数字,现在要对10个数字进行累加 List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); JavaRDD<Integer> numbers = sc.parallelize(numberList); // 使用reduce操作对集合中的数字进行累加 // reduce操作的原理: // 首先将第一个和第二个元素,传入call()方法,进行计算,会获取一个结果,比如1 + 2 = 3 // 接着将该结果与下一个元素传入call()方法,进行计算,比如3 + 3 = 6 // 所以reduce操作的本质,就是聚合,将多个元素聚合成一个元素 int sum = numbers.reduce(new Function2<Integer, Integer, Integer>() { private static final long serialVersionUID = 1L; @Override public Integer call(Integer v1, Integer v2) throws Exception { return v1 + v2; } }); System.out.println(sum); // 关闭JavaSparkContext sc.close(); } private static void collect(){ // 创建SparkConf和JavaSparkContext SparkConf conf = new SparkConf() .setAppName("collect") .setMaster("local"); JavaSparkContext sc = new JavaSparkContext(conf); // 有一个集合,里面有1到10,10个数字,现在要对10个数字进行累加 List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); JavaRDD<Integer> numbers = sc.parallelize(numberList); // 使用map操作将集合中的所有数字乘以2 JavaRDD<Integer> doubleNumbers = numbers.map( new Function<Integer, Integer>() { private static final long serialVersionUID = 1L; @Override public Integer call(Integer v1) throws Exception { return v1 * 2; } }); // foreach action操作,是在远程集群上遍历RDD中的元素 // 而使用collect操作,将分布式在远程集群上的doubleNumbers RDD的数据拉取到本地 // 这种方式,一般不建议使用,因为如果rdd中的数据量比较大的话,比如超过1万条 // 那么性能会比较差,因为要从远程走大量的网路传输,将数据获取到本地 // 此外,除了性能差,还可能在rdd中数据量特别大的情况下,发生oom异常,内存溢出 // 因此,通常,还是推荐使用foreach action操作,来对最终的rdd元素进行处理 List<Integer> doubleNumberList = doubleNumbers.collect(); for(Integer num : doubleNumberList){ System.out.println(num); } // 关闭JavaSparkContext sc.close(); } private static void count(){ // 创建SparkConf和JavaSparkContext SparkConf conf = new SparkConf() .setAppName("count") .setMaster("local"); JavaSparkContext sc = new JavaSparkContext(conf); // 有一个集合,里面有1到10,10个数字,现在要对10个数字进行累加 List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); JavaRDD<Integer> numbers = sc.parallelize(numberList); // 对RDD使用count操作,统计RDD中有多少个元素 long count = numbers.count(); System.out.println(count); // 关闭JavaSparkContext sc.close(); } private static void take(){ // 创建SparkConf和JavaSparkContext SparkConf conf = new SparkConf() .setAppName("take") .setMaster("local"); JavaSparkContext sc = new JavaSparkContext(conf); // 有一个集合,里面有1到10,10个数字,现在要对10个数字进行累加 List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); JavaRDD<Integer> numbers = sc.parallelize(numberList); // take操作,与collect类似,也是从远程集群上,获取rdd的数据,拉取到本地 // 但是collect操作是获取rdd的所有数据,take只是获取n个数据 List<Integer> top3Numbers = numbers.take(3); for(Integer num : top3Numbers){ System.out.println(num); } // 关闭JavaSparkContext sc.close(); } private static void saveAsTextFile(){ // 创建SparkConf和JavaSparkContext SparkConf conf = new SparkConf() .setAppName("saveAsTextFile") .setMaster("local"); JavaSparkContext sc = new JavaSparkContext(conf); // 有一个集合,里面有1到10,10个数字,现在要对10个数字进行累加 List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); JavaRDD<Integer> numbers = sc.parallelize(numberList); // 使用map操作将集合中的所有数字乘以2 JavaRDD<Integer> doubleNumbers = numbers.map( new Function<Integer, Integer>() { private static final long serialVersionUID = 1L; @Override public Integer call(Integer v1) throws Exception { return v1 * 2; } }); // 直接将rdd中的数据,保存到HDFS文件中 // 但是要注意,我们这里只能指定文件夹,也就是目录 // 那么实际上,会保存为目录中的/double_number/part-00000文件 doubleNumbers.saveAsTextFile("hdfs://spark1:9000/double_number"); // 关闭JavaSparkContext sc.close(); } private static void countByKey(){ // 创建SparkConf和JavaSparkContext SparkConf conf = new SparkConf() .setAppName("countByKey") .setMaster("local"); JavaSparkContext sc = new JavaSparkContext(conf); // 模拟集合 @SuppressWarnings("unchecked") List<Tuple2<String, String>> studentList = Arrays.asList( new Tuple2<String, String>("class1", "leo"), new Tuple2<String, String>("class2", "jack"), new Tuple2<String, String>("class1", "marry"), new Tuple2<String, String>("class2", "tom"), new Tuple2<String, String>("class2", "david")); // 并行化创建集合,创建JavaPairRDD JavaPairRDD<String, String> students = sc.parallelizePairs(studentList); // 对RDD应用countByKey操作,统计每个班级的学生人数,也就是统计每个key对应的元素个数 // 这就是countByKey的作用 // countByKey返回的类型,直接就是Map<String, Object> Map<String, Object> studentCounts = students.countByKey(); for(Map.Entry<String, Object> studentCount : studentCounts.entrySet()){ System.out.println(studentCount.getKey() + ":" + studentCount.getValue()); } // 关闭JavaSparkContext sc.close(); }}
Scala版本
object ActionOperation { def main(args: Array[String]): Unit = {// reduce()// collect()// count()// take()// saveAsTextFile() countByKey() } def reduce(): Unit = { val conf = new SparkConf().setAppName("reduce").setMaster("local") val sc = new SparkContext(conf) val numberArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val numbers = sc.parallelize(numberArray, 1) val sum = numbers.reduce(_ + _) println(sum) } def collect(): Unit = { val conf = new SparkConf().setAppName("reduce").setMaster("local") val sc = new SparkContext(conf) val numberArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val numbers = sc.parallelize(numberArray, 1) val doubleNumbers = numbers.map { num => num * 2 } val doubleNumberArray = doubleNumbers.collect() for(num <- doubleNumberArray){ println(num) } } def count(): Unit = { val conf = new SparkConf().setAppName("reduce").setMaster("local") val sc = new SparkContext(conf) val numberArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val numbers = sc.parallelize(numberArray, 1) val count = numbers.count() println(count) } def take(): Unit = { val conf = new SparkConf().setAppName("reduce").setMaster("local") val sc = new SparkContext(conf) val numberArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val numbers = sc.parallelize(numberArray, 1) val top3Numbers = numbers.take(3) for(num <- top3Numbers){ println(num) } } def saveAsTextFile(): Unit = { val conf = new SparkConf().setAppName("reduce").setMaster("local") val sc = new SparkContext(conf) val numberArray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val numbers = sc.parallelize(numberArray, 1) val doubleNumbers = numbers.map { num => num * 2 } doubleNumbers.saveAsTextFile("hdfs://spark1:9000/double_number") } def countByKey(): Unit = { val conf = new SparkConf().setAppName("reduce").setMaster("local") val sc = new SparkContext(conf) val studentList = Array(new Tuple2("class1", "leo"), new Tuple2("class2", "jack"), new Tuple2("class1", "tom"), new Tuple2("class2", "jen"), new Tuple2("class2", "marry")) val students = sc.parallelize(studentList, 1) val studentCounts = students.countByKey() println(studentCounts) }}
阅读全文
0 0
- Spark action算子案例
- spark之action算子
- Spark transformation算子案例
- Spark算子-RDD Action(saveAsNewAPIHadoopFile)
- spark transformation和action算子
- Spark算子总结及案例
- 【spark】Spark transformation和action的算子
- Spark RDD使用详解5--Action算子
- Spark的Action算子的简单例子
- Spark Transformation和Action算子速查表
- Spark独到见解--Action算子总结
- Spark的Transform算子和Action算子列举和示例
- Spark RDD部分算子应用案例
- spark算子map reduce小案例
- Spark算子总结(带案例)
- spark笔记二之Spark程序模型Transformation算子与action算子
- 【Spark】RDD操作详解4——Action算子
- 【Spark】RDD操作详解4——Action算子
- JDK&变量&运算符与表达式
- 归一化函数 normalized()
- hibernate 关联关系(多对一、多对多、一对一)的配置
- Spring分模块开发
- Improving Deep Neural Networks Regularization 参考答案
- Spark action算子案例
- LeetCode: Palindrome Number
- C#基础学习第三篇:继承
- 【个人训练】(POJ1328)Radar Installation
- c提高1
- OpenJudge百炼-2809-计算2的N次方-C语言-高精度计算
- 可扩展对话框
- spring + shiro + cas 实现sso单点登录
- PAT乙级1069. 微博转发抽奖(20)