• Spark-WordCount


    words.txt 数据

    this is one line
    this is two line
    
    
    def main(args: Array[String]): Unit = {
        //创建SparkConf()并且设置App的名称
        val conf = new SparkConf()
        .setAppName("wordCount")
        .setMaster("local")  // 如果需要在集群运行需要注释掉setMaster,不然在集群里面就是单个节点运行.
    
        //创建SparkContext,该对象是提交spark app的入口
        val sc = new SparkContext(conf)
    
        //使用sc创建rdd,并且执行相应的transformation和action
        // sc.textFile("hdfs://master:9000/words.txt") //master主机上的 hdfs的 /words.txt文件
        sc.textFile("D:\words.txt") // 本地的 D:words.txt
        .flatMap(_.split(" ")) // 按照空格拆分每一行数据
        .map((_, 1)) // 将拆分的数据转换成 (word,1)的形式
        .reduceByKey(_ + _, 1) // 将相同的单词的value相加,并且设置为1个分区
        .sortBy(_._2, false) // 根据value进行 降序排序
        .foreach(println) // 打印输出
    
        //    停止sc,结束该任务
        sc.stop()
    }
    
    (this,2)
    (is,2)
    (line,2)
    (two,1)
    (one,1)
    
  • 相关阅读:
    9.24 Django Form组件
    9.21 form 和Ajax详解
    vue生命周期
    禁止输入框复制粘贴
    【转义字符】HTML 字符实体< &gt: &
    dispatch emit broadcast
    删除git项目
    新建github项目,邀请成员
    git常用命令行
    echarts用法
  • 原文地址:https://www.cnblogs.com/studyNotesSL/p/11367751.html
Copyright © 2020-2023  润新知