shell spark-submit提交之后获取appid,并在程序中扫描状态

使用shell获取spark-submit的appid并监控状态
本文介绍了如何通过shell脚本提交spark作业,并在提交后获取application_id。接着,利用YARN的REST API来监控作业状态,以application_1591596770810_0207为例展示了具体操作。

#首先看一下提交脚本

#/sbin/bash
/opt/cloudera/parcels/SPARK2/bin/spark2-submit \ 
export JAVA_HOME=/opt/jdk1.8.0_31
TASK_TYPE=$1
TASK_JSON=$2
SPARK_CONFIG=$3
appId=`
$SPARK_CONFIG \
"$TASK_JSON" \
$TASK_TYPE \
2>&1 | tee /dev/tty | grep -i "Submitting application" | grep -o  application_[0-#9]*_[0-9]*`
echo "appid:"$appId

就是下面这两个 我们输出格式application_1591596770810_0207
appId=2>&1 | tee /dev/tty | grep -i "Submitting application" | grep -o application_[0-#9]*_[0-9]*
echo “appid:”$appId

###利用yarn提供的restapi去取

//yarnaddress: http://*****:8088/ws/v1/cluster/apps
    private 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 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 = JSONObject.parseObject(jsonText);// new JSONObject(jsonText);

            return json;

        } finally {

            is.close();

        }

    }

###以下值提供参考

 JSONObject info = app.getJSONObject(i);

            String id = info.getString("id");
           // if (!id.equals("application_1591164470599_0150")) continue;

            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.getInteger("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("-------------------------------------------------------------------------------------------------------");

#####文出自,南修子,转载请注明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南修子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值