pub fn iter_records_from_file<P>(
path: P,
) -> Result<SqllogIterator<BufReader<File>>, ParseError>Expand description
从文件读取并返回 Sqllog 迭代器(流式处理)
这是一个便捷函数,从文件读取日志并返回 SqllogIterator 迭代器。
使用迭代器可以避免一次性加载所有数据到内存,适合处理大文件。
§参数
path- 日志文件路径
§返回
Ok(SqllogIterator<BufReader<File>>)- Sqllog 迭代器Err(ParseError)- 文件打开错误
§示例
use dm_database_parser_sqllog::iter_records_from_file;
let parser = iter_records_from_file("sqllog.txt")?;
let mut sqllog_count = 0;
let mut error_count = 0;
for result in parser {
match result {
Ok(sqllog) => {
sqllog_count += 1;
println!("Sqllog {}: 用户={}, SQL={}",
sqllog_count, sqllog.meta.username, sqllog.body);
}
Err(err) => {
error_count += 1;
eprintln!("错误 {}: {}", error_count, err);
}
}
}
println!("成功: {} 条, 错误: {} 个", sqllog_count, error_count);