主頁 >  其他 > Flink原始碼漫游指南<二>:全能管家StreamExecutionEnviornment

Flink原始碼漫游指南<二>:全能管家StreamExecutionEnviornment

2021-11-08 09:42:54 其他

StreamExecutionEnvironment是stream program執行的環境,其子類LocalStreamEnvironment會讓程式在當前JJVM中執行,而子類RemoteStreamEnvironment會讓程式在遠程集群中執行, ——Flink官方注釋

首先,我們來看一看一個典型的Flink中的wordcount程式是什么樣的,

public class WordCount {

    public static void main(String[] args) throws Exception {
        //定義socket的埠號
        int port;
        try{
            ParameterTool parameterTool = ParameterTool.fromArgs(args);
            port = parameterTool.getInt("port");
        }catch (Exception e){
            System.err.println("沒有指定port引數,使用默認值9000");
            port = 9000;
        }

        //?獲取運行環境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //?連接socket獲取輸入的資料
        DataStreamSource<String> text = env.socketTextStream("10.192.12.106", port, "\n");

        //計算資料
        DataStream<WordWithCount> windowCount = text.flatMap(new FlatMapFunction<String, WordWithCount>() {
            public void flatMap(String value, Collector<WordWithCount> out) throws Exception {
                String[] splits = value.split("\\s");
                for (String word:splits) {
                    out.collect(new WordWithCount(word,1L));
                }
            }
        })//打平操作,把每行的單詞轉為<word,count>型別的資料
                .keyBy("word")//針對相同的word資料進行分組
                .timeWindow(Time.seconds(2),Time.seconds(1))//指定計算資料的視窗大小和滑動視窗大小
                .sum("count");
               
        //把資料列印到控制臺
        windowCount.print()
                .setParallelism(1);//使用一個并行度
        //?因為flink是懶加載的,所以必須呼叫execute方法,上面的代碼才會執行
        env.execute("streaming word count");

    }

請注意看代碼中?標記部分,可見我們Flink程式的第一步就是獲得一個StreamExecutionEnviorment,然后通過SEE提供的方法添加source來獲得第一個DataStream,最后提供SEE提供的execution( )方法執行程式,在這里SEE有三個作用,

在Flink官方注釋中說道:“StreamExecutionEnviorment會提供方法來控制job的執行(比如設定并行度、容錯和檢查點方面的引數)以及與外界交換資料”,那么,SEE類具體是如何實作的呢,本文將從Flink原始碼出發,一步步學習SEE的組成,

從下面的代碼中,我們首先看看SEE有哪些屬性(英文已做翻譯)

/** The default name to use for a streaming job if no other name has been specified.
 * 默認JOB名稱 */
public static final String DEFAULT_JOB_NAME = "Flink Streaming Job";

/** The time characteristic that is used if none other is set.
 * 默認使用的時間語意:處理時間*/
private static final TimeCharacteristic DEFAULT_TIME_CHARACTERISTIC = TimeCharacteristic.ProcessingTime;

/** The default buffer timeout (max delay of records in the network stack).
 * 默認快取延時(資料在網路堆疊中的最大延時) */
private static final long DEFAULT_NETWORK_BUFFER_TIMEOUT = 100L;

/**
 * The environment of the context (local by default, cluster if invoked through command line).
 * context的enviorment (默認是local,如果是命令列提交則是cluster模式)
 */
private static StreamExecutionEnvironmentFactory contextEnvironmentFactory;

/** The default parallelism used when creating a local environment.
 * 創建local enviornment的默認并行度,為當前虛擬機可以獲得的最大可用cpu數量 */
private static int defaultLocalParallelism = Runtime.getRuntime().availableProcessors();


/** ?The execution configuration for this environment.  執行config*/
private final ExecutionConfig config = new ExecutionConfig();

/** ?Settings that control the checkpointing behavior. */
private final CheckpointConfig checkpointCfg = new CheckpointConfig();

/** job中transformation的鏈表集合 */
protected final List<StreamTransformation<?>> transformations = new ArrayList<>();

private long bufferTimeout = DEFAULT_NETWORK_BUFFER_TIMEOUT;

/** 是否可以鏈接chain */
protected boolean isChainingEnabled = true;

/** The state backend used for storing k/v state and state snapshots. 
  * 存盤kv狀態和狀態快照用的狀態后端 */
private StateBackend defaultStateBackend;

/** The time characteristic used by the data streams. datastream使用的時間語意 */
private TimeCharacteristic timeCharacteristic = DEFAULT_TIME_CHARACTERISTIC;

/** 下面的cacheFile是給taskmanager用的分布式快取 */
protected final List<Tuple2<String, DistributedCache.DistributedCacheEntry>> cacheFile = new ArrayList<>();

我們可以看到,除了一些常規的屬性,還有兩個重要屬性:ExecutionConfigCheckpointConfig (已由?標記出來)

SEE的屬性組成大概可以歸納成下圖

我們再來分別看看這兩個屬性里可以定義哪些引數,首先是ExecutionConfig

private static final long serialVersionUID = 1L;

/**
 * The constant to use for the parallelism, if the system should use the number
 * of currently available slots. 如果系統需要用當前可以slot數,使用這個常量用于設定
 */
@Deprecated
public static final int PARALLELISM_AUTO_MAX = Integer.MAX_VALUE;

/**
 * The flag value indicating use of the default parallelism. This value can
 * be used to reset the parallelism back to the default state.
 * 指明是否使用默認并行度的flag值,這個值可以用于重設并行度回到默認狀態
 */
public static final int PARALLELISM_DEFAULT = -1;

/**
 * The flag value indicating an unknown or unset parallelism. This value is
 * not a valid parallelism and indicates that the parallelism should remain
 * unchanged.
 */
public static final int PARALLELISM_UNKNOWN = -2;

private static final long DEFAULT_RESTART_DELAY = 10000L;

// --------------------------------------------------------------------------------------------

/** Defines how data exchange happens - batch or pipelined
 * 指明資料交換是如何發生的-批處理或流處理*/
private ExecutionMode executionMode = ExecutionMode.PIPELINED;

/** 閉包清理器等級:默認情況下遞回清理所有
 */
private ClosureCleanerLevel closureCleanerLevel = ClosureCleanerLevel.RECURSIVE;

private int parallelism = PARALLELISM_DEFAULT;

/**
 * The program wide maximum parallelism used for operators which haven't specified a maximum
 * parallelism. The maximum parallelism specifies the upper limit for dynamic scaling and the
 * number of key groups used for partitioned state.
 */
private int maxParallelism = -1;

/**
 * @deprecated Should no longer be used because it is subsumed by RestartStrategyConfiguration
 */
@Deprecated
private int numberOfExecutionRetries = -1;

private boolean forceKryo = false;

/** Flag to indicate whether generic types (through Kryo) are supported */
private boolean disableGenericTypes = false;

private boolean objectReuse = false;

private boolean autoTypeRegistrationEnabled = true;

private boolean forceAvro = false;

private CodeAnalysisMode codeAnalysisMode = CodeAnalysisMode.DISABLE;

/** If set to true, progress updates are printed to System.out during execution 如果為true,程式動態會被列印到System.out*/
private boolean printProgressDuringExecution = true;

private long autoWatermarkInterval = 0;

/**
 * Interval in milliseconds for sending latency tracking marks from the sources to the sinks.
 * 發送從source到sink的延遲檢測mark的間隔
 */
private long latencyTrackingInterval = MetricOptions.LATENCY_INTERVAL.defaultValue();

private boolean isLatencyTrackingConfigured = false;

/**
 * @deprecated Should no longer be used because it is subsumed by RestartStrategyConfiguration
 */
@Deprecated
private long executionRetryDelay = DEFAULT_RESTART_DELAY;

private RestartStrategies.RestartStrategyConfiguration restartStrategyConfiguration =
   new RestartStrategies.FallbackRestartStrategyConfiguration();

private long taskCancellationIntervalMillis = -1;

/**
 * Timeout after which an ongoing task cancellation will lead to a fatal
 * TaskManager error, usually killing the JVM.
 */
private long taskCancellationTimeoutMillis = -1;

/** This flag defines if we use compression for the state snapshot data or not. Default: false
 * 是否對狀態快照壓縮*/
private boolean useSnapshotCompression = false;

/** Determines if a task fails or not if there is an error in writing its checkpoint data. Default: true
 * 決定 當做checkpoint時發生了error時task任務是否失敗*/
private boolean failTaskOnCheckpointError = true;

/** The default input dependency constraint to schedule tasks. */
private InputDependencyConstraint defaultInputDependencyConstraint = InputDependencyConstraint.ANY;

// ------------------------------- User code values --------------------------------------------

private GlobalJobParameters globalJobParameters;

// Serializers and types registered with Kryo and the PojoSerializer
// we store them in linked maps/sets to ensure they are registered in order in all kryo instances.

private LinkedHashMap<Class<?>, SerializableSerializer<?>> registeredTypesWithKryoSerializers = new LinkedHashMap<>();

private LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> registeredTypesWithKryoSerializerClasses = new LinkedHashMap<>();

private LinkedHashMap<Class<?>, SerializableSerializer<?>> defaultKryoSerializers = new LinkedHashMap<>();

private LinkedHashMap<Class<?>, Class<? extends Serializer<?>>> defaultKryoSerializerClasses = new LinkedHashMap<>();

private LinkedHashSet<Class<?>> registeredKryoTypes = new LinkedHashSet<>();

private LinkedHashSet<Class<?>> registeredPojoTypes = new LinkedHashSet<>();

以上可以看出,ExecutionConfig可以設定的內容包括 程式的默認并行度、執行失敗時的重試次數、重啟嘗試之間的時間間隔、程式的執行模式—流或批、 啟用或禁止“closure cleaner閉包清理器”、 是否允許register 型別和序列化器來提供處理基本型別和POJO型別的效率 等內容,

以下我們再看看checkpointConfig里有什么屬性

private static final long serialVersionUID = -750378776078908147L;

/** The default checkpoint mode: exactly once. 默認checkpoint模式:精準一次 */
public static final CheckpointingMode DEFAULT_MODE = CheckpointingMode.EXACTLY_ONCE;

/** The default timeout of a checkpoint attempt: 10 minutes. 默認一次checkpoint的最長時間 十分鐘*/
public static final long DEFAULT_TIMEOUT = 10 * 60 * 1000;

/** The default minimum pause to be made between checkpoints: none. 兩次checkpint之間的最小間隔*/
public static final long DEFAULT_MIN_PAUSE_BETWEEN_CHECKPOINTS = 0;

/** The default limit of concurrently happening checkpoints: one. 同時執行的最大checkpoint個數 1次*/
public static final int DEFAULT_MAX_CONCURRENT_CHECKPOINTS = 1;

// ------------------------------------------------------------------------

/** Checkpointing mode (exactly-once vs. at-least-once). checkpoint模式*/
private CheckpointingMode checkpointingMode = DEFAULT_MODE;

/** Periodic checkpoint triggering interval. 周期性的checkpoint觸發間隔:默認關閉*/
private long checkpointInterval = -1; // disabled

/** Maximum time checkpoint may take before being discarded. */
private long checkpointTimeout = DEFAULT_TIMEOUT;

/** Minimal pause between checkpointing attempts. checkpoint嘗試的最小間隔*/
private long minPauseBetweenCheckpoints = DEFAULT_MIN_PAUSE_BETWEEN_CHECKPOINTS;

/** Maximum number of checkpoint attempts in progress at the same time. 同時進行的checkpoint嘗試的最大個數*/
private int maxConcurrentCheckpoints = DEFAULT_MAX_CONCURRENT_CHECKPOINTS;

/** Flag to force checkpointing in iterative jobs. */
private boolean forceCheckpointing;

/** Cleanup behaviour for persistent checkpoints. */
private ExternalizedCheckpointCleanup externalizedCheckpointCleanup;

/** Determines if a tasks are failed or not if there is an error in their checkpointing. Default: true */
private boolean failOnCheckpointingErrors = true;

可見CheckpointConfig中定義的都是checkpoint相關的引數和策略,

由以上三個類的屬性可見StreamExecutionEnviorment+ExecutionConfig+CheckpointConfig 定義了Flink最頂層的環境配置和策略選擇,

我們再回過頭來看看,SEE中定義了哪些重要方法?(各種get和set方法省略,注釋大部分都翻譯了,重點看?標注的地方)

首先我們看看SEE定義了哪些獲得source的方法

/**
 * Creates a new data stream that contains a sequence of numbers. This is a parallel source,
 * if you manually set the parallelism to {@code 1}
 * (using {@link org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator#setParallelism(int)})
 * the generated sequence of elements is in order.
 * ?創建一個包含了一系列數字的sequence,如果你設定了大于1的并行度,那么這將是一個并行的source,創建出來的elements是有序的
 * @param from
 *        The number to start at (inclusive) 第一個數字(包含在內)
 * @param to
 *        The number to stop at (inclusive) 最后一個數字(包含在內)
 * @return A data stream, containing all number in the [from, to] interval
 */
public DataStreamSource<Long> ?generateSequence(long from, long to) {
   if (from > to) {
      throw new IllegalArgumentException("Start of sequence must not be greater than the end");
   }
   return addSource(new StatefulSequenceSource(from, to), "Sequence Source");
   //?最后其實是使用了addSource方法
}
/**
 * Creates a new data stream that contains the given elements. The elements must all be of the
 * same type, for example, all of the {@link String} or {@link Integer}.
 * ?創建一個包含指定element的datastream,所有的element必須是相同的型別
 *
 * <p>The framework will try and determine the exact type from the elements. In case of generic
 * elements, it may be necessary to manually supply the type information via
 * {@link #fromCollection(java.util.Collection, org.apache.flink.api.common.typeinfo.TypeInformation)}.
 * 框架會嘗試獲取element的明確type,可能需要手動設定型別資訊
 * 
 * <p>Note that this operation will result in a non-parallel data stream source, i.e. a data
 * stream source with a degree of parallelism one.
 * 這個方法產生的datastream的并行度為1
 *
 * @param data
 *        The array of elements to create the data stream from.
 * @param <OUT>
 *        The type of the returned data stream
 * @return The data stream representing the given array of elements
 */
@SafeVarargs
public final <OUT> DataStreamSource<OUT> ?fromElements(OUT... data) {
   if (data.length == 0) {
      throw new IllegalArgumentException("fromElements needs at least one element as argument");
   }

   TypeInformation<OUT> typeInfo;
   try {
      typeInfo = TypeExtractor.getForObject(data[0]);
   }
   catch (Exception e) {
      throw new RuntimeException("Could not create TypeInformation for type " + data[0].getClass().getName()
            + "; please specify the TypeInformation manually via "
            + "StreamExecutionEnvironment#fromElements(Collection, TypeInformation)", e);
   }
   //?最后實際使用了fromCollection方法
   return fromCollection(Arrays.asList(data), typeInfo);
}
/**
 * Creates a data stream from the given non-empty collection.
 * ?從給出的非空collection中創建datastream,datastream的type是collection中element的type
 * <p>Note that this operation will result in a non-parallel data stream source,
 * i.e., a data stream source with parallelism one. 產生的datastream的并行度為1
 *
 * @param data
 *        The collection of elements to create the data stream from
 * @param typeInfo
 *        The TypeInformation for the produced data stream
 * @param <OUT>
 *        The type of the returned data stream
 * @return The data stream representing the given collection
 */
public <OUT> DataStreamSource<OUT> ?fromCollection(Collection<OUT> data, TypeInformation<OUT> typeInfo) {
   Preconditions.checkNotNull(data, "Collection must not be null");

   // must not have null elements and mixed elements
   FromElementsFunction.checkCollection(data, typeInfo.getTypeClass());

   SourceFunction<OUT> function;
   try {
      function = new FromElementsFunction<>(typeInfo.createSerializer(getConfig()), data);
   }
   catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
   }
   //?實際使用了addSource方法
   return addSource(function, "Collection Source", typeInfo).setParallelism(1);
}
/**
 * Creates a new data stream that contains elements in the iterator. The iterator is splittable,
 * allowing the framework to create a parallel data stream source that returns the elements in
 * the iterator.
 * ?從iterator中創建包含其中elements的datastream
 * iterator是可分割的,允許框架創建并行的回傳iterator中資料的datastream
 *
 * <p>Because the iterator will remain unmodified until the actual execution happens, the type
 * of data returned by the iterator must be given explicitly in the form of the type
 * information. This method is useful for cases where the type is generic. In that case, the
 * type class (as given in
 * {@link #fromParallelCollection(org.apache.flink.util.SplittableIterator, Class)} does not
 * supply all type information.
 * ?因為iterator直到真正的execution發生之前都會保持不變,所以iterator的回傳型別必須被明確給出(第二個引數)
 * 本方法對型別為泛型的情況非常有用
 * 
 * @param iterator
 *        The iterator that produces the elements of the data stream
 * @param typeInfo
 *        The TypeInformation for the produced data stream.
 * @param <OUT>
 *        The type of the returned data stream
 * @return A data stream representing the elements in the iterator
 */
private <OUT> DataStreamSource<OUT> ?fromParallelCollection(SplittableIterator<OUT> iterator, TypeInformation<OUT>
			typeInfo, String operatorName) {
		//?實際上使用的是addsource
		return addSource(new FromSplittableIteratorFunction<>(iterator), operatorName, typeInfo);
	}
/**
 * Creates a new data stream that contains the strings received infinitely from a socket. Received strings are
 * decoded by the system's default character set. On the termination of the socket server connection retries can be
 * initiated.
 * ?創建一個datastream從socket中接收無窮無盡的資料流,用系統默認的charset決議接收的strings
 * 只有當socket server連接中斷,才可以啟動重新連接
 *
 * <p>Let us note that the socket itself does not report on abort and as a consequence retries are only initiated when
 * the socket was gracefully terminated.
 * 明確一點:socket本身不會報告abort,因此,只有當socket本身被優雅地中止之后才會啟動重試
 *
 * @param hostname
 *        The host name which a server socket binds
 * @param port
 *        The port number which a server socket binds. A port number of 0 means that the port number is automatically
 *        allocated.
 * @param delimiter ?分割接收到的string的分隔符
 *        A string which splits received strings into records
 * @param maxRetry 當program等待socket連接時的最大重試間隔(秒級),0代表立即結束,負數代表永遠嘗試重連
 *        The maximal retry interval in seconds while the program waits for a socket that is temporarily down.
 *        Reconnection is initiated every second. A number of 0 means that the reader is immediately terminated,
 *        while a    negative value ensures retrying forever.
 * @return A data stream containing the strings received from the socket
 */
@PublicEvolving
public DataStreamSource<String> ?socketTextStream(String hostname, int port, String delimiter, long maxRetry) {
   //?最后使用的是addSource
   return addSource(new SocketTextStreamFunction(hostname, port, delimiter, maxRetry),
         "Socket Stream");
}
/**
 * Reads the given file line-by-line and creates a data stream that contains a string with the
 * contents of each such line. The {@link java.nio.charset.Charset} with the given name will be
 * used to read the files.
 * ?一行行地讀取給定檔案,創建包含這每一行string的datastream,在charsetName中設定讀取格式
 *
 * <p><b>NOTES ON CHECKPOINTING: 做checkpoint時注意</b> The source monitors the path, creates the
 * {@link org.apache.flink.core.fs.FileInputSplit FileInputSplits} to be processed,
 * forwards them to the downstream {@link ContinuousFileReaderOperator readers} to read the actual data,
 * and exits, without waiting for the readers to finish reading. This implies that no more checkpoint
 * barriers are going to be forwarded after the source exits, thus having no checkpoints after that point.
 * ?source會監控檔案路徑,創建FileInputSplits,將其傳輸到ContinuousFileReaderOperator readers來讀取真實資料,然后就可以exit了
 * 無需等到readers完全讀完資料,exit之后就不會產生barrier,即不會做checkpoint了,
 *
 * @param filePath
 *        The path of the file, as a URI (e.g., "file:///some/local/file" or "hdfs://host:port/file/path")
 * @param charsetName
 *        The name of the character set used to read the file
 * @return The data stream that represents the data read from the given file as text lines
 */
public DataStreamSource<String> ?readTextFile(String filePath, String charsetName) {
   Preconditions.checkArgument(!StringUtils.isNullOrWhitespaceOnly(filePath), "The file path must not be null or blank.");

   TextInputFormat format = new TextInputFormat(new Path(filePath));
   format.setFilesFilter(FilePathFilter.createDefaultFilter());
   TypeInformation<String> typeInfo = BasicTypeInfo.STRING_TYPE_INFO;
   format.setCharsetName(charsetName);

   //?最終執行的是readFile方法
   return readFile(format, filePath, FileProcessingMode.PROCESS_ONCE, -1, typeInfo);
}
/**
 * Reads the contents of the user-specified {@code filePath} based on the given {@link FileInputFormat}.
 * Depending on the provided {@link FileProcessingMode}, the source may periodically monitor (every {@code interval} ms)
 * the path for new data ({@link FileProcessingMode#PROCESS_CONTINUOUSLY}), or process once the data currently in the
 * path and exit ({@link FileProcessingMode#PROCESS_ONCE}). In addition, if the path contains files not to be processed,
 * the user can specify a custom {@link FilePathFilter}. As a default implementation you can use
 * {@link FilePathFilter#createDefaultFilter()}.
 * 基于給定的FileInputFormat讀取filePath中的內容
 * ???根據FileProcessingMode,source有兩種處理策略:
 *        ??一、PROCESS_CONTINUOUSLY:周期性地在path中掃描新資料
 *        ??二、PROCESS_ONCE:處理當前contents中的檔案然后退出(只處理一次)
 *
 * @param inputFormat
 *        The input format used to create the data stream 用于創建datastream的inputFormat
 * @param filePath
 *        The path of the file, as a URI (e.g., "file:///some/local/file" or "hdfs://host:port/file/path")
 * @param watchType 即FileProcessingMode,有PROCESS_CONTINUOUSLY和PROCESS_ONCE兩種
 *        The mode in which the source should operate, i.e. monitor path and react to new data, or process once and exit
 * @param typeInformation
 *        Information on the type of the elements in the output stream
 * @param interval 當FileProcessingMode為有PROCESS_CONTINUOUSLY和PROCESS_ONCE時的掃描檔案間隔
 *        In the case of periodic path monitoring, this specifies the interval (in millis) between consecutive path scans
 * @param <OUT> 回傳的datastream的資料型別
 *        The type of the returned data stream
 * @return The data stream that represents the data read from the given file
 */
@PublicEvolving
public <OUT> DataStreamSource<OUT> ?readFile(FileInputFormat<OUT> inputFormat,
                                 String filePath,
                                 FileProcessingMode watchType,
                                 long interval,
                                 TypeInformation<OUT> typeInformation) {

   Preconditions.checkNotNull(inputFormat, "InputFormat must not be null.");
   Preconditions.checkArgument(!StringUtils.isNullOrWhitespaceOnly(filePath), "The file path must not be null or blank.");

   inputFormat.setFilePath(filePath);
   //?最后實際上使用的是createFileInput()方法
   return createFileInput(inputFormat, typeInformation, "Custom File Source", watchType, interval);
}

根據以上添加source的方法的呼叫關系,我們可以得到下面這張圖

可以看到,最終是通過addSource和createFileInput兩個方法創建了新的DataStream,那么我們再來看看這兩個方法是怎么實作的吧

/**
 * Ads a data source with a custom type information thus opening a
 * {@link DataStream}. Only in very special cases does the user need to
 * support type information. Otherwise use
 * {@link #addSource(org.apache.flink.streaming.api.functions.source.SourceFunction)}
 * ?使用自定義type創建一個data source以及對應的datastream,如果typeInfo為null,則從SourceFunction中提取type資訊?
 *
 * @param function
 *        the user defined function
 * @param sourceName
 *        Name of the data source
 * @param <OUT>
 *        type of the returned stream
 * @param typeInfo
 *        the user defined type information for the stream
 * @return the data stream constructed
 */
@SuppressWarnings("unchecked")
public <OUT> DataStreamSource<OUT> ?addSource(SourceFunction<OUT> function, String sourceName, TypeInformation<OUT> typeInfo) {

   //?注意:typeInfo是可以為null的,可以從Sourcefunction提取type或者從SourceFunction創建type
   if (typeInfo == null) {
      if (function instanceof ResultTypeQueryable) {
         typeInfo = ((ResultTypeQueryable<OUT>) function).getProducedType();
      } else {
         try {
            typeInfo = TypeExtractor.createTypeInfo(
                  SourceFunction.class,
                  function.getClass(), 0, null, null);
         } catch (final InvalidTypesException e) {
            typeInfo = (TypeInformation<OUT>) new MissingTypeInfo(sourceName, e);
         }
      }
   }

   boolean isParallel = function instanceof ParallelSourceFunction;

   clean(function);
   StreamSource<OUT, ?> sourceOperator;
   //?創建SourceOperator
   if (function instanceof StoppableFunction) {
      sourceOperator = new StoppableStreamSource<>(cast2StoppableSourceFunction(function));
   } else {
      sourceOperator = new StreamSource<>(function);
   }

   //?創建DataStream并回傳(注:DataStreamSource是DataStream的子類)
   return new DataStreamSource<>(this, typeInfo, sourceOperator, isParallel, sourceName);
}
private <OUT> DataStreamSource<OUT> ?createFileInput(FileInputFormat<OUT> inputFormat,
                                       TypeInformation<OUT> typeInfo,
                                       String sourceName,
                                       FileProcessingMode monitoringMode,
                                       long interval) {

   //?檢查引數非空
   Preconditions.checkNotNull(inputFormat, "Unspecified file input format.");
   Preconditions.checkNotNull(typeInfo, "Unspecified output type information.");
   Preconditions.checkNotNull(sourceName, "Unspecified name for the source.");
   Preconditions.checkNotNull(monitoringMode, "Unspecified monitoring mode.");

   //?如果FileProcessingMode是PROCESS_CONTINUOUSLY(只進行一次全量檔案匯入)且 掃描間隔小于規定的最小interval則報錯
   //?Preconditions.checkArgument()會檢查第一個引數是否為true,如果不是true則報錯
   Preconditions.checkArgument(monitoringMode.equals(FileProcessingMode.PROCESS_ONCE) ||
         interval >= ContinuousFileMonitoringFunction.MIN_MONITORING_INTERVAL,
      "The path monitoring interval cannot be less than " +
            ContinuousFileMonitoringFunction.MIN_MONITORING_INTERVAL + " ms.");

   //?封裝引數
   ContinuousFileMonitoringFunction<OUT> monitoringFunction =
      new ContinuousFileMonitoringFunction<>(inputFormat, monitoringMode, getParallelism(), interval);

   ContinuousFileReaderOperator<OUT> reader =
      new ContinuousFileReaderOperator<>(inputFormat);

   SingleOutputStreamOperator<OUT> source = addSource(monitoringFunction, sourceName)
         .transform("Split Reader: " + sourceName, typeInfo, reader);

   //?創建和回傳DataStream
   return new DataStreamSource<>(source);
}

以上,我們介紹完了SEE中創建DataStream的方法,至于最后DataStream的具體實作請見下一篇文章

最后我們再來看看SEE的execute()方法,因為SEE抽象類中定義的execute()方法大多會被具體的子類override,所以我們不看SEE中的該方法,直接看子類的實作,以下我們以SEE的一個子類 LocalStreamEnvironment 來探究execute中做了什么,

/**
 * Triggers the program execution. The environment will execute all parts of
 * the program that have resulted in a "sink" operation. Sink operations are
 * for example printing results or forwarding them to a message queue.
 * ?觸發程式的真正執行,enviorment會執行所有的部分(以sink結尾)
 * 舉例:sink算子會將結果列印或輸出到訊息佇列中
 * 
 * <p>The program execution will be logged and displayed with the provided name
 * ?程式的執行會以jobName為名記載入日志和展示
 * 
 * Executes the JobGraph of the on a mini cluster of CLusterUtil with a user
 * specified name. ?在一個mini cluster中執行JobGraph
 *
 * @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 {
   // ?將流轉化為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");

   // ?加載和封裝user定義的設定引數
   configuration.addAll(this.configuration);

   if (!configuration.contains(RestOptions.BIND_PORT)) {
      configuration.setString(RestOptions.BIND_PORT, "0");
   }

   //?設定每個task manager中的slot數量
   int numSlotsPerTaskManager = configuration.getInteger(TaskManagerOptions.NUM_TASK_SLOTS, jobGraph.getMaximumParallelism());

   //?配置MiniCluster的引數
   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 miniCluster = new MiniCluster(cfg);

   //?啟動miniCluster,回傳jobGraph的執行結果
   try {
      miniCluster.start();
      configuration.setInteger(RestOptions.PORT, miniCluster.getRestAddress().get().getPort());

      ?return miniCluster.executeJobBlocking(jobGraph);
   }
   finally {
      transformations.clear();
      miniCluster.close();
   }
}

由上面的原始碼可見,execute中執行的內容大致就是創建JobGraph、封裝引數資訊、創建cluster、把JobGraph提交到cluster等待回傳執行結果,當然最后別忘了清理記憶體和關閉集群,也就是說在execute之前,都是客戶端在進行flink程式的封裝,只有通過execute()提交到cluster才是程式真正的執行,這也是flink程式懶加載的真相,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/352166.html

標籤:其他

上一篇:大資料熱點圖制作(微重點)

下一篇:Hadoop/Spark大資料 Cloudera CCA Spark and Hadoop certificate CCA175認證

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布
  • 2023年最新微信小程式抓包教程

    01 開門見山 隔一個月發一篇文章,不過分。 首先回顧一下《微信系結手機號資料庫被脫庫事件》,我也是第一時間得知了這個訊息,然后跟蹤了整件事情的經過。下面是這起事件的相關截圖以及近日流出的一萬條資料樣本: 個人認為這件事也沒什么,還不如關注一下之前45億快遞資料查詢渠道疑似在近日復活的訊息。 訊息是 ......

    uj5u.com 2023-04-20 08:48:24 more
  • web3 產品介紹:metamask 錢包 使用最多的瀏覽器插件錢包

    Metamask錢包是一種基于區塊鏈技術的數字貨幣錢包,它允許用戶在安全、便捷的環境下管理自己的加密資產。Metamask錢包是以太坊生態系統中最流行的錢包之一,它具有易于使用、安全性高和功能強大等優點。 本文將詳細介紹Metamask錢包的功能和使用方法。 一、 Metamask錢包的功能 數字資 ......

    uj5u.com 2023-04-20 08:47:46 more
  • vulnhub_Earth

    前言 靶機地址->>>vulnhub_Earth 攻擊機ip:192.168.20.121 靶機ip:192.168.20.122 參考文章 https://www.cnblogs.com/Jing-X/archive/2022/04/03/16097695.html https://www.cnb ......

    uj5u.com 2023-04-20 07:46:20 more
  • 從4k到42k,軟體測驗工程師的漲薪史,給我看哭了

    清明節一過,盲猜大家已經無心上班,在數著日子準備過五一,但一想到銀行卡里的余額……瞬間心情就不美麗了。最近,2023年高校畢業生就業調查顯示,本科畢業月平均起薪為5825元。調查一出,便有很多同學表示自己又被平均了。看著這一資料,不免讓人想到前不久中國青年報的一項調查:近六成大學生認為畢業10年內會 ......

    uj5u.com 2023-04-20 07:44:00 more
  • 最新版本 Stable Diffusion 開源 AI 繪畫工具之中文自動提詞篇

    🎈 標簽生成器 由于輸入正向提示詞 prompt 和反向提示詞 negative prompt 都是使用英文,所以對學習母語的我們非常不友好 使用網址:https://tinygeeker.github.io/p/ai-prompt-generator 這個網址是為了讓大家在使用 AI 繪畫的時候 ......

    uj5u.com 2023-04-20 07:43:36 more
  • 漫談前端自動化測驗演進之路及測驗工具分析

    隨著前端技術的不斷發展和應用程式的日益復雜,前端自動化測驗也在不斷演進。隨著 Web 應用程式變得越來越復雜,自動化測驗的需求也越來越高。如今,自動化測驗已經成為 Web 應用程式開發程序中不可或缺的一部分,它們可以幫助開發人員更快地發現和修復錯誤,提高應用程式的性能和可靠性。 ......

    uj5u.com 2023-04-20 07:43:16 more
  • CANN開發實踐:4個DVPP記憶體問題的典型案例解讀

    摘要:由于DVPP媒體資料處理功能對存放輸入、輸出資料的記憶體有更高的要求(例如,記憶體首地址128位元組對齊),因此需呼叫專用的記憶體申請介面,那么本期就分享幾個關于DVPP記憶體問題的典型案例,并給出原因分析及解決方法。 本文分享自華為云社區《FAQ_DVPP記憶體問題案例》,作者:昇騰CANN。 DVPP ......

    uj5u.com 2023-04-20 07:43:03 more
  • msf學習

    msf學習 以kali自帶的msf為例 一、msf核心模塊與功能 msf模塊都放在/usr/share/metasploit-framework/modules目錄下 1、auxiliary 輔助模塊,輔助滲透(埠掃描、登錄密碼爆破、漏洞驗證等) 2、encoders 編碼器模塊,主要包含各種編碼 ......

    uj5u.com 2023-04-20 07:42:59 more
  • Halcon軟體安裝與界面簡介

    1. 下載Halcon17版本到到本地 2. 雙擊安裝包后 3. 步驟如下 1.2 Halcon軟體安裝 界面分為四大塊 1. Halcon的五個助手 1) 影像采集助手:與相機連接,設定相機引數,采集影像 2) 標定助手:九點標定或是其它的標定,生成標定檔案及內參外參,可以將像素單位轉換為長度單位 ......

    uj5u.com 2023-04-20 07:42:17 more
  • 在MacOS下使用Unity3D開發游戲

    第一次發博客,先發一下我的游戲開發環境吧。 去年2月份買了一臺MacBookPro2021 M1pro(以下簡稱mbp),這一年來一直在用mbp開發游戲。我大致分享一下我的開發工具以及使用體驗。 1、Unity 官網鏈接: https://unity.cn/releases 我一般使用的Apple ......

    uj5u.com 2023-04-20 07:40:19 more