Spark第一弹——实现WordCount的三种方式

Spark 专栏收录该内容
2 篇文章 0 订阅

方法一   Scala语言

分步进行

$scala>val rdd1 = sc.textFile("/home/centos/tmp/test.txt")
$scala>val rdd2 = rdd1.flatMap(line=>line.split(" "))
$scala>val rdd3 = rdd2.map(word = > (word,1))
$scala>val rdd4 = rdd3.reduceByKey(_ + _)
$scala>rdd4.collect

一步到位

sc.textFile("/home/centos/test.txt").flatMap(_.split(" ")).map((_,1)).reduceByKey(_ + _).collect

过滤单词

sc.textFile("/home/centos/test.txt").flatMap(_.split(" ")).filter(_.contains("wor")).map((_,1)).reduceByKey(_ + _).collect

方法2 Spark本地类库

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
		<project xmlns="http://maven.apache.org/POM/4.0.0"
				 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
				 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
			<modelVersion>4.0.0</modelVersion>

			<groupId>com</groupId>
			<artifactId>SparkDemo</artifactId>
			<version>1.0-SNAPSHOT</version>
			<dependencies>
				<dependency>
					<groupId>org.apache.spark</groupId>
					<artifactId>spark-core_2.11</artifactId>
					<version>2.1.0</version>
				</dependency>
			</dependencies>
		</project>

创建Scala.object

import org.apache.spark.{SparkConf, SparkContext}
object WordCount {
	def main(args: Array[String]): Unit = {
		//创建Spark配置对象
		val conf = new SparkConf();
		conf.setAppName("WordCountSpark")
		//设置master属性
		conf.setMaster("local") ;

		//通过conf创建sc
		val sc = new SparkContext(conf);

		//加载文本文件
		val rdd1 = sc.textFile("d:/scala/test.txt");
		//压扁
		val rdd2 = rdd1.flatMap(line => line.split(" ")) ;
		//映射w => (w,1)
		val rdd3 = rdd2.map((_,1))
		val rdd4 = rdd3.reduceByKey(_ + _)
		val r = rdd4.collect()
		r.foreach(println)
	}
}

方法3 JAVA语言

import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * java版
 */
public class WordCountJava2 {
    public static void main(String[] args) {
        //创建SparkConf对象
        SparkConf conf = new SparkConf();
        conf.setAppName("WordCountJava2");
        conf.setMaster("local");

        //创建java sc
        JavaSparkContext sc = new JavaSparkContext(conf);
        //加载文本文件
        JavaRDD<String> rdd1 = sc.textFile("F:\\BigData\\Scala\\scalaWorkspace\\FileTmp\\test.txt");

        //压扁
        JavaRDD<String> rdd2 = rdd1.flatMap(new FlatMapFunction<String, String>() {
            public Iterator<String> call(String s) throws Exception {
                List<String> list = new ArrayList<String>();
                String[] arr = s.split(" ");
                for(String ss :arr){
                    list.add(ss);
                }
                return list.iterator();
            }
        });

        //映射,word -> (word,1)
        JavaPairRDD<String,Integer> rdd3 = rdd2.mapToPair(new PairFunction<String, String, Integer>() {
            public Tuple2<String, Integer> call(String s) throws Exception {
                return new Tuple2<String, Integer>(s,1);
            }
        });

        //reduce化简
        JavaPairRDD<String,Integer> rdd4 = rdd3.reduceByKey(new Function2<Integer, Integer, Integer>() {
            public Integer call(Integer v1, Integer v2) throws Exception {
                return v1 + v2;
            }
        });

        //
        List<Tuple2<String,Integer>> list = rdd4.collect();
        for(Tuple2<String, Integer> t : list){
            System.out.println(t._1() + " : " + t._2());
        }
    }
}

 

  • 0
    点赞
  • 0
    评论
  • 7
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值