用kyotocabinet备份MediaWiki数据

package jay;

import static java.lang.System.out;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import kyotocabinet.Cursor;
import kyotocabinet.DB;

public class TakeBack {
	static Connection conn;
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://DBServer:3306/wiki", "user", "passwd");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static void showHashDB() {
		DB db = new DB();
		if (!db.open("witube.kch", DB.OWRITER | DB.OCREATE)) {
			System.err.println("open error: " + db.error());
		}

		Cursor cur = db.cursor();
		cur.jump();
		String[] rec;
		while ((rec = cur.get_str(true)) != null) {
			System.out.println(rec[0] + ":" + rec[1]);
			break;
		}
		cur.disable();
		if (!db.close()) {
			System.err.println("close error: " + db.error());
		}
	}

	static void backupMediawiki() throws Exception {
		DB db = new DB();
		if (!db.open("witube.kch", DB.OWRITER | DB.OCREATE)) {
			System.err.println("open error: " + db.error());
		}

		String sql = "SELECT page_title, old_text FROM `page` p, revision r, text t WHERE page_latest = r.rev_id and r.rev_text_id = t.old_id and page_namespace = 0";
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()) {
			InputStream in = rs.getBlob(1).getBinaryStream();
			byte[] bytes = new byte[89200000];
			int len = in.read(bytes);
			String title = new String(bytes, 0, len, "UTF-8");
			in.close();

			in = rs.getBlob(2).getBinaryStream();
			len = in.read(bytes);
			if (len == -1) {
				out.println("标题:" + title + " 没有内容。");
				continue;
			}
			String content = new String(bytes, 0, len, "UTF-8");
			in.close();
			// out.println("title: " + title + " content: " + content);

			db.set(title, content);
		}

		rs.close();
		stmt.close();
		conn.close();
		if (!db.close()) {
			System.err.println("close error: " + db.error());
		}
	}

	public static void main(String[] args) throws Exception {
		backupMediawiki();
		showHashDB();
	}
}

你可能感兴趣的:(用kyotocabinet备份MediaWiki数据)