美文网首页
spark wordcount

spark wordcount

作者: 大龄程序员在帝都 | 来源:发表于2017-04-15 14:58 被阅读40次

    首先上传含有很多单词的txt文件:上传到hdfs上

    如下所示: Hamlet.txt是一个含有很多单词的文本文件,我们通过hdfs命令上传到hdfs中


    上传命令:

    hdfs dfs -put /root/ww/Hamlet.txt /user/root
    #说明
    hdfs dfs -put 本地文件 hdfs上路径
    

    上传以后通过spark执行:
    只有当result.count时,才会真正的执行,这是一个action。
    如下是对应的scala代码,在spark命令行中直接执行
    sc在初始化时spark自动设置的

    val  rdd = sc.textFile("/user/root/Hamlet.txt")
    val result = rdd.flatMap(line => line.split("\\s+")).map(word => (word,1)).reduceByKey(_ + _)
    result.count
    
    

    执行结果如下:

    Paste_Image.png

    在spark UI中可以看到对应的job

    Paste_Image.png Paste_Image.png

    相关文章

      网友评论

          本文标题:spark wordcount

          本文链接:https://www.haomeiwen.com/subject/fiwnattx.html