|
01 package jay;
02
03 import static java.lang.System.out;
04
05 import java.io.InputStream;
06 import java.sql.Connection;
07 import java.sql.DriverManager;
08 import java.sql.ResultSet;
09 import java.sql.Statement;
10
11 import kyotocabinet.Cursor;
12 import kyotocabinet.DB;
13
14 public class TakeBack {
15 static Connection conn;
16 static {
17 try {
18 Class.forName("com.mysql.jdbc.Driver");
19 conn = DriverManager.getConnection(
20 "jdbc:mysql://DBServer:3306/wiki", "user", "passwd");
21 } catch (Exception e) {
22 e.printStackTrace();
23 }
24 }
25
26 static void showHashDB() {
27 DB db = new DB();
28 if (!db.open("witube.kch", DB.OWRITER | DB.OCREATE)) {
29 System.err.println("open error: " + db.error());
30 }
31
32 Cursor cur = db.cursor();
33 cur.jump();
34 String[] rec;
35 while ((rec = cur.get_str(true)) != null) {
36 System.out.println(rec[0] + ":" + rec[1]);
37 break;
38 }
39 cur.disable();
40 if (!db.close()) {
41 System.err.println("close error: " + db.error());
42 }
43 }
44
45 static void backupMediawiki() throws Exception {
46 DB db = new DB();
47 if (!db.open("witube.kch", DB.OWRITER | DB.OCREATE)) {
48 System.err.println("open error: " + db.error());
49 }
50
51 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";
52 Statement stmt = conn.createStatement();
53 ResultSet rs = stmt.executeQuery(sql);
54 while (rs.next()) {
55 InputStream in = rs.getBlob(1).getBinaryStream();
56 byte[] bytes = new byte[89200000];
57 int len = in.read(bytes);
58 String title = new String(bytes, 0, len, "UTF-8");
59 in.close();
60
61 in = rs.getBlob(2).getBinaryStream();
62 len = in.read(bytes);
63 if (len == -1) {
64 out.println("标题:" + title + " 没有内容。");
65 continue;
66 }
67 String content = new String(bytes, 0, len, "UTF-8");
68 in.close();
69 // out.println("title: " + title + " content: " + content);
70
71 db.set(title, content);
72 }
73
74 rs.close();
75 stmt.close();
76 conn.close();
77 if (!db.close()) {
78 System.err.println("close error: " + db.error());
79 }
80 }
81
82 public static void main(String[] args) throws Exception {
83 backupMediawiki();
84 showHashDB();
85 }
86 }
|
|