尚硅谷Flink教程學習筆記(批處理wordcount)

B站視頻wordcount
入門踩個坑,先把idea和scala的源碼關聯好才能通過ctrl+鼠標點擊進入對應源碼,否則只會有簡單的函數顯示
首先創建好自己的maven項目,然後在src文件下新建一個文件夾叫scala並且標記爲源文件目錄

然後是pom.xml文件的配置,加入下列依賴和plugins之後讓maven自己導入就行了

<?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>First_JAVA</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <build>
    
        <plugins>
        
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.4.6</version>
                <executions>
                    <execution>
                        <!--聲明綁定到maven的compile階段-->
                        <goals>
                            <goal>compile</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>
                        <!--聲明綁定到maven的compile階段-->
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            
        </plugins>
        
    </build>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-scala -->
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-scala_2.11</artifactId>
            <version>1.10.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.10.0</version>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-connector-kafka-0.11_2.11</artifactId>
            <version>1.10.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.bahir</groupId>
            <artifactId>flink-connector-redis_2.11</artifactId>
            <version>1.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-connector-elasticsearch6_2.11</artifactId>
            <version>1.10.0</version>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-statebackend-rocksdb_2.11</artifactId>
            <version>1.10.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-table-planner_2.11</artifactId>
            <version>1.10.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-table-planner-blink_2.11</artifactId>
            <version>1.10.0</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.flink</groupId>
            <artifactId>flink-csv</artifactId>
            <version>1.10.0</version>
        </dependency>

    </dependencies>


</project>

然後是wordcount.scala的代碼,放在新建的scala源文件目錄下

package com.atguigu.wc

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

// 批處理 word count
object WordCount_1 {
  def main(args: Array[String]): Unit = {
    // 創建一個批處理的執行環境
    val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment
    // 從文件中讀取數據
    val inputDataSet: DataSet[String] = env.readTextFile("D:\\MyCodeforIDEA\\JAVA\\First_JAVA\\src\\main\\resources\\word.txt")
    // 基於 DataSet做轉換,首先按空格分詞打散,然後按照word作爲key做group by
    val resultDataSet: AggregateDataSet[(String, Int)] = inputDataSet
      .flatMap(_.split(" "))    // 分詞得到所有word構成的數據集
      .map( (_, 1) )    // 轉換成一個二元組 (word, count),來一個單詞就給他附加一個1
      .groupBy(0)    // 以二元組中第一個元素作爲 key 分組
      .sum(1)    // 聚合二元組中第二個元素的值

    // 打印輸出
    resultDataSet.print()
  }
}

存儲在idea的resources下的word.txt文件,裏面存放的是以空格符分割的單詞字符串

hello world
hello flink
hello scala
how are you
fine thank you
and you

最後代碼的打印輸出是:

(fine,1)
(flink,1)
(world,1)
(thank,1)
(are,1)
(scala,1)
(you,3)
(and,1)
(hello,3)
(how,1)

視頻裏提到的一個問題

import org.apache.flink.api.scala.{AggregateDataSet, DataSet, ExecutionEnvironment}import org.apache.flink.api.scala._

用上面的import會報錯,而用下面這個不會報錯,錯誤如下:
Error:(11, 15) could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[String]
      .flatMap(_.split(" "))
大概意思是找不到隱藏值,在flink處理底層轉換的時候,所有的數據類型都要包裝成TypeInfomation,那麼就涉及到一個隱式轉換,那麼如果要引入這個隱式轉換,就要使用:
import org.apache.flink.api.scala._
因爲所有的隱式轉換就在這裏面

報錯圖
我遇到的一個問題就是老師在視頻講解的時候點進AggregateDataSet的源代碼的時候,和我的好像不太一樣?而且我的org.apache.flink.api.scala裏好像沒有scala這個Object類,就很奇怪,大家有遇到過嗎?是什麼問題呢?
我的圖:
在這裏插入圖片描述
老師的圖:
老師圖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章