2015/8/27

Yarn Client java api example


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.apache.hadoop.yarn.api.records.ApplicationReport;
import org.apache.hadoop.yarn.api.records.YarnApplicationState;
import org.apache.hadoop.yarn.client.api.YarnClient;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.exceptions.YarnException;
public class YarnClientTest {
public static void main(String[] args) throws YarnException, IOException {
if (args.length != 2) {
System.err.println("Expecting 2 arguments");
System.exit(1);
}
String type = args[0];
String app_id = null;
String user_id = null;
List<ApplicationId> user_applications = new ArrayList<ApplicationId>();
YarnApplicationState running_app = YarnApplicationState.RUNNING;
YarnConfiguration conf = new YarnConfiguration();
YarnClient c = YarnClient.createYarnClient();
c.init(conf);
c.start();
if (type.equalsIgnoreCase("app_id")) {
app_id = args[1];
for (ApplicationReport application: c.getApplications()) {
if (application.getApplicationId().equals(app_id) && application.getYarnApplicationState().equals(running_app))
c.killApplication(application.getApplicationId());
}
} else if (type.equalsIgnoreCase("user_id")) {
user_id = args[1];
for (ApplicationReport application: c.getApplications()) {
if (application.getUser().equals(user_id))
user_applications.add(application.getApplicationId());
if (!user_applications.isEmpty()) {
for (ApplicationId application_id: user_applications) {
if (application.getYarnApplicationState().equals(running_app))
c.killApplication(application_id);
}
} else {
System.out.println("The user_id " + user_id + " does not have any applications associated");
}
}
} else {
System.err.println("Expecting either 'app_id' or 'user_id' as parameter 1");
System.exit(1);
}
}
}


spring

/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.yarn.examples;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.yarn.YarnSystemConstants;
import org.springframework.yarn.client.CommandLineClientRunner;
import org.springframework.yarn.client.YarnClient;
/**
* Main class for kill-application example.
*
* @author Janne Valkealahti
*
*/
public class Main extends CommandLineClientRunner {
private static final Log log = LogFactory.getLog(Main.class);
public static void main(String args[]) {
Properties properties = StringUtils.splitArrayElementsIntoProperties(args, "=");
if (properties == null) {
properties = new Properties();
}
boolean nokill = Boolean.parseBoolean(properties.getProperty("nokill"));
String appid = properties.getProperty("appid");
if (nokill) {
new Main().doMain(new String[] {
YarnSystemConstants.DEFAULT_CONTEXT_FILE_CLIENT,
YarnSystemConstants.DEFAULT_ID_CLIENT,
CommandLineClientRunner.OPT_SUBMIT
});
} else if (appid != null) {
new Main().doMain(new String[] {
YarnSystemConstants.DEFAULT_CONTEXT_FILE_CLIENT,
YarnSystemConstants.DEFAULT_ID_CLIENT,
CommandLineClientRunner.OPT_KILL,
CommandLineClientRunner.ARG_APPLICATION_ID + "=" + appid
});
} else if (!nokill) {
ConfigurableApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext("application-context.xml");
System.out.println("Submitting kill-application example");
YarnClient client = (YarnClient) context.getBean("yarnClient");
ApplicationId applicationId = client.submitApplication();
System.out.println("Submitted kill-application example");
System.out.println("Waiting 30 seconds before aborting the application");
Thread.sleep(30000);
System.out.println("Asking resource manager to abort application with applicationid=" + applicationId);
client.killApplication(applicationId);
} catch (Throwable e) {
log.error("Error in main method", e);
} finally {
if (context != null) {
context.close();
}
}
}
}
}