Flink DataStream API Programming Guide学习&译文(未完待续)

本文介绍如何使用Apache Flink DataStream API进行流处理程序开发,包括数据源设置、窗口操作及常见转换方法,并通过WebSocket数据源实现WordCount示例。



DataStream programs in Flink are regular programs that implement transformations on data streams (e.g., filtering, updating state, defining windows, aggregating). The data streams are initially created from various sources (e.g., message queues, socket streams, files). Results are returned via sinks, which may for example write the data to files, or to standard output (for example the command line terminal). Flink programs run in a variety of contexts, standalone, or embedded in other programs. The execution can happen in a local JVM, or on clusters of many machines.(很简单一看就懂)


如下是一个基于Web Socket作为数据源的一个Stream的wordcount程序:

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time

object WindowWordCount {
  def main(args: Array[String]) {

    val env = StreamExecutionEnvironment.getExecutionEnvironment
    val text = env.socketTextStream("localhost", 9999)

    val counts = text.flatMap { _.toLowerCase.split("\\W+") filter { _.nonEmpty } }
      .map { (_, 1) }
      .keyBy(0)
      .timeWindow(Time.seconds(5))
      .sum(1)

    counts.print

    env.execute("Window Stream WordCount")
  }
}


 为了运行程序。你首先需要开启netcat工具,使用 nc -lk 9999开启,之后运行程序。然后在nc的终端输入单词既可看见Flink程序输出!

DataStream Transformations


Data transformation可以讲一个或者多个DataStream转成一个新的DataStream,同时可以组合使用多个transformation组成复杂的拓扑!


transformation算子描述可以参考:https://ci.apache.org/projects/flink/flink-docs-release-1.2/dev/datastream_api.html#datastream-transformations


对于元组,case class ,集合的提取进行匿名匹配是不支持的,例如:


val data: DataStream[(Int, String, Double)] = // [...]
data.map {
  case (id, name, temperature) => // [...]
}

匿名的模式匹配需要使用拓展的Scala API 参考: Scala API extension .


Physical partitioning


如果需要的话,Flink同时支持对DataStream进行算子转换的数据集进行底层的分区控制,使用如下函数:







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

javartisan

对您有帮助,欢迎老板赐一杯奶茶

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

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

打赏作者

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

抵扣说明:

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

余额充值