1.批處理方式(攢一批再處理,看到的是處理後的最終結果)
import org.apache.flink.api.scala._
object BatchWordCount {
def main(args: Array[String]): Unit = {
//創建執行環境
val env = ExecutionEnvironment.getExecutionEnvironment
//從文件中獲取數據
val inpath="D:\\programs\\sparkPrograms\\FlinkProgarm\\src\\main\\resources\\hello.txt"
val inputDS: DataSet[String] = env.readTextFile(inpath)
val res = inputDS.flatMap(_.split(" "))
.map((_,1))
.groupBy(0)
.sum(1)
res.print()
}
}
2.流處理方式(來一個處理一個,每輸入一條數據,實時的看到變化)
使用socket流的方式輸入
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.api.scala._
object StreamWordCount {
def main(args: Array[String]): Unit = {
val env=StreamExecutionEnvironment.getExecutionEnvironment
val dataStream = env.socketTextStream("192.168.252.129",7777)
val wc = dataStream.flatMap(_.split(" "))
.filter(_.nonEmpty)
.map((_,1))
.keyBy(0)
.sum(1)
wc.print()
env.execute("wordcount")
}
}
運行:
1.先在linux系統上安裝netcat,它可以通過使用TCP或UDP協議的網絡連接讀寫數據
yum install -y nc
2.運行nc命令
nc -lk 7777
說明:
-l參數
用於指定nc將處於偵聽模式。指定該參數,則意味着nc被當作server,偵聽並接受連接,而非向其它地址發起連接。
-k<通信端口>
強制 nc 待命鏈接.當客戶端從服務端斷開連接後,過一段時間服務端也會停止監聽。 但通過選項 -k 我們可以強制服務器保持連接並繼續監聽端口。
指定端口7777
3.開啓flink程序後,在nc服務下輸入數據
在本地運行時,運行結果前面的數字1-4代表所在線程,flink程序中沒有設置線程數,所以線程數是當前系統的cpu核數
提交到flink集羣上時,該數字代表的是slot的編號
我們也可以在flink程序中設置線程數,如圖,
線程數設爲爲2,那麼結果線程只有1,2
錯誤記錄
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
解決方案:加入以下依賴
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
原因:
編譯時slf4j-api中public final class LoggerFactor類中private final static void bind()方法會尋找具體的日誌實現類綁定,主要通過StaticLoggerBinder.getSingleton()的語句調用。所以出現這個問題就是沒有加具體的日誌實現,只加了接口。
附:maven工程要導入的依賴和插件
<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>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</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>