3535import org .apache .flink .runtime .minicluster .MiniCluster ;
3636import org .apache .flink .runtime .minicluster .MiniClusterConfiguration ;
3737import org .apache .flink .runtime .util .LeaderConnectionInfo ;
38+ import org .apache .flink .yarn .YarnClientYarnClusterInformationRetriever ;
3839import org .apache .flink .yarn .YarnClusterDescriptor ;
3940import org .apache .hadoop .yarn .api .records .ApplicationId ;
4041import org .apache .hadoop .yarn .api .records .ApplicationReport ;
@@ -62,7 +63,7 @@ public static ClusterClient createClusterClient(Options launcherOptions) throws
6263 if (mode .equals (ClusterMode .standalone .name ())) {
6364 return createStandaloneClient (launcherOptions );
6465 } else if (mode .equals (ClusterMode .yarn .name ())) {
65- // return createYarnSessionClient(launcherOptions);
66+ return createYarnSessionClient (launcherOptions );
6667 }
6768 throw new IllegalArgumentException ("Unsupported cluster client type: " );
6869 }
@@ -77,50 +78,56 @@ public static ClusterClient createStandaloneClient(Options launcherOptions) thro
7778 return clusterClient ;
7879 }
7980
80- // public static ClusterClient createYarnSessionClient(Options launcherOptions) {
81- // String flinkConfDir = StringUtils.isEmpty(launcherOptions.getFlinkconf()) ? "" : launcherOptions.getFlinkconf();
82- // Configuration config = StringUtils.isEmpty(flinkConfDir) ? new Configuration() : GlobalConfiguration.loadConfiguration(flinkConfDir);
83- // String yarnConfDir = launcherOptions.getYarnconf();
84- //
85- // if (StringUtils.isNotBlank(yarnConfDir)) {
86- // try {
87- // config.setString(ConfigConstants.PATH_HADOOP_CONFIG, yarnConfDir);
88- // FileSystem.initialize(config, null);
89- //
90- // YarnConfiguration yarnConf = YarnConfLoader.getYarnConf(yarnConfDir);
91- // YarnClient yarnClient = YarnClient.createYarnClient();
92- // yarnClient.init(yarnConf);
93- // yarnClient.start();
94- // ApplicationId applicationId = null;
95- //
96- // String yarnSessionConf = launcherOptions.getYarnSessionConf();
97- // yarnSessionConf = URLDecoder.decode(yarnSessionConf, Charsets.UTF_8.toString());
98- // Properties yarnSessionConfProperties = PluginUtil.jsonStrToObject(yarnSessionConf, Properties.class);
99- // Object yid = yarnSessionConfProperties.get("yid");
100- //
101- // if (null != yid) {
102- // applicationId = toApplicationId(yid.toString());
103- // } else {
104- // applicationId = getYarnClusterApplicationId(yarnClient);
105- // }
106- //
107- // System.out.println("applicationId=" + applicationId.toString());
108- //
109- // if (StringUtils.isEmpty(applicationId.toString())) {
110- // throw new RuntimeException("No flink session found on yarn cluster.");
111- // }
112- //
113- // AbstractYarnClusterDescriptor clusterDescriptor = new YarnClusterDescriptor(config, yarnConf, flinkConfDir, yarnClient, false);
114- // ClusterClient clusterClient = clusterDescriptor.retrieve(applicationId);
115- // clusterClient.setDetached(true);
116- // return clusterClient;
117- // } catch (Exception e) {
118- // throw new RuntimeException(e);
119- // }
120- // }else{
121- // throw new RuntimeException("yarn mode must set param of 'yarnconf'!!!");
122- // }
123- // }
81+ public static ClusterClient createYarnSessionClient (Options launcherOptions ) {
82+ String flinkConfDir = StringUtils .isEmpty (launcherOptions .getFlinkconf ()) ? "" : launcherOptions .getFlinkconf ();
83+ Configuration config = StringUtils .isEmpty (flinkConfDir ) ? new Configuration () : GlobalConfiguration .loadConfiguration (flinkConfDir );
84+ String yarnConfDir = launcherOptions .getYarnconf ();
85+
86+ if (StringUtils .isNotBlank (yarnConfDir )) {
87+ try {
88+ config .setString (ConfigConstants .PATH_HADOOP_CONFIG , yarnConfDir );
89+ FileSystem .initialize (config , null );
90+
91+ YarnConfiguration yarnConf = YarnConfLoader .getYarnConf (yarnConfDir );
92+ YarnClient yarnClient = YarnClient .createYarnClient ();
93+ yarnClient .init (yarnConf );
94+ yarnClient .start ();
95+ ApplicationId applicationId = null ;
96+
97+ String yarnSessionConf = launcherOptions .getYarnSessionConf ();
98+ yarnSessionConf = URLDecoder .decode (yarnSessionConf , Charsets .UTF_8 .toString ());
99+ Properties yarnSessionConfProperties = PluginUtil .jsonStrToObject (yarnSessionConf , Properties .class );
100+ Object yid = yarnSessionConfProperties .get ("yid" );
101+
102+ if (null != yid ) {
103+ applicationId = toApplicationId (yid .toString ());
104+ } else {
105+ applicationId = getYarnClusterApplicationId (yarnClient );
106+ }
107+
108+ System .out .println ("applicationId=" + applicationId .toString ());
109+
110+ if (StringUtils .isEmpty (applicationId .toString ())) {
111+ throw new RuntimeException ("No flink session found on yarn cluster." );
112+ }
113+
114+ YarnClusterDescriptor clusterDescriptor = new YarnClusterDescriptor (
115+ config ,
116+ yarnConf ,
117+ yarnClient ,
118+ YarnClientYarnClusterInformationRetriever .create (yarnClient ),
119+ false );
120+
121+ ClusterClientProvider <ApplicationId > retrieve = clusterDescriptor .retrieve (applicationId );
122+ ClusterClient <ApplicationId > clusterClient = retrieve .getClusterClient ();
123+ return clusterClient ;
124+ } catch (Exception e ) {
125+ throw new RuntimeException (e );
126+ }
127+ }else {
128+ throw new RuntimeException ("yarn mode must set param of 'yarnconf'!!!" );
129+ }
130+ }
124131
125132
126133 private static ApplicationId getYarnClusterApplicationId (YarnClient yarnClient ) throws Exception {
0 commit comments