Flink学习17:算子介绍flatMap

1.flatmap核心包含两部操作:

1.将数据切分

2.拍扁

 

示例:

输出结果:
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment

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

    //create env
    val env = StreamExecutionEnvironment.getExecutionEnvironment

    //create ds
    val ds = env.fromElements("hadoop is good", "spark is fast", "flink is better")

    val flatMapedDs = ds.flatMap(_.split(" "))

    flatMapedDs.print()

    env.execute()


  }

}

输出结果:

2.自定义flatmap方法

核心操作:

1.继承FlatMapFunction类

2.重写flatMap方法

示例:

import org.apache.flink.api.common.functions.FlatMapFunction
import org.apache.flink.api.scala.createTypeInformation
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.util.Collector

object myFlatMapTest {

  //defined my flatMap func

  //judge the word length,bigger than special length then split,else do nothing
  class myFlatMap(maxWordLength:Int) extends FlatMapFunction[String, String]{
    override def flatMap(t: String, collector: Collector[String]): Unit = {

      if (t.length>maxWordLength){
        t.split(" ").foreach(collector.collect)
      }
    }
  }

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

    //create env
    val env = StreamExecutionEnvironment.getExecutionEnvironment

    //create ds
    val ds = env.fromElements("this is a good morning, long long long and long", "that day is friday")

//use my flatMap func

    val flatMapedDs = ds.flatMap(new myFlatMap(25))

    flatMapedDs.print()

    env.execute()
 

  }

}



输出结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值