従来の大規模データ処理、一般的に、バッチ式、すなわち、今日収集されたデータは、その後、明日私たちは、誰もが使用するために、今日を動作するようにデータを収集し、そのデータの多くの例では、適時ビジネスの成功のためには重要です。
FLINKスパーク共通し、オープンソースの大規模な処理エンジンです、目標はパフォーマンスを向上させるために持って来るためにデータ処理システム内のすべてをサポートすることです。どちらも、比較的成熟したエコシステムを持っています。ビッグデータエンジンの次の世代は、最も強力な競争相手です。
生態が全体的に、より満足のいくスパーク、機械学習での統合と使いやすさをリードしています。
FLINKは、ストリーム・コンピューティングにおける明らかな利点を有し、コアアーキテクチャモデルは、より徹底的かつ柔軟です。
この記事では、一例として、主プロセスストリームFLINKを解析し、ソースコードとしてストリーミング内部機構を導入します。
でDataStream全体的な概要
主な5つの部分は、のは、紹介させています。
1.動作環境StreamExecutionEnvironment
StreamExecutionEnvironmentストリーミング処理容器、抽象クラスであり、2つの実装クラス、すなわち、あります
LocalStreamEnvironment:
RemoteStreamEnvironment:
/ ** * StreamExecutionEnvironmentは、ストリーミングプログラムが実行されるコンテキストです。A * { @link LocalStreamEnvironment}は、現在のJVMで実行を引き起こすであろう * { @link RemoteStreamEnvironment}リモートセットアップの実行が発生します。 * * <P>環境は、並列度を設定するなど、ジョブの実行を制御するための方法を(提供します *またはフォールトトレランス/チェックポイントのパラメータ)と外の世界(データ・アクセス)と対話します。 * * @see org.apache.flink.streaming.api.environment.LocalStreamEnvironment * @see org.apache.flink.streaming.api.environment.RemoteStreamEnvironment * /
2.データ入力データソースのデータソース
これは、入力フォーマットが含まれているのInputFormat
/ ** *新しいデータソースを作成します。 * * @paramのコンテキストデータソースが実行されますする環境。 * @param データソースが実行する入力フォーマットのInputFormat。 * @paramはこの入力フォーマットによって生成要素のタイプ。 * / 公共のデータソース(ExecutionEnvironmentコンテキスト、のInputFormat <OUT、?>のInputFormat、種別情報<OUT> 種類、文字列dataSourceLocationName){ スーパー(コンテキスト、タイプ)。 この .dataSourceLocationName = dataSourceLocationName。 もし(のInputFormat == NULL ){ スロー 新しい( "入力フォーマットがNULLではないかもしれない。"、IllegalArgumentExceptionを)。 } この .inputFormat = のInputFormat。 もし(のInputFormat のinstanceof NonParallelInput){ この .parallelism = 1 。 } }
データソースとデータソースFLINK構築されたサードパーティのデータソースに分かれ、および内部データソースファイル、ネットワークポートとソケットタイプのデータセットされている、サードパーティのデータソースなどのコネクタ実用的なカフカコネクタを接続する方法、ESコネクタ、その定義それはsourceFunctionを実現することができ、コネクタをカプセル化します。
3.DataStream変換
でDataStream:同じタイプのストリーム要素は、変換、次の例により他でDataStreamでDataStreamに変換することができます。
@linkデータストリーム#マップ
@linkデータストリーム#フィルタ
StreamOperator:基本的なインターフェイスストリーミング演算子、3つの実装クラス
AbstractStreamOperator:
OneInputStreamOperator:
TwoInputStreamOperator:
/ ** *ストリーム事業者のための基本的なインターフェース。実装者は次のいずれかを実装します * { @link org.apache.flink.streaming.api.operators.OneInputStreamOperator}又は * {@link org.apache.flink.streaming.api.operators.TwoInputStreamOperator} to create operators * that process elements. * * <p>The class {@link org.apache.flink.streaming.api.operators.AbstractStreamOperator} * offers default implementation for the lifecycle and properties methods. * * <p>Methods of {@code StreamOperator} are guaranteed not to be called concurrently. Also, if using * the timer service, timer callbacks are also guaranteed not to be called concurrently with * methods on {@code StreamOperator}. * * @param <OUT> The output type of the operator */
4.DataStreamSink输出
/** * Adds the given sink to this DataStream. Only streams with sinks added * will be executed once the {@link StreamExecutionEnvironment#execute()} * method is called. * * @param sinkFunction * The object containing the sink's invoke function. * @return The closed DataStream. */ public DataStreamSink<T> addSink(SinkFunction<T> sinkFunction) { // read the output type of the input Transform to coax out errors about MissingTypeInfo transformation.getOutputType(); // configure the type if needed if (sinkFunction instanceof InputTypeConfigurable) { ((InputTypeConfigurable) sinkFunction).setInputType(getType(), getExecutionConfig()); } StreamSink<T> sinkOperator = new StreamSink<>(clean(sinkFunction)); DataStreamSink<T> sink = new DataStreamSink<>(this, sinkOperator); getExecutionEnvironment().addOperator(sink.getTransformation()); return sink; }
5.执行
/** * Executes the JobGraph of the on a mini cluster of ClusterUtil with a user * specified name. * * @param jobName * name of the job * @return The result of the job execution, containing elapsed time and accumulators. */ @Override public JobExecutionResult execute(String jobName) throws Exception { // transform the streaming program into a JobGraph StreamGraph streamGraph = getStreamGraph(); streamGraph.setJobName(jobName); JobGraph jobGraph = streamGraph.getJobGraph(); jobGraph.setAllowQueuedScheduling(true); Configuration configuration = new Configuration(); configuration.addAll(jobGraph.getJobConfiguration()); configuration.setString(TaskManagerOptions.MANAGED_MEMORY_SIZE, "0"); // add (and override) the settings with what the user defined configuration.addAll(this.configuration); if (!configuration.contains(RestOptions.BIND_PORT)) { configuration.setString(RestOptions.BIND_PORT, "0"); } int numSlotsPerTaskManager = configuration.getInteger(TaskManagerOptions.NUM_TASK_SLOTS, jobGraph.getMaximumParallelism()); MiniClusterConfiguration cfg = new MiniClusterConfiguration.Builder() .setConfiguration(configuration) .setNumSlotsPerTaskManager(numSlotsPerTaskManager) .build(); if (LOG.isInfoEnabled()) { LOG.info("Running job on local embedded Flink mini cluster"); } MiniCluster miniCluster = new MiniCluster(cfg); try { miniCluster.start(); configuration.setInteger(RestOptions.PORT, miniCluster.getRestAddress().get().getPort()); return miniCluster.executeJobBlocking(jobGraph); } finally { transformations.clear(); miniCluster.close(); } }
6.总结
Flink的执行方式类似于管道,它借鉴了数据库的一些执行原理,实现了自己独特的执行方式。
7.展望
Stream涉及的内容还包括Watermark,window等概念,因篇幅限制,这篇仅介绍flink DataStream API使用及原理。
下篇将介绍Watermark,下下篇是windows窗口计算。
参考资料
【1】https://baijiahao.baidu.com/s?id=1625545704285534730&wfr=spider&for=pc