sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Flink 读取 本地日志文件
在 Flink 中,可以使用 StreamExecutionEnvironment 的 readTextFile() 或者 addSource() 来读取本地日志文件。
使用 readTextFile() 方法读取本地日志文件示例如下所示:

点击查看代码
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
public class ReadLocalLogs {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 设置并行度为1(便于调试)
        env.setParallelism(1);
        String logPath = "/path/to/local/logs";
        DataStream<String> logs = env.readTextFile(logPath);
        // 对每条日志进行处理操作
        logs.print();
        env.execute("Read Local Logs");
    }
}

2、上面的代码会将指定路径下的日志文件逐行读入到 Flink 程序中,然后通过 .print() 打印输出。
使用 addSource() 方法自定义数据源读取本地日志文件示例如下所示:

点击查看代码
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import java.io.*;
public class CustomDataSource implements RichSourceFunction<String> {
    private volatile boolean isRunning = true;
    private BufferedReader reader;
    @Override
    public void open(Configuration parameters) throws Exception {
        File file = new File("/path/to/local/logs");
        InputStream inputStream = new FileInputStream(file);
        reader = new BufferedReader(new InputStreamReader(inputStream));
    }
    @Override
    public void run(SourceContext<String> ctx) throws Exception {
        while (isRunning && !Thread.currentThread().isInterrupted()) {
            String line = reader.readLine();
            if (line != null) {
                ctx.collect(line);
            } else {
                Thread.sleep(500);
            }
        }
    }
    @Override
    public void cancel() {
        isRunning = false;
    }
}
public class MainClass {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); 
        // 创建自定义数据源
        DataStream<String> logs = env.addSource(new CustomDataSource()); 
        // 对每条日志进行处理操作
        logs.print();
        env.execute("Custom Source Example");
    }
}
这里我们自定义了一个名为 CustomDataSource 的类,该类实现了 RichSourceFunction 接口,重写了其中的三个方法:open(), run(), cancel()。在 open() 方法中初始化了一个 BufferedReader 对象,从而能够按行读取本地日志文件;在 run() 方法中不断读取日志内容,直到达到结尾或被取消;最后在 cancel() 方法中关闭相应的资源。

注意:需要根据实际情况修改 /path/to/local/logs 为正确的本地日志文件路径。

posted on   sunny123456  阅读(246)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-01-12 SpringBoot简单整合JPA
2023-01-12 Springboot简单整合JPA示例
2023-01-12 Springboot 整合JPA
2023-01-12 Springboot使用JPA配置多数据源
2023-01-12 Jpa使用小坑
2023-01-12 JPQL语法总结对比原生sql
2023-01-12 SpringDataJpa的四种查询方式详解
点击右上角即可分享
微信分享提示