java-springboot-读取日志文件的多种方法

发生背景

事情是这样的,线上环境,一个同事手动执行一个admin接口,将邮寄表的物流单号给抹平了(相当于删除),业务同学是依赖物流单号取找快递单的。

所以���要把抹平的大约1000条记录找回来。

那么问题来了,怎么找回来了?

经过排查,还好,更新快递单号时是有log日志的。但是log都是整个对象的形式打印的,快递单号就在其中一个字段上。

三台机器,一共1000多条记录。

目标log:data.txt

 

ini

代码解读

复制代码

{id=21100,expressNumber=SF10000000,amount=1000} {id=21101,expressNumber=SF10000010,amount=1100} {id=21103,expressNumber=SF10030000,amount=1090} {id=21106,expressNumber=SF10005000,amount=1500} {id=21109,expressNumber=SF10200000,amount=900}

解决方案

当时想到的方案是把目标log 复制到单独的文件中,然后写程序读取。他们以json string的方式输出,可以变成json 对象来处理。

后来想想使用awk可以能快点。

由于是事后回顾,更为了多掌握些技巧,两种方案都说说。

awk

程序员必备命令。现在想想,我当时处理应该使用这种方式,最简洁,最快速的方式。

使用awk进行两次分割,拿到id和expressNumber的值,再组装成sql,批量执行update。

···

读取文件

这是相对较笨的方法,但是,它的通用的方案。java的不同版本提供了不同的文件操作类库和技巧,借此梳理出来,留作备用

NOTE: 在springboot项目中,获取要读取文件的路径后才能读取文件内容。

如文件data.txt放在了项目的classpath下

篇幅限制下面就只能给大家展示小册部分内容了。这份面试笔记包括了:Java面试、Spring、JVM、MyBatis、Redis、MySQL、并发编程、微服务、Linux、Springboot、SpringCloud、MQ、Kafka 面试专题

需要全套面试笔记【点击此处】即可免费获取

css

代码解读

复制代码

项目名称 --src/main下 ----resources ------common/data.txt

那么文件路径的获取如下

 

ini

代码解读

复制代码

// 读取一个文件:classpath下的文件 String classPath = ReadFile.class.getResource("/").getPath(); String resource = "common/data.txt"; String path = classPath + resource; return path;

一行一行读取

如文件data.txt中的内容,每行都是一个json格式的字符串。我们可以一行一行读取,转为json对象,进行业务逻辑

使用 BufferedReader 实现

代码如下

 

arduino

代码解读

复制代码

try (BufferedReader br = new BufferedReader(new FileReader(path))) { String line; while ((line = br.readLine()) != null) { // 业务逻辑处理 System.out.println(line); } }

使用 java8 实现
 

less

代码解读

复制代码

try (Stream<String> stream = Files.lines(Paths.get(path), Charset.defaultCharset())) { stream.forEach(System.out::println); }

使用 Scanner 实现
 

csharp

代码解读

复制代码

try (Scanner scanner = new Scanner(new File(path))) { while (scanner.hasNextLine()) { System.out.println(scanner.nextLine()); } }

原文代码:ReadFile.java

参考: how-can-i-read-a-large-text-file-line-by-line-using-java

一次全部读取
使用 java7 实现
 

arduino

代码解读

复制代码

try { List<String> lines = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8); for (String line : lines) { System.out.println(line); } }catch (Exception exception){}

使用 commons io 实现
 

javascript

代码解读

复制代码

<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> try { List<String> lines = FileUtils.readLines(new File(path)); for (String line : lines) { System.out.println(line); } }catch (Exception exception){}

使用 commons io 读取整个文件的字符串
 

arduino

代码解读

复制代码

try { List<String> lines = FileUtils.readLines(new File(path)); for (String line : lines) { System.out.println(line); } }catch (Exception exception){ }

原文代码: ReadFile.java

读取 MySQL 的增删改日志文件,可以使用 MySQL 的 binlog,binlog 是 MySQL 的二进制日志,记录了 MySQL 的所有更新操作,包括增删改等。 下面是使用 Spring Boot 读取 MySQL binlog 的步骤: 1. 在 MySQL 配置文件中开启 binlog,可以在 my.cnf 或 my.ini 文件中添加如下配置: ``` [mysqld] log-bin=mysql-bin binlog-format=ROW ``` 这里将 binlog 日志文件存储在名为 mysql-bin 的文件中,格式为 ROW。 2. 在 Spring Boot 中添加 MySQL 驱动和 binlog 相关的依赖,例如: ``` <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <dependency> <groupId>com.github.shyiko</groupId> <artifactId>mysql-binlog-connector-java</artifactId> <version>0.17.0</version> </dependency> ``` 这里使用了 mysql-connector-java 和 mysql-binlog-connector-java 两个依赖。 3. 在 Spring Boot 中编写读取 binlog 日志的代码,例如: ``` @Component public class BinlogReader { private final BinaryLogClient client; public BinlogReader() { client = new BinaryLogClient("localhost", 3306, "root", "password"); client.registerEventListener(event -> { EventData data = event.getData(); if (data instanceof WriteRowsEventData) { WriteRowsEventData write = (WriteRowsEventData) data; System.out.println("inserted rows: " + write.getRows()); } else if (data instanceof UpdateRowsEventData) { UpdateRowsEventData update = (UpdateRowsEventData) data; System.out.println("updated rows: " + update.getRows()); } else if (data instanceof DeleteRowsEventData) { DeleteRowsEventData delete = (DeleteRowsEventData) data; System.out.println("deleted rows: " + delete.getRows()); } }); } @PostConstruct public void start() throws IOException { client.connect(); } @PreDestroy public void stop() throws IOException { client.disconnect(); } } ``` 这里使用了 BinaryLogClient 类来连接 MySQL,通过 registerEventListener 方法注册事件监听器来监听 binlog 日志的写入、更新、删除操作。 需要注意的是,直接读取 MySQL 的 binlog 日志文件可能会对性能和稳定性造成影响,建议在使用前先进行充分测试和评估。同时,也建议使用专业的数据库同步工具来进行 MySQL 数据库的同步,如阿里云的 DTS、腾讯云的 CDC 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值