(一)、yum安装wiredtiger(2.6.1支持mongodb.3.4及以下版本,如果要解析更高版本的mongodb journal日志,wiredtiger也需要升到高版本),安装后查看结果如下:(也可用python或c)
(二)、用java进行解析,需要建立相应的软连接(java.library.path)
(三)、导入wiredtiger.jar(linux安装wiredtiger后会自动生成wiredtiger.jar,用find / -name wiredtiger.jar查找)到java项目中,准备写代码
(四)、写代码,然后导出jar包到linux环境下运行:
package com.my;
import com.wiredtiger.db.Connection;
import com.wiredtiger.db.Cursor;
import com.wiredtiger.db.Session;
import com.wiredtiger.db.wiredtiger;
class Lsn {
int file;
long offset;
}
public class wired {
static void
print_record(Lsn lsn, int opcount,
int rectype, int optype, long txnid, int fileid,
byte[] key, byte[] value)
{
System.out.print(
"LSN [" + lsn.file + "][" + lsn.offset + "]." + opcount +
": record type " + rectype + " optype " + optype +
" txnid " + txnid + " fileid " + fileid);
System.out.println(" key size " + key.length +
" value size " + value.length);
if(key.length != 0) {
System.out.println("key -> "+ new String(key));
System.out.println("value -> "+ new String(value));
}
if (rectype == wiredtiger.WT_LOGREC_MESSAGE)
System.out.println("Application Record: " + new String(value));
}
// 可用
/*public static void main(String[] args) {
Connection conn = null;
Session s;
Cursor c;
try {
conn = wiredtiger.open("mongo", "create,log=(enabled=true,archive=true,path=journal,compressor=snappy)");
s = conn.open_session(null);
c = s.open_cursor("log:", null, null);
while(c.next() == 0) {
System.out.println("Got: " + c.getKeyInt());
}
} catch (Exception e) {
System.err.println(""+e);
} finally {
conn.close(null);
}
}*/
static void log() {
Connection conn = null;
Session s;
Cursor c;
Lsn lsn = new Lsn();
byte[] logrec_key, logrec_value;
long txnid;
int fileid, opcount, optype, rectype;
try {
conn = wiredtiger.open("mongo", "create,log=(enabled=true,path=journal,compressor=snappy,recover=on)");
s = conn.open_session(null);
// s.create("log:", "key_format=U,value_format=U");
c = s.open_cursor("log:", null, null);
while(c.next() == 0) {
lsn.file = c.getKeyInt();
lsn.offset = c.getKeyLong();
opcount = c.getKeyInt();
txnid = c.getValueLong();
rectype = c.getValueInt();
optype = c.getValueInt();
fileid = c.getValueInt();
logrec_key = c.getValueByteArray();
logrec_value = c.getValueByteArray();
print_record(lsn, opcount,
rectype, optype, txnid, fileid, logrec_key, logrec_value);
}
} catch (Exception e) {
System.err.println(""+e);
} finally {
conn.close(null);
}
}
public static void main(String[] args) {
log();
}
}
(五)、linux环境下执行,执行完后解析journal日志的结果保存到1.log中