美文网首页Flink
flink03-入门demo

flink03-入门demo

作者: chen_666 | 来源:发表于2020-04-15 23:28 被阅读0次

    1.pom.xml

    <?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>flink</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.flink</groupId>
                <artifactId>flink-scala_2.11</artifactId>
                <version>1.7.0</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.0</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>compile</goal>
                        <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>
    

    2.批处理wordcount

    def main(args: Array[String]): Unit = {
    
      //构造执行环境
      val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment
      //读取文件
      val input = "file:///d:/temp/hello.txt"
      val ds: DataSet[String] = env.readTextFile(input)
      // 其中flatMap 和Map 中  需要引入隐式转换
      import org.apache.flink.api.scala.createTypeInformation
      //经过groupby进行分组,sum进行聚合
      val aggDs: AggregateDataSet[(String, Int)] = ds.flatMap(_.split(" ")).map((_, 1)).groupBy(0).sum(1)
      // 打印
      aggDs.print()
    
    }
    

    3.流处理

    import org.apache.flink.api.java.utils.ParameterTool
    import org.apache.flink.streaming.api.scala.{DataStream, StreamExecutionEnvironment}
    
    object StreamWcApp {
    
      def main(args: Array[String]): Unit = {
        //从外部命令中获取参数
        val tool: ParameterTool = ParameterTool.fromArgs(args)
        val host: String = tool.get("host")
        val port: Int = tool.get("port").toInt
    
        //创建流处理环境
        val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
        //接收socket文本流
        val textDstream: DataStream[String] = env.socketTextStream(host,port)
       // flatMap和Map需要引用的隐式转换
        import org.apache.flink.api.scala._
       //处理 分组并且sum聚合
        val dStream: DataStream[(String, Int)] = textDstream.flatMap(_.split(" ")).filter(_.nonEmpty).map((_,1)).keyBy(0).sum(1)
       //打印
        dStream.print()
        
        env.execute()
      }
    

    相关文章

      网友评论

        本文标题:flink03-入门demo

        本文链接:https://www.haomeiwen.com/subject/qvbkvhtx.html