Spark集群上跑wordcount

版权声明:版权声明中 https://blog.csdn.net/lds_include/article/details/88976295

Spark集群上跑wordcount

1、创建WordCount程序的jar(maven项目)

编写SparkWC.scala文件

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

/**
  * scala:是一个强类型语言
  * 模板代码
  */
object SparkWC {

  def main(args: Array[String]): Unit = {
    //创建文件配置信息类对象
    val conf: SparkConf = new SparkConf()

    //指定应用程序名称
    conf.setAppName("SparkWC")

    //指定本地测试模式(local)中用几个线程来模式集群
    //local[2]:调用两个线程
    //loca:调用一个
    //loca[*]:调用系统所有空闲线程来模拟集群运行
    conf.setMaster("local[*]")

    //创建上下文对象(提交集群的入口类)
    val sc: SparkContext = new SparkContext(conf)

    //读取数据
    val liens: RDD[String] = sc.textFile("E:\\Test-workspace\\test\\input\\spark01\\新建文本文档.txt")

    //处理数据
    //将数据切分开来为一个个单词
    //flatmap:将多个元素压平为一块
    val words: RDD[String] = liens.flatMap(_.split(" "))

    //将单词后面跟上一个1,生成一个个对偶元组
    val paired: RDD[(String, Int)] = words.map((_, 1))

    //进行聚合操作
    val reduced: RDD[(String, Int)] = paired.reduceByKey(_+_)
    val res: RDD[(String, Int)] = reduced.sortBy(_._2, false)
    //println(res.collect().toBuffer)
    res.saveAsTextFile("E:\\Test-workspace\\test\\output\\spark01\\output")
    sc.stop()

  }
}

pom.xml中编写入口类

<!--maven打jar包(无论是scala还是java)时候指定主类的插件-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>basic.SparkWC</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

2、程序打包并且上传#### 打maven的jar包

  • 找maven管理工具
    在这里插入图片描述
  • 清除并且重新生成jar包
    在这里插入图片描述
  • 检查jar包是否生成
    在这里插入图片描述

上传jar包到集群上去

  • 上传jar包
    在这里插入图片描述

3、执行spark的job

执行spark的job

/usr/local/spark-1.6.1-bin-hadoop2.6/bin/spark-submit \
--class basic.SparkCount \
--master spark://min1:7077 \
--executor-memory 2G \
--total-executor-cores 4 \
/tempdataforhdfs_lds/spark-1.0-SNAPSHOT.jar \

3、查看执行结果

hadoop fs -cat /sparkWc/out

猜你喜欢

转载自blog.csdn.net/lds_include/article/details/88976295