用java编程解析mongodb3.4下journal日志

(一)、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中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值