import com.google.common.collect.Sets;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
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;
import java.io.IOException;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
@Data
@Slf4j
public class YarnAdapter {
private YarnClient yarnClient;
public void init() {
Configuration conf = new YarnConfiguration();
Configuration configuration = new YarnConfiguration(conf);
yarnClient = YarnClient.createYarnClient();
yarnClient.init(configuration);
yarnClient.start();
}
public YarnApplicationState getApplicationState(ApplicationId applicationId) {
YarnApplicationState yarnApplicationState = null;
try {
init();
ApplicationReport applicationReport = yarnClient.getApplicationReport(applicationId);
yarnApplicationState = applicationReport.getYarnApplicationState();
} catch (YarnException e) {
log.error("client.getApplications YarnException", e);
} catch (IOException e) {
log.error("client.getApplications IOException", e);
} finally {
try {
yarnClient.close();
} catch (IOException e) {
log.error("client.getApplications IOException", e);
}
}
return yarnApplicationState;
}
public void killApplicationId(ApplicationId applicationId) {
try {
init();
yarnClient.killApplication(applicationId);
} catch (YarnException e) {
log.error("client.killApplicationId YarnException", e);
} catch (IOException e) {
log.error("client.killApplicationId IOException", e);
} finally {
try {
yarnClient.close();
} catch (IOException e) {
log.error("client.getApplications IOException", e);
}
}
}
public ApplicationId getRunningApplicationId(String applicationName) {
Set<String> applicationTypes = Sets.newHashSet();
applicationTypes.add("MAPREDUCE");
applicationTypes.add("SPARK");
EnumSet<YarnApplicationState> applicationStates = EnumSet.noneOf(YarnApplicationState.class);
applicationStates.add(YarnApplicationState.ACCEPTED);
applicationStates.add(YarnApplicationState.SUBMITTED);
applicationStates.add(YarnApplicationState.RUNNING);
applicationStates.add(YarnApplicationState.NEW);
applicationStates.add(YarnApplicationState.NEW_SAVING);
List<ApplicationReport> applicationReports = null;
try {
init();
applicationReports = yarnClient.getApplications(applicationTypes, applicationStates);
} catch (YarnException e) {
log.error("client.getApplications YarnException", e);
} catch (IOException e) {
log.error("client.getApplications IOException", e);
} finally {
try {
yarnClient.close();
} catch (IOException e) {
log.error("client.getApplications IOException", e);
}
}
// 获取最新版本的运行程序
ApplicationId latestApplicationId = new ApplicationId() {
@Override
public int getId() {
return 0;
}
@Override
protected void setId(int id) {
}
@Override
public long getClusterTimestamp() {
return 0;
}
@Override
protected void setClusterTimestamp(long clusterTimestamp) {
}
@Override
protected void build() {
}
};
if (CollectionUtils.isNotEmpty(applicationReports)) {
for (ApplicationReport applicationReport : applicationReports) {
if (StringUtils.equals(applicationReport.getName(), applicationName)) {
if (applicationReport.getApplicationId().compareTo(latestApplicationId) == 1) {
latestApplicationId = applicationReport.getApplicationId();
}
}
}
}
return latestApplicationId;
}
}
yarnClient使用
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...