Spark开发单词统计程序

文章详细介绍了如何使用Scala和Java开发Spark的WordCount程序,包括环境配置、代码编写、任务提交等步骤,涉及SparkConf、SparkContext的使用,以及本地调试和打包上传到集群运行的过程。
摘要由CSDN通过智能技术生成

1)环境:

Scala 2.11.12

spark 2.4.3

java 1.8

添加依赖:

<dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.4.3</version>
            <scope>provided</scope>

</dependency>

2)用Scala开发 wordcount程序

代码如下:

package com.imooc.scala

import org.apache.spark.{SparkConf, SparkContext}

object WordCountScala {
  def main(args: Array[String]): Unit = {
    //第一步:创建SparkContext
    val conf = new SparkConf()
    conf.setAppName("WordCountScala")//设置任务名称
      //.setMaster("local")//local表示在本地执行
    val sc = new SparkContext(conf)
    //第二步:加载数据
    var path = "D:\\hello.txt"
    if(args.length==1){
      path=args(0);
    }
    val linesRDD = sc.textFile(path)
    //第三步:对数据进行切割,把一行数据切分成一个一个的单词
    val wordsRDD = linesRDD.flatMap(_.split(" "))
    //第四步:迭代words,将每个word转化为(word,1)这种形式
    val pairRDD = wordsRDD.map((_,1))
    //第五步:根据key(其实就是word)进行分组聚合统计
    val wordCountRDD = pairRDD.reduceByKey(_ + _)
    //第六步:将结果打印到控制台
    wordCountRDD.foreach(wordCount=>println(wordCount._1+"--"+wordCount._2))
    //第七步:停止SparkContext
    sc.stop()
  }

}

3)java 开发wordcount代码

package com.imooc.java;

import org.apache.spark.SparkConf;
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.*;
import scala.Tuple2;
import java.util.Arrays;
import java.util.Iterator;

/**
 * 单词计数
 */
public class WordCountJava {
    public static void main(String[] args) {

        //第一步:创建SparkContext:
//注意,针对java代码需要获取JavaSparkContext
        SparkConf conf = new SparkConf();
        conf.setAppName("WordCountJava");
                //.setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(conf);
//第二步:加载数据
        String path ="D:\\hello.txt";
        if(args.length==1){
            path=args[0];
        }
        JavaRDD<String> linesRDD = sc.textFile(path);
//第三步:对数据进行切割,把一行数据切分成一个一个的单词
//注意:FlatMapFunction的泛型,第一个参数表示输入数据类型,第二个表示是输出
        JavaRDD<String> wordRDD = linesRDD.flatMap(new FlatMapFunction<String,String>(){

        public Iterator<String> call(String line) throws Exception {
            return Arrays.asList(line.split(" ")).iterator();

        }
    });
//第四步:迭代words,将每个word转化为(word,1)这种形式
        JavaPairRDD<String,Integer> pairRDD = wordRDD.mapToPair(new PairFunction<String, String, Integer>()  {
            public Tuple2<String, Integer> call(String word) throws Exception {
                return new Tuple2<String, Integer>(word, 1);
            }
        });

//注意:PairFunction的泛型,第一个参数是输入数据类型
        JavaPairRDD<String, Integer> wordCountRDD = pairRDD.reduceByKey(new Function2<Integer, Integer, Integer>() {
            public Integer call(Integer i1, Integer i2) throws Exception {
                return i1 + i2;
            }
        });

//第六步:将结果打印到控制台
        wordCountRDD.foreach(new VoidFunction<Tuple2<String, Integer>>() {
            public void call(Tuple2<String, Integer> tup) throws Exception {
                System.out.println(tup._1+"--"+tup._2);
            }
        });
        //第七步:停止sparkContext
        sc.stop();

    }

}

4) 任务提交:

本地调试和 打包上传到集群中运行

添加打包依赖:

 <build>
    <plugins>
    <!-- java编译插件 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
    <!-- scala编译插件 -->
        <plugin>
            <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>3.1.6</version>
        <configuration>
            <scalaCompatVersion>2.11</scalaCompatVersion>
            <scalaVersion>2.11.12</scalaVersion>
        </configuration>
        <executions>
            <execution>
                <id>compile-scala</id>
                <phase>compile</phase>
                <goals>
                    <goal>add-source</goal>
                    <goal>compile</goal>
                </goals>
            </execution>
            <execution>
                <id>test-compile-scala</id>
                <phase>test-compile</phase>
                <goals>
                    <goal>add-source</goal>
                    <goal>testCompile</goal>
                </goals>
            </execution>
        </executions>
        </plugin>
        <!-- 打包插件 -->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass></mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11</artifactId>
            <version>2.4.3</version>
            <scope>provided</scope>

        </dependency>

    </dependencies>

打包上传之后写脚本执行:

vi wordCountJob.sh
spark-submit \
--class com.imooc.scala.WordCountScala \
--master yarn \
--deploy-mode client \
--executor-memory 1G \
--num-executors 1 \
db_spark-1.0-SNAPSHOT-jar-with-dependencies.jar \
hdfs://bigdata01:9000/test/hello.txt

到此就完成了spark程序的开发!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值