spark读取文件并返回特定字符总数

该博客详细介绍了如何利用Apache Spark从文件中读取数据,并计算出特定字符的总数。通过实例展示了Spark的DataFrame API和SQL操作,强调了其在大数据处理中的高效性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Spark

import chapter_05.void
import org.apache.avro.SchemaBuilder.array
import org.apache.commons.lang3.Functions.call
import org.apache.spark.api.java.function.VoidFunction
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Spark03_WordCount {



  def main(args: Array[String]): Unit ={

    //application
    //spark 框架
    //todo建立和spark框架的链接
    //JDBC

    val sparConf = new SparkConf().setMaster("local").setAppName("WordCount")
    val sc = new SparkContext(sparConf)

//一行一行的
    val lines: RDD[String] = sc.textFile(path = "Datas")
//一个一个的
    val words: RDD[String] = lines.flatMap(_.split(" "))
//单词合一
    val wordandone: RDD[(String, Int)] = words.map{(_, 1)}

    // shuffle分区内局部聚合,在全局聚合 shffle
    val reduced: RDD[(String, Int)] =  wordandone.reduceByKey((_+_))
    val sorted: RDD[(String, Int)] = reduced.sortBy(_._1, false)
    sorted.foreach(println)
    println("-------------------")
    val a = 0
    val array =  sorted.take(sorted.count().toInt).toArray
    for (a <- 0 until sorted.count().toInt){
      if (array.apply(a.toInt)._1.equals("GET")){
        print("GET COUNT IS "+ array.apply(a.toInt)._2)
      }
    }
    println("-------------------")
    var c = 0;
    for (a <- 0 until sorted.count().toInt){
      if (array.apply(a.toInt)._1.matches("Port.*")){
        println("--- "+ array.apply(a.toInt)._1)
        if (c < array.apply(a.toInt)._1.substring(5).toInt){
             c = array.apply(a.toInt)._1.substring(5).toInt
            println(c)
        }
      }
    }
    print("最大值"+c)
    //todo 关闭连接*/
    sc.stop()
  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悲喜物外

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值