您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Flink怎么執(zhí)行用戶程序”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Flink怎么執(zhí)行用戶程序”吧!
以flink on yarn為例:
(1)在CliFrontend的main()方法中,生成GenericCLI、FlinkYarnSessionCli、DefaultCLI三種命令行對象,依次放入ArrayList對象customCommandLines中
public static void main(final String[] args) { EnvironmentInformation.logEnvironmentInfo(LOG, "Command Line Client", args); ...... // 3. load the custom command lines final List<CustomCommandLine> customCommandLines = loadCustomCommandLines( configuration, configurationDirectory); ...... }
在后面 run() -> validateAndGetActiveCommandLine()方法中依次從customCommandLines對象中取出命令行對象,調(diào)用isActive()方法,判斷是哪一種命令行
public CustomCommandLine validateAndGetActiveCommandLine(CommandLine commandLine) { for (CustomCommandLine cli : customCommandLines) { if (cli.isActive(commandLine)) { return cli; } } throw new IllegalStateException("No valid command-line found."); }
FlinkYarnSessionCli的isActive()方法中,會去判斷運(yùn)行bin/flink腳本時是否傳入了-m參數(shù),其值是否為yarn-cluster
@Override public boolean isActive(CommandLine commandLine) { final String jobManagerOption = commandLine.getOptionValue(addressOption.getOpt(), null); final boolean yarnJobManager = ID.equals(jobManagerOption); final boolean hasYarnAppId = commandLine.hasOption(applicationId.getOpt()) || configuration.getOptional(YarnConfigOptions.APPLICATION_ID).isPresent(); final boolean hasYarnExecutor = YarnSessionClusterExecutor.NAME.equalsIgnoreCase(configuration.get(DeploymentOptions.TARGET)) || YarnJobClusterExecutor.NAME.equalsIgnoreCase(configuration.get(DeploymentOptions.TARGET)); return hasYarnExecutor || yarnJobManager || hasYarnAppId || (isYarnPropertiesFileMode(commandLine) && yarnApplicationIdFromYarnProperties != null); }
addressOption為匹配-m,ID為yarn-cluster
(2)在CliFrontend的run()方法中,通過getEffectiveConfiguration()方法得到Configuration對象,傳入的命令行對象activeCommandLine即為上面第一個步驟中得到的FlinkYarnSessionCli;
在getEffectiveConfiguration()方法中會調(diào)用FlinkYarnSessionCli的applyCommandLineOptionsToConfiguration()方法來增加和yarn相關(guān)的配置,代碼如下:
public Configuration applyCommandLineOptionsToConfiguration(CommandLine commandLine) throws FlinkException { // we ignore the addressOption because it can only contain "yarn-cluster" final Configuration effectiveConfiguration = new Configuration(configuration); applyDescriptorOptionToConfig(commandLine, effectiveConfiguration); final ApplicationId applicationId = getApplicationId(commandLine); if (applicationId != null) { final String zooKeeperNamespace; if (commandLine.hasOption(zookeeperNamespace.getOpt())){ zooKeeperNamespace = commandLine.getOptionValue(zookeeperNamespace.getOpt()); } else { zooKeeperNamespace = effectiveConfiguration.getString(HA_CLUSTER_ID, applicationId.toString()); } effectiveConfiguration.setString(HA_CLUSTER_ID, zooKeeperNamespace); effectiveConfiguration.setString(YarnConfigOptions.APPLICATION_ID, ConverterUtils.toString(applicationId)); effectiveConfiguration.setString(DeploymentOptions.TARGET, YarnSessionClusterExecutor.NAME); } else { effectiveConfiguration.setString(DeploymentOptions.TARGET, YarnJobClusterExecutor.NAME); } ...... ...... }
其中關(guān)鍵配置DeploymentOptions.TARGET,即程序目標(biāo)運(yùn)行環(huán)境;YarnJobClusterExecutor.NAME 值為
public enum YarnDeploymentTarget { PER_JOB("yarn-per-job"), ..... }
在ClientUitls的executeProgram()中通過下面代碼設(shè)置:
public static void executeProgram( PipelineExecutorServiceLoader executorServiceLoader, Configuration configuration, PackagedProgram program, boolean enforceSingleJobExecution, boolean suppressSysout) throws ProgramInvocationException { ...... try { ...... StreamContextEnvironment.setAsContext( executorServiceLoader, configuration, userCodeClassLoader, enforceSingleJobExecution, suppressSysout); ...... } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } }
ClientUtils的executeProgram()方法中調(diào)用PackagedProgram的invokeInteractiveModeForExecution(),來執(zhí)行用戶main()方法
private static void callMainMethod(Class<?> entryClass, String[] args) throws ProgramInvocationException { Method mainMethod; if (!Modifier.isPublic(entryClass.getModifiers())) { ...... } try { mainMethod = entryClass.getMethod("main", String[].class); } catch (NoSuchMethodException e) { ...... } catch (Throwable t) { ...... } if (!Modifier.isStatic(mainMethod.getModifiers())) { ...... } if (!Modifier.isPublic(mainMethod.getModifiers())) { ...... } try { mainMethod.invoke(null, (Object) args); } catch (IllegalArgumentException e) { ...... } catch (IllegalAccessException e) { ...... } catch (InvocationTargetException e) { ...... } catch (Throwable t) { } }
到此,相信大家對“Flink怎么執(zhí)行用戶程序”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。