-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebd1616
commit eef0975
Showing
4 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/scala/com/flink/tutorials/api/transformations/FilterExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.flink.tutorials.api.transformations | ||
|
||
import org.apache.flink.api.common.functions.RichFilterFunction | ||
import org.apache.flink.streaming.api.scala._ | ||
|
||
|
||
object FilterExample { | ||
|
||
def main(args: Array[String]): Unit = { | ||
// 创建 Flink 执行环境 | ||
val senv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment | ||
|
||
val dataStream: DataStream[Int] = senv.fromElements(1, 2, -3, 0, 5, -9, 8) | ||
|
||
// 使用 => 构造Lambda表达式 | ||
val lambda = dataStream.filter ( input => input > 0 ) | ||
|
||
// 使用 _ 构造Lambda表达式 | ||
val lambda2 = dataStream.map { _ > 0 } | ||
|
||
// 继承RichFilterFunction | ||
// limit参数可以从外部传入 | ||
class MyFilterFunction(limit: Int) extends RichFilterFunction[Int] { | ||
|
||
override def filter(input: Int): Boolean = { | ||
if (input > limit) { | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
} | ||
|
||
val richFunctionDataStream = dataStream.filter(new MyFilterFunction(2)) | ||
richFunctionDataStream.print() | ||
|
||
senv.execute("basic filter transformation") | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/scala/com/flink/tutorials/api/transformations/FlatMapExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.flink.tutorials.api.transformations | ||
|
||
import org.apache.flink.streaming.api.scala._ | ||
|
||
object FlatMapExample { | ||
|
||
def main(args: Array[String]): Unit = { | ||
// 创建 Flink 执行环境 | ||
val senv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment | ||
|
||
val dataStream: DataStream[String] = senv.fromElements("Hello World", "Hello this is Flink") | ||
|
||
// split函数的输入为 "Hello World" 输出为 "Hello" 和 "World" 组成的列表 ["Hello", "World"] | ||
// flatMap将列表中每个元素提取出来 | ||
// 最后输出为 ["Hello", "World", "Hello", "this", "is", "Flink"] | ||
val words = dataStream.flatMap ( input => input.split(" ") ) | ||
|
||
val words2 = dataStream.map { _.split(" ") } | ||
|
||
// 只对字符串数量大于15的句子进行处理 | ||
val longSentenceWords = dataStream.flatMap { | ||
input => { | ||
if (input.size > 15) { | ||
input.split(" ") | ||
} else { | ||
Seq.empty | ||
} | ||
} | ||
} | ||
|
||
senv.execute("basic flatMap transformation") | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/scala/com/flink/tutorials/api/transformations/KeyByExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.flink.tutorials.api.transformations | ||
|
||
import org.apache.flink.streaming.api.scala._ | ||
|
||
object KeyByExample { | ||
|
||
def main(args: Array[String]): Unit = { | ||
// 创建 Flink 执行环境 | ||
val senv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment | ||
|
||
val dataStream: DataStream[(Int, Double)] = senv.fromElements((1, 1.0), (2, 3.2), (1, 5.5), (3, 10.0), (3, 12.5)) | ||
|
||
val keyedStream = dataStream.keyBy(0).sum(1) | ||
|
||
keyedStream.print() | ||
|
||
senv.execute("basic keyBy transformation") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters