java 管理MYSQL 二进制日志 binlog

本文介绍了一个Java程序,用于读取MySQL二进制日志文件的最后一行,并将其转换为文本格式。程序进一步解析文本文件,提取最后一条操作SQL语句,支持INSERT、UPDATE和DELETE语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MysqlLog {

	public static final String folder_name = "D:\\mysql_log\\";
	public static final String file_index_name = "D:\\mysql_log\\binary_log.index";
	public static final String file_txt_name = "D:\\mysql_log\\binary_log.txt";
	public static final String mysql_binlog_exe = "D:\\mysql_log\\mysqlbinlog.exe";
	public static final String insert = "insert";
	public static final String update = "update";
	public static final String delete = "delete from";

	public static void main(String[] args) throws Exception {

		String lastFileName = readLastLine(file_index_name, "utf-8");
		System.out.println(lastFileName);
		convertToTxt(lastFileName);
		readContent(new File(file_txt_name), "utf-8");
		String actionSql = lastActionSql(file_txt_name, "utf-8");
		System.out.println(actionSql);
	}
	
	public static boolean isExistActionSql(final String content,final String... actionSqls){
		for(String actionSql : actionSqls){
			Pattern pattern = Pattern.compile(actionSql);
			Matcher matcher = pattern.matcher(content);
			if(matcher.find()){
				return true;
			}
		}
		return false;
	}
	
	public static String lastActionSql(String fileName,String charset){
		String actionSql = "";
		RandomAccessFile rf = null;
		try {
			rf = new RandomAccessFile(fileName, "r");
			long len = rf.length();
			long start = rf.getFilePointer();
			long nextend = start + len - 1;
			String line;
			rf.seek(nextend);
			int c = -1;
			while (nextend > start) {
				c = rf.read();
				if (c == '\n' || c == '\r') {
					line = rf.readLine();
					
					if (line != null) {
						if(isExistActionSql(line,insert,update,delete)){
							actionSql = new String(line.getBytes("ISO-8859-1"),
									charset);
							return actionSql;
						}
					}
					nextend--;
				}
				nextend--;
				rf.seek(nextend);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rf != null)
					rf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return actionSql;
	}

	public static String readContent(File file, String charset) {
		StringBuffer sb = new StringBuffer();
		InputStreamReader inputStreamReader = null;
		BufferedReader bufferedReader = null;
		try {
			inputStreamReader = new InputStreamReader(
					new FileInputStream(file), charset);
			bufferedReader = new BufferedReader(inputStreamReader);
			String content = "";
			while ((content = bufferedReader.readLine()) != null) {
				System.out.println(content);
			}
			System.out.println(sb.toString());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				inputStreamReader.close();
				bufferedReader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	public static void convertToTxt(String fileName) {
		Runtime runtime = Runtime.getRuntime();
		String cmd = "cmd.exe /c " + mysql_binlog_exe + " " + fileName + " > "
				+ file_txt_name;
		try {
			runtime.exec(cmd);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String readLastLine(String filename, String charset) {
		String lastFileName = "";
		RandomAccessFile rf = null;
		try {
			rf = new RandomAccessFile(filename, "r");
			long len = rf.length();
			long start = rf.getFilePointer();
			long nextend = start + len - 1;
			String line;
			rf.seek(nextend);
			int c = -1;
			while (nextend > start) {
				c = rf.read();
				if (c == '\n' || c == '\r') {
					line = rf.readLine();
					if (line != null) {
						lastFileName = new String(line.getBytes("ISO-8859-1"),
								charset);
						return lastFileName;
					}
					nextend--;
				}
				nextend--;
				rf.seek(nextend);
				// if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行
				// lastFileName += new
				// String(rf.readLine().getBytes("ISO-8859-1"), charset);
				// }
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rf != null)
					rf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return lastFileName;
	}

	public String getTime() {

		Calendar now = Calendar.getInstance(); // get the current time!
		String time, mon, dd, hh, mm, sec, mes;
		// format the time!
		int month = now.get(Calendar.MONTH) + 1; // 月份必须加1,比如是12月,get返回的是11
		int day = now.get(Calendar.DAY_OF_MONTH);
		int hour = now.get(Calendar.HOUR_OF_DAY);
		int minute = now.get(Calendar.MINUTE);
		int second = now.get(Calendar.SECOND);
		int mesl = now.get(Calendar.MILLISECOND);

		/*
		 * 位数不足前面补0
		 */
		if (month < 10)
			mon = "0" + month;
		else
			mon = "" + month;
		if (day < 10)
			dd = "0" + day;
		else
			dd = "" + day;
		if (hour < 10)
			hh = "0" + hour;
		else
			hh = "" + hour;
		if (minute < 10)
			mm = "0" + minute;
		else
			mm = "" + minute;
		if (second < 10)
			sec = "0" + second;
		else
			sec = "" + second;
		if (mesl < 10)
			mes = "00" + mesl;
		else if (mesl < 100)
			mes = "0" + mesl;
		else
			mes = "" + mesl;
		time = now.get(Calendar.YEAR) + mon + dd + hh + mm + sec + mes;
		return time;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值