本篇內(nèi)容主要講解“Flink怎么執(zhí)行用戶程序”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Flink怎么執(zhí)行用戶程序”吧!
創(chuàng)新互聯(lián)主營恩施土家網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,手機(jī)APP定制開發(fā),恩施土家h5成都微信小程序搭建,恩施土家網(wǎng)站營銷推廣歡迎恩施土家等地區(qū)企業(yè)咨詢
以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 ListcustomCommandLines = 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腳本時(shí)是否傳入了-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即為上面第一個(gè)步驟中得到的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í)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!