Spark实现WordCount案例

1.文本文件:

Preface
“The Forsyte Saga” was the title originally destined for that part of it which is called “The Man of Property”; 
and to adopt it for the collected chronicles of the Forsyte family has indulged the Forsytean tenacity that is in all of us. 
The word Saga might be objected to on the ground that it connotes the heroic and that there is little heroism in these pages.
 But it is used with a suitable irony; and, after all, this long tale, though it may deal with folk in frock coats, furbelows 

方式一:没用正则处理的

val file = spark.sparkContext.textFile("file:///F:\\dataset\\The_Man_of_Property.txt")
    file.flatMap(line=>line.split(" ")).map((_,1)).reduceByKey(_+_).sortBy(_._2,ascending = false)
      .take(10).foreach(println(_))

方式二:添加正则表达式

val p = "[0-9a-zA-Z]+".r
    file.flatMap(line => line.split(" ")).map(x=>(p.findAllIn(x).mkString(""),1)).reduceByKey(_+_)
    .take(10).foreach(println)

结果:
(welshed,1)
(mattered,1)
(someone,10)
(disregarded,2)
(jowl,2)
(bone,1)
(House,6)

方式三:利用sortBy()进行排序

sortBy(_._2,ascending = false) 
默认升序
ascending = false 代表:降序

val p = "[0-9a-zA-Z]+".r
    file.flatMap(line => line.split(" ")).map(x=>(p.findAllIn(x).mkString(""),1)).reduceByKey(_+_)
    .sortBy(_._2,ascending = false)
    .take(10).foreach(println)
结果:
(the,5168)
(of,3425)
(to,2810)
(and,2686)
(a,2564)             

Spark sql实现

导入隐式转换

 import spark.implicits._
//hdfs上文件
    val file = spark.sparkContext.textFile("/data/The_Man_of_Property.txt")
    val p = "[0-9a-zA-Z]+".r
    val dataFrame = file.flatMap(line=>line.split(" ")).map(x=>(p.findAllIn(x).mkString(""),1)).reduceByKey(_+_).toDF("word","count")

    dataFrame.createOrReplaceTempView("Property")


    dataFrame.show(5)
+-----------+-----+                                                             
|       word|count|
+-----------+-----+
|    welshed|    1|
|   mattered|    1|
|    someone|   10|
|disregarded|    2|
|       jowl|    2|
+-----------+-----+
only showing top 5 rows

createOrReplaceTempView使用

createOrReplaceTempView:创建临时视图,此视图的生命周期与用于创建此数据集的[SparkSession]相关联。
dataFrame.createOrReplaceTempView("Property")
    //指定列名
scala> val res = spark.sql("select * from Property ")
21/01/12 20:25:20 WARN metastore.ObjectStore: Failed to get database global_temp, returning NoSuchObjectException
res: org.apache.spark.sql.DataFrame = [word: string, count: int]

scala> res.show(5)
+-----------+-----+
|       word|count|
+-----------+-----+
|    welshed|    1|
|   mattered|    1|
|    someone|   10|
|disregarded|    2|
|       jowl|    2|
+-----------+-----+
only showing top 5 rows
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值