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);
}
}
// 獲取最新版本的運(yùn)行程序
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使用
最後編輯於 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門生闲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人勘伺,你說我怎么就攤上這事跪腹」由荆” “怎么了飞醉?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)屯阀。 經(jīng)常有香客問我缅帘,道長(zhǎng),這世上最難降的妖魔是什么难衰? 我笑而不...
- 正文 為了忘掉前任钦无,我火速辦了婚禮,結(jié)果婚禮上盖袭,老公的妹妹穿的比我還像新娘失暂。我一直安慰自己,他們只是感情好鳄虱,可當(dāng)我...
- 文/花漫 我一把揭開白布弟塞。 她就那樣靜靜地躺著,像睡著了一般拙已。 火紅的嫁衣襯著肌膚如雪决记。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼潮罪,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起错洁,我...
- 序言:老撾萬榮一對(duì)情侶失蹤秉宿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后屯碴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體描睦,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年导而,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了忱叭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站实牡,受9級(jí)特大地震影響陌僵,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜创坞,卻給世界環(huán)境...
- 文/蒙蒙 一碗短、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧题涨,春花似錦偎谁、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至婉支,卻和暖如春鸯隅,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背向挖。 一陣腳步聲響...
- 正文 我出身青樓跟畅,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親溶推。 傳聞我的和親對(duì)象是個(gè)殘疾皇子徊件,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 毒膽7 雙膽79 三膽079 絕殺碼5 刺殺碼4 五碼復(fù)試01789 個(gè)人觀點(diǎn)奸攻,僅供參考!