从日志文件中筛选相关信息

前景介绍

将消息存储到 tableStore 失败后,会将数据的相关信息打印到日志文件中,相关信息中包含消息的 id 等信息。我们需要拿到这个日志文件,将存储失败的数据进行重新存储,因此需要拿到从日志文件中的拿到“payload”这个 key 对应的 value 进行消息的重新存储。

如何从一个日志文件中,找到key 对应的 value?

一、先将日志文件进行导入

IO 类的相关知识

链接如下:链接: link

// 利用字节流进行文本出
 File file = new File("/Users/hzh/a.txt");
 String s = brLine(file);
 // 找到分隔符,进行分割后,找到数组中对应的下标位置
 String[] split = s.split("\\|");
 System.out.println(split[13]);
 String json = split[13];
 
public static String brLine(File file) throws Exception {
      // BufferedReader和 FileReader 都是采用了缓存区的,因此 BufferedReader 对于性能的提升不是很大
      // 运用到 BufferedReader 和 BufferedWriter 主要是用他们的 readLine()和 nextLine()方法
      BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
      String s = bufferedReader.readLine();
      return s;
  }

二、从中筛选出key 对应的 value

String 类的相关知识

参考 JDK 中 String 相关方法

// 方法一:	
// 将 String 转换为 json 对象,然后根据 key 取出。
// 这个的 JsonObject 是 fastjson 包
JSONObject jsonObject = JSONObject.parseObject(json);
String payload = (String)jsonObject.get("opDesc");
System.out.println(payload);
//方法二:
//直接找到对应的下标
String substring = json.substring(27, 37);
System.out.println(substring);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值