spark rest api解析

南宫喜
2023-12-01
package com.ht.bigdata.servive4;
//import org.geotools.jdbc.JDBCDataStore;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import java.text.SimpleDateFormat;




import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Decode {


    public static void main(String[] args) throws Exception {

//        String time1 = new SimpleDateFormat("hh:mm:ss").format(new Date());
//        System.out.println(time1);
//        String stringDate = "Thu Oct 16 07:13:48 GMT 2015";
//        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'GMT' yyyy",Locale.US);
//        Date date =sdf.parse(stringDate);
//        System.out.println(date.toString());


//字符串转Date
//        String stringDate = "Thu Oct 16 07:13:48 GMT 2015";
//
//        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM ddHH:mm:ss 'GMT' yyyy",Locale.US);
//        Date date =sdf.parse(stringDate);
//
//        DateFormat  df2 = DateFormat.getDateTimeInstance();
//        System.out.println(date.toString());
//        System.out.println(df2.format(date));

//        String stringDate = "2019-10-14T14:27:16.247GMT";
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.ENGLISH);
//        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));//定义时间系统
//        Date date =sdf.parse(stringDate);
//
//        DateFormat  df2 = DateFormat.getDateTimeInstance();//正常格式打印出来
//        System.out.println(date.toString());
//
//        System.out.println(df2.format(date));

//        Date date = new Date();
//        DateFormat cstFormat = new SimpleDateFormat();
//
//        DateFormat gmtFormat = new SimpleDateFormat();
//
//        TimeZone gmtTime = TimeZone.getTimeZone("GMT");
//
//        TimeZone cstTime = TimeZone.getTimeZone("CST");
//
//        cstFormat.setTimeZone(gmtTime);
//
//        gmtFormat.setTimeZone(cstTime);
//
//        System.out.println("GMT Time: " + cstFormat.format(date));
//
//        System.out.println("CST Time: " + gmtFormat.format(date));

//        String strURL = "http://localhost:4040/api/v1/applications/local-1571829482286/jobs";

        String strURL = "http://localhost:4040/api/v1/applications";
        URL url = new URL(strURL);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
        String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            buffer.append(line.toString());
            System.out.println("--------"+line);
        }
        reader.close();
        httpConn.disconnect();
        String js=buffer.toString();
        System.out.println(buffer.toString());

        JSONArray jsonArray = new JSONArray(js);//JSON数组元素
        for(int i=0;i<jsonArray .length();i++){
            //遍历所有JSON数组元素
            JSONObject jsonObject = jsonArray .getJSONObject(i);//第i个JSON对象
            String name= jsonObject.getString("name");//APP Name
            String attempts= jsonObject.getString("attempts");//进入具体信息内容
//            System.out.println(attempts);
            JSONArray jsonArray_2 = new JSONArray(attempts);//attempts仍为JSON数组
            for(int j=0;j<jsonArray_2 .length();j++) {
                JSONObject jsonObject_2 = jsonArray_2 .getJSONObject(j);
                String startTime= jsonObject_2.getString("startTime");//获取该APP开始时间,默认GMT时间系统
                String endTime= jsonObject_2.getString("endTime");//获取该APP结束时间,默认GMT时间系统
                Boolean completed= jsonObject_2.getBoolean("completed");//正在进行还是已完成

                //时间系统转换
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",Locale.ENGLISH);
                sdf.setTimeZone(TimeZone.getTimeZone("GMT"));//指定时间系统
                Date start_date =sdf.parse(startTime);//得到的是北京时间
                Date end_date =sdf.parse(endTime);

                //时间使用正常格式的形如2019-10-15 11:05:31
                DateFormat  df2 = DateFormat.getDateTimeInstance();
                startTime=df2.format(start_date);
                endTime=df2.format(end_date);

                //开始与结束时间差,毫秒
                Long interval=end_date.getTime()-start_date.getTime();

                System.out.println("Spark程序"+name+":"+startTime+"----"+endTime+"  运行了"+interval/1000.0+"秒"+"   是否完成:"+completed);
            }
        }
    }
}

 

 类似资料: