说明:在生产中,我们需要了解yarn上job的运行状况时不可能一直到yarn界面去看,我们可以从yarn的REST API获取job运行状态信息,简单java代码如下:
Test_Yarn_Rest.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test_Yarn_Rest {
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
public static void main(String[] args) throws IOException, JSONException {
JSONObject json = readJsonFromUrl("http://10.1.236.53:8088/ws/v1/cluster/apps");
System.out.println(json.toString());
JSONObject apps=json.getJSONObject("apps");
JSONArray app=apps.getJSONArray("app");
for(int i=0;i<app.length();i++){
JSONObject info=app.getJSONObject(i);
String id =info.getString("id");
System.out.println("appid:"+id);
String user =info.getString("user");
System.out.println("user:"+user);
String name =info.getString("name");
System.out.println("job name:"+name);
String queue =info.getString("queue");
System.out.println("job queue:"+queue);
String state =info.getString("state");
System.out.println("job state:"+state);
String finalStatus =info.getString("finalStatus");
System.out.println("job finalStatus:"+finalStatus);
int progress =info.getInt("progress");
System.out.println("job progress:"+progress);
String applicationType =info.getString("applicationType");
System.out.println("job applicationType:"+applicationType);
long startedTime=info.getLong("startedTime");
System.out.println("job startedTime:"+startedTime);
long finishedTime=info.getLong("finishedTime");
System.out.println("job finishedTime:"+finishedTime);
long elapsedTime=info.getLong("elapsedTime");
System.out.println("job elapsedTime:"+elapsedTime);
System.out.println("-------------------------------------------------------------------------------------------------------");
}
}
}
输出结果如下:(当然你可以根据自己的需求进行过滤)
-------------------------------------------------------------------------------------------------------
appid:application_1546508207602_0001
user:yinkp
job name:PigLatin:pigSmoke.sh
job queue:default
job state:FINISHED
job finalStatus:SUCCEEDED
job progress:100
job applicationType:MAPREDUCE
job startedTime:1546508367912
job finishedTime:1546508390941
job elapsedTime:23029
-------------------------------------------------------------------------------------------------------
appid:application_1546508207602_0004
user:yinkp
job name:DistributedShell
job queue:default
job state:FINISHED
job finalStatus:SUCCEEDED
job progress:100
job applicationType:YARN
job startedTime:1546508461000
job finishedTime:1546508469481
job elapsedTime:8481
-------------------------------------------------------------------------------------------------------
appid:application_1546508207602_0003
user:yinkp
job name:OrderedWordCount
job queue:default
job state:FINISHED
job finalStatus:SUCCEEDED
job progress:100
job applicationType:TEZ
job startedTime:1546508442685
job finishedTime:1546508459367
job elapsedTime:16682
-------------------------------------------------------------------------------------------------------