zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    马拦过河卒
    最小生成树 kruskal算法
    链表,关键是结构体的快排
    Shortest Prefixes 字典树
    串的匹配
    A Beautiful Meadow
    Tiling
    邻接矩阵的宽度遍历
    邻接矩阵的深度遍历
    Form1.frm
  • 原文地址:https://www.cnblogs.com/studyNotesSL/p/11367751.html
Copyright © 2011-2022 走看看