Spark学习(函数)


 
def main(args: Array[String]): Unit = {
   //匿名函数 () => Unit 是匿名函数类型 函数体() => println("I'm an anonymous function")
   val f1: () => Unit = () => println("I'm an anonymous function")
   //匿名函数
   val f2= (a:Int)=>{
    println(a)
   }
   var f3 :(Int)=>Unit = (x) => {println(x)} //与f2等效
   //f1()
   //f2(100)

  var theList1 = List(1,2,3,4,5,6,7,8,9)
  //Scala的泛型
  var theList2 = GetSort[Int](theList1,5,(x,y)=>x>y)
  var theList3 = GetSort[Int](theList1,5,MyCompare)
  theList2.foreach(print)
  println("===========")
  theList3.foreach(print)
}
def MyCompare(x:Int,y:Int):Boolean={
   x>y
}
//类似于泛型,函数参数
def GetSort[T](L1:List[T],MaxL:T, f1:(T,T)=>Boolean): List[T] ={
    var theRet : List[T] = List()
    for(x <- L1)
    {
        if(f1(MaxL,x))
          {
             theRet = x::theRet
          }
    }
    theRet
}
后记:不同的语言其实已经非常雷同,原因就是大家都在干类似的事情。
匿名函数和C#的类似,在调用的时候需要注意和C#的lamuda表达式。
Spark的count函数用于计算DataFrame或Dataset中的行数。它返回一个Long类型的值,表示行的数量。在使用count函数时,可以对数据进行去重操作,即使用count(distinct column)来计算某一列的唯一值数量。\[1\] 在Spark中,对于count(distinct)的优化,可以使用grouping sets原理。首先,通过expand操作将数据展开,然后以指定的列作为key进行HashAggregate操作,相当于进行了group by操作,从而实现了去重。接下来,可以直接计算count(id)、count(name)等,将数据分而治之,从一定程度上缓解了数据倾斜的问题。\[2\] 总结起来,Spark对count(distinct)的优化是通过展开数据并进行HashAggregate操作来实现去重,然后进行分组计算,从而提高计算效率。\[2\] #### 引用[.reference_title] - *1* *2* [Spark Sql之count(distinct)分析&&学习&&验证](https://blog.youkuaiyun.com/Lzx116/article/details/126153664)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Spark基础操作(一)](https://blog.youkuaiyun.com/weixin_44275820/article/details/119893892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值