3.2 测试

3.2.1 测试1: 打包到 Linux 测试

使用 maven 的打包命令打包. 然后测试

bin/spark-submit --class day01.WordCount --master yarn input/spark_test-1.0-SNAPSHOT.jar

3.2.2 测试2: idea 本地直接提交应用

使用 local 模式执行

相当于代码是在 window 下执行的.

package day01

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

object WordCount {
    def main(args: Array[String]): Unit = {
        // 1. 创建 SparkConf对象, 并设置 App名字, 并设置为 local 模式
        val conf: SparkConf = new SparkConf().setAppName("WordCount").setMaster("local[*]")
        // 2. 创建SparkContext对象
        val sc = new SparkContext(conf)
        // 3. 使用sc创建RDD并执行相应的transformation和action
        val wordAndCount: Array[(String, Int)] = sc.textFile(ClassLoader.getSystemResource("words.txt").getPath)
            .flatMap(_.split(" "))
            .map((_, 1))
            .reduceByKey(_ + _)
            .collect()
        wordAndCount.foreach(println)
        // 4. 关闭连接
        sc.stop()
    }
}
Copyright © 尚硅谷大数据 2019 all right reserved,powered by Gitbook
该文件最后修订时间: 2019-08-09 00:21:43

results matching ""

    No results matching ""