Scala版算子总结(包括filter,sample等)【Scala版全代码】

package com.bjsxt

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext


object SuanZi {
  def main(args: Array[String]): Unit = {
    /**
     * 配置
     * conf
     */
    val conf=new SparkConf().setAppName("test").setMaster("local")
    /**
     * 上下文,传入conf
     */
    val sc=new SparkContext(conf)
    /**
     * sc.textFile
     * 传入文件
     */
    val textFile=sc.textFile("./words")
    /**
     * filter算子
     * 过滤
     */
    val filter=textFile.filter(ss=>{
      "hello HBase".equals(ss)
    })
    /**
     * 循环打印输出
     */
    filter.foreach(sd=>{
      println(sd)
    })
    /**
     * sample
     * 取样
     */
    val sample=textFile.sample(true,0.1, 100).foreach(println)
    val flatMap=textFile.flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_)
    flatMap.map(_.swap).sortByKey(true).map(_.swap).foreach(println)
  }
  
}

猜你喜欢

转载自blog.csdn.net/wyqwilliam/article/details/81149154
今日推荐