天天看點

(2)flink的wordCount

文章目錄

      • 基礎工程
        • 依賴
        • 打包插件
      • 代碼

<dependencies>
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-scala_2.11</artifactId>
        <version>1.7.0</version>
    </dependency>

    <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>
           

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

object WordCount {

  def main(args: Array[String]): Unit = {

    //1. 類似于spark中sparkContext 建構上下文環境
    val env = ExecutionEnvironment.getExecutionEnvironment
    //2 . source 抽取資料 dataSet
    val ds = env.readTextFile("D:\\test\\wc.txt")
    //3. transform 邏輯轉換
    // 其中flatMap 和Map 中  需要引入隐式轉換
    import org.apache.flink.api.scala.createTypeInformation
    //spark中的reduceByKey = flink中 groupBy(元組下标從0開始) + sum(元組下标)
    val rs = ds.flatMap(_.split(",")).map((_,1)).groupBy(0).sum(1)
    rs.print()
  }
}
           
Error:(14, 24) could not find implicit value for evidence parameter of type org.apache.flink.api.common.typeinfo.TypeInformation[String]
    val rs = ds.flatMap(_.split(",")).map((_,1)).groupBy(0).sum(1)