首页 > 其他 > 详细

Flink基础(二):快速上手

时间:2020-08-03 18:28:24      阅读:69      评论:0      收藏:0      [点我收藏+]

1 搭建 maven 工程 FlinkTutorial

1.1 pom 文件
技术分享图片
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.atguigu.flink</groupId>
 <artifactId>FlinkTutorial</artifactId>
 <version>1.0-SNAPSHOT</version>
 <dependencies>
 <dependency>
 <groupId>org.apache.flink</groupId>
 <artifactId>flink-scala_2.11</artifactId>
 <version>1.7.2</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-scala -->
 <dependency>
 <groupId>org.apache.flink</groupId>
 <artifactId>flink-streaming-scala_2.11</artifactId>
 <version>1.7.2</version>
 </dependency>
 </dependencies>
<build>
 <plugins>
 <!-- 该插件用于将 Scala 代码编译成 class 文件 -->
 <plugin>
 <groupId>net.alchim31.maven</groupId>
 <artifactId>scala-maven-plugin</artifactId>
 <version>3.4.6</version>
 <executions>
 <execution>
 <!-- 声明绑定到 maven 的 compile 阶段 -->
 <goals>
 <goal>testCompile</goal>
 </goals>
 </execution>
 </executions>
 </plugin>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-assembly-plugin</artifactId>
 <version>3.0.0</version>
 <configuration>
 <descriptorRefs>
 <descriptorRef>jar-with-dependencies</descriptorRef>
 </descriptorRefs>
 </configuration>
 <executions>
 <execution>
 <id>make-assembly</id>
 <phase>package</phase>
 <goals>
 <goal>single</goal>
 </goals>
 </execution>
 </executions>
 </plugin>
 </plugins>
</build>
</project>
View Code
1.2 添加 scala 框架 和 scala 文件夹
技术分享图片

2 批处理 wordcount

src/main/scala/com.atguigu.wc/WordCount.scala 
技术分享图片
object WordCount {
 def main(args: Array[String]): Unit = {
 // 创建执行环境
 val env = ExecutionEnvironment.getExecutionEnvironment
 // 从文件中读取数据
 val inputPath = "D:\\Projects\\BigData\\TestWC1\\src\\main\\resources\\hello.txt"
 val inputDS: DataSet[String] = env.readTextFile(inputPath)
 // 分词之后,对单词进行 groupby 分组,然后用 sum 进行聚合
 val wordCountDS: AggregateDataSet[(String, Int)] = inputDS.flatMap(_.split(" 
")).map((_, 1)).groupBy(0).sum(1)
 // 打印输出
 wordCountDS.print()
 } }
View Code
注意:Flink 程序支持 java 和 scala 两种语言,本课程中以 scala 语言为主。在引入包中,有 java 和 scala 两种包时注意要使用 scala 的包。 

3 流处理 wordcount 

src/main/scala/com.atguigu.wc/StreamWordCount.scala
技术分享图片
object StreamWordCount {
 def main(args: Array[String]): Unit = {
 // 从外部命令中获取参数
 val params: ParameterTool = ParameterTool.fromArgs(args)
 val host: String = params.get("host")
 val port: Int = params.getInt("port")
 // 创建流处理环境
 val env = StreamExecutionEnvironment.getExecutionEnvironment
 // 接收 socket 文本流
 val textDstream: DataStream[String] = env.socketTextStream(host, port)
 // flatMap 和 Map 需要引用的隐式转换
 import org.apache.flink.api.scala._
 val dataStream: DataStream[(String, Int)] = 
textDstream.flatMap(_.split("\\s")).filter(_.nonEmpty).map((_, 1)).keyBy(0).sum(1)
 dataStream.print().setParallelism(1)
 // 启动 executor,执行任务
 env.execute("Socket stream word count")
 } }
View Code
测试——在 linux 系统中用 netcat 命令进行发送测试。
nc -lk 7777

 

 

 

 

Flink基础(二):快速上手

原文:https://www.cnblogs.com/qiu-hua/p/13412841.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!