文章目錄
1. Flink API的抽象級別分析
Flink中提供了4種不同層次的API:
- 低級API:提供了對時間和狀態的細粒度控制,簡潔性和易用性較差,主要應用在對一些複雜事件的處理邏輯上。
- 核心API:主要提供了針對流數據和離線數據的處理,對低級API進行了一些封裝,提供了filter、sum、max、min等高級函數,簡單且易用,所以在工作中應用比較廣泛。
- Table API:一般與DataSet或者DataStream緊密關聯,首先通過一個DataSet或DataStream創建出一個Table;然後用類似於filter、join或者select關係型轉化操作來轉化爲一個新的Table對象;最後將一個Table對象轉回一個DataSet或DataStream。與SQL不同的是,Table API的查詢不是一個指定的SQL字符串,而是調用指定的API方法
- SQL: Flink的SQL集成是基於Apache Calcite的,Apache Calcite實現了標準的SQL,使用起來比其他API更靈活,因爲可以直接使用SQL語句,Table API和SQL可以很容易地結合在一塊使用,它們都返回Table對象。
2. Flink DataStream常用API
DataStream API主要分爲3塊:DataSource、Transformation、Sink。
- DataSource是程序的數據源輸入,可以通過StreamExecutionEnvironment.addSource(sourceFunction)爲程序添加一個數據源。
- Transformation是具體的操作,它對一個或多個輸入數據源進行計算處理,比如Map、FlatMap和Filter等操作。
- Sink是程序的輸出,它可以把Tranfomation處理之後的數據輸出到指定的存儲介質中。
3. DataSource
Flink針對DataStream提供了大量的已經實現的DataSource(數據源)接口。
3.1 基於文件
readTextFile(path)
讀取文本文件文件遵循TextInputFormat逐行讀取規則並返回。
3.2 基於Socket
socketTextStream
從Socket中讀取數據,元素可以通過一個分隔符分開
3.3 基於集合
fromCollection(Collection)
通過Java的Collection集合創建一個數據流,集合中的所有元素必須是相同類型的
3.4 自定義輸入
addSource可以實現讀取第三方數據源的數據
4. 自定義數據源
4.1 自定義無並行度
可以通過實現SourceFunction接口來自定義無並行度(並行度爲1)的數據源
package streaming;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
/**
* 模擬產生從1開始遞增數字,每次遞增加1
* 自定義實現並行度爲1的Source
*/
public class MyNoParalleSource implements SourceFunction<Long> {
private long count = 1L;
private boolean isRunning = true;
/**
* 啓動一個Source
* 大部分情況下,都需要在這個run方法中實現一個循環
* 這樣就可以循環產生數據了
* @param sourceContext
* @throws Exception
*/
public void run(SourceContext<Long> sourceContext) throws Exception {
while(isRunning){
sourceContext.collect(count);
count++;
//每秒產生一條數據
Thread.sleep(1000);
}
}
/**
* 執行cancel操作時候會調用的方法
*/
public void cancel() {
isRunning = false;
}
}
4.2 自定義有並行度
通過實現ParallelSourceFunction接口或者繼承RichParallelSourceFunction來自定義有並行度的數據源
實現ParallelSourceFunction接口:
package streaming;
import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;
/**
* 自定義實現一個多並行度的Source
*/
public class MyParalleSource implements ParallelSourceFunction<Long> {
private long count = 1L;
private boolean isRunning = true;
/**
* 啓動一個Source
* 大部分情況下,都需要在這個run方法中實現一個循環,這樣就可以產生數據了
* @param sourceContext
* @throws Exception
*/
public void run(SourceContext<Long> sourceContext) throws Exception {
while(isRunning){
sourceContext.collect(count);
count++;
//每秒產生一條數據
Thread.sleep(1000);
}
}
/**
* 取消一個cancel的時候會調用的方法
*/
public void cancel() {
isRunning = false;
}
}
繼承RichParallelSourceFunction:
package streaming;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;
/**
* 自定義實現一個支持多並行度的source
* RichParallelSourceFunction會額外提供open和close方法
* 如果在source中獲取其他鏈接資源,那麼可以在open方法中打開資源鏈接,在close中關閉資源鏈接
*/
public class MyRichParalleSource extends RichParallelSourceFunction<Long> {
private long count = 1L;
private boolean isRunning = true;
/**
* 啓動一個Source
* 大部分情況下,都需要在這個run方法中實現一個循環,這樣就可以產生數據了
* @param sourceContext
* @throws Exception
*/
public void run(SourceContext<Long> sourceContext) throws Exception {
while(isRunning){
sourceContext.collect(count);
count++;
//每秒產生一條數據
Thread.sleep(1000);
}
}
/**
* 取消一個cancel的時候會調用的方法
*/
public void cancel() {
isRunning = false;
}
/**
* 這個方法只會在最開始的時候被調用一次
* 實現獲取鏈接的代碼
* @param parameters
* @throws Exception
*/
public void open(Configuration parameters) throws Exception{
System.out.println("open.......");
super.open(parameters);
}
/**
* 實現關閉鏈接的代碼
* @throws Exception
*/
public void close() throws Exception{
super.close();
}
}