java处理binlog日志误更新操作
package com.jfb.jfbmanager;import com.github.shyiko.mysql.binlog.BinaryLogFileReader;import com.github.shyiko.mysql.binlog.event.*;import com.github.shyiko.mysql.binlog.event.deserialization.ChecksumT
package com.jfb.jfbmanager;
import com.github.shyiko.mysql.binlog.BinaryLogFileReader;
import com.github.shyiko.mysql.binlog.event.*;
import com.github.shyiko.mysql.binlog.event.deserialization.ChecksumType;
import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;
import com.jfb.jfbmanager.utils.DBMysqlUtil;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
/**
* @program:
* @description:
* @author: hqh
* @create: 2020-07-27 15:29
**/
public class test {
public static void main(String[] args) throws IOException, ParseException {
List<String> list = new ArrayList<>();
List<String> list2 = new ArrayList<>();
String str = "2020-08-06 17:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = simpleDateFormat.parse(str);
long time = parse.getTime();
String filePath="E:\\jiexinjin\\binlog日志\\binlog.000013";
File binlogFile = new File(filePath);
EventDeserializer eventDeserializer = new EventDeserializer();
eventDeserializer.setChecksumType(ChecksumType.CRC32);
BinaryLogFileReader reader = new BinaryLogFileReader(binlogFile, eventDeserializer);
try {
int i = 0;
for (Event event; (event = reader.readEvent()) != null; ) {
EventHeader header = event.getHeader();
long timestamp = header.getTimestamp();
Date date = new Date(timestamp);
//System.out.println(StringUtil.formatDate(date,"yyyy-MM-dd HH:mm:ss"));
//读取指定时间之后的更新数据
if(timestamp>time){
EventType eventType = header.getEventType();
String type = eventType.toString();
if(type.equals("EXT_UPDATE_ROWS"))
{
UpdateRowsEventData data = event.getData();
List<Map.Entry<Serializable[], Serializable[]>> rows = data.getRows();
//System.out.println(rows.size());
for(Map.Entry<Serializable[], Serializable[]> mmps:rows)
{
//String ssw = Arrays.toString(mmps.getKey());
list2.add(Arrays.toString(mmps.getKey()));
}
// List<updateRow> collect = data.getRows().stream().map(e -> new updateRow(e.getKey(), e.getValue())).collect(Collectors.toList());
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
reader.close();
}
}
public static void write(List<String> list ) throws IOException {
File txt=new File("E:\\updatelog.txt");
if(!txt.exists()){
txt.createNewFile();
}
FileOutputStream fos=new FileOutputStream(txt,true);
for(String str :list)
{
//true表示在文件末尾追加
fos.write(str.getBytes());
}
fos.close();
}
}
class updateRow{
private Serializable[] key;
private Serializable[] value;
public Serializable[] getKey() {
return key;
}
public void setKey(Serializable[] key) {
this.key = key;
}
public Serializable[] getValue() {
return value;
}
public void setValue(Serializable[] value) {
this.value = value;
}
public updateRow(){
}
public updateRow(Serializable[] key, Serializable[] value) {
this.key = key;
this.value = value;
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)