Flink自定义Source之读取url

package com.telecom.journal.util.source;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.telecom.journal.model.Api;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;

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

public class YarnRestSource extends RichSourceFunction {

    private String url;
    private ObjectMapper objectMapper = new ObjectMapper();

    public YarnRestSource(String url) {
        this.url = url;
    }

    @Override
    public void run(SourceContext ctx) throws Exception {
        while (true) {
            URL yarnUrl = new URL(url);  // 指定要连接的URL
            HttpURLConnection connection = (HttpURLConnection) yarnUrl.openConnection();
            connection.setRequestMethod("GET");

            // 读取数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = reader.readLine();

            // TODO: 解析JSON数据并进行相应的处理
            // 将line解析为JsonNode对象
            JsonNode jsonNode = objectMapper.readTree(line);
            // 获取名为"apps"的属性下的名为"app"的子节点
            JsonNode appNode = jsonNode.path("apps").path("app");
            for (JsonNode node : appNode) {
                // 将每个子节点转换为JSON字符串形式,并存储在value变量中
                String value = objectMapper.writeValueAsString(node);
                // 将JSON字符串反序列化为Api类的对象
                Api api = objectMapper.readValue(value, Api.class);
                ctx.collect(api);
            }

            reader.close();
            Thread.sleep(600000);// 10分钟读取一次
        }
    }

    @Override
    public void cancel() {
    }
}

你可能感兴趣的:(#,Flink-Java版,flink)