天天看點

flink學習——批處理wordcount和流處理wordcount

1.使用maven建構flink項目

本地需要java和scala,我已經裝好了。

我的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>org.example</groupId>
    <artifactId>flinktest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-scala_2.12</artifactId>
            <version>1.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-streaming-scala_2.12</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>
           

2.批處理:

import org.apache.flink.api.scala.ExecutionEnvironment
import org.apache.flink.api.scala._

object wc {
  def main(args: Array[String]): Unit = {
    val env = ExecutionEnvironment.getExecutionEnvironment
    val inputPath="C:\\Users\\caoji\\IdeaProjects\\flinktest\\src\\main\\resources\\cj.text"
    val inputDataSet=env.readTextFile(inputPath)
    val result=inputDataSet.flatMap(_.split(" ")).map((_,1)).groupBy(0).sum(1)
    result.print()
  }
}
           

在cj.text中寫一些内容,執行結果如下:

flink學習——批處理wordcount和流處理wordcount

3.流處理

因為要使用linux的nc -lk,在windows環境下可以使用自帶linux bash。我按照https://blog.csdn.net/rockyzhang1992/article/details/79648333這篇部落格的方法進行操作。

步驟1:配置并啟用自帶linux虛拟機

步驟2:安裝linux,我是在microsoft store下載下傳的ubuntu

flink學習——批處理wordcount和流處理wordcount

步驟3:在cmd輸入bash就可以進入bash界面了

繼續流處理wordcount:

import org.apache.flink.api.scala.ExecutionEnvironment
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.api.scala._

object StreamWC {
  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment

    val inputDataStream=env.socketTextStream("localhost",9999)
    val result=inputDataStream.flatMap(_.split(" ")).map((_,1)).keyBy(0).sum(1)

    result.print()

    env.execute()
  }
}
           

運作,觀察執行情況:

flink學習——批處理wordcount和流處理wordcount

完成

繼續閱讀