java+反射+多線程+生產者消費者模式+讀取xml(SAX)入數據庫mysql-【費元星Q9715234】

java+反射+多線程+生產者消費者模式+讀取xml(SAX)入數據庫mysql-【費元星Q9715234】java

說明以下,不懂的問題直接我【費元星Q9715234】mysql

1.反射的意義在於不將xml tag寫死在sax 的代碼中,直接配置到一個bean就能夠了。spring

2.多線程分工明確,線程各作各的任務,生產者只用了一個線程,使用多線程沒有意義,瓶頸在IOsql

3.SAX讀取超大文件的性能較好,dom等均不佳。數據庫

4.總的瓶頸在DB的入庫,能夠使用insert ,好一點使用spring 預編譯,保證全數據的入庫。我採用的是mysql 的load,性能提高8倍左右,缺點是沒有對數據中的阿拉伯文等語種作處理,多線程

 

 

 

 1 /**
 2  * <a> 讀xml的管 理類 </a>
 3  *
 4  * @author: feiyuanxing@baidu.com
 5  * @time 2016/12/27
 6  */
 7 public class ReadXmlManger {
 8     private static final Logger logger = LoggerFactory.getLogger(ReadXmlManger.class);
 9 
10     public static String insertTempFile;
11     public static String updateTempFile;
12 
13     /**
14      * 各類字符型的入庫 尚未作
15      *
16      * @param args
17      */
18     public static void main(String[] args) {
19         Long starTime = System.currentTimeMillis();
20         String needAnalysisFileDir = "D:\\creditcode\\xml\\";
21         insertTempFile = "D:\\creditcode\\tmp\\tmpInsert.sql";
22         updateTempFile = "D:\\\\creditcode\\\\tmp\\\\tmpDelete.sql";
23 
24         if (args.length == 3) {
25             needAnalysisFileDir = args[0];
26             insertTempFile = args[1];
27             updateTempFile = args[2];
28             System.out.println("argument is right:" + args.length + Arrays.toString(args));
29         }
30         // else {
31         //            System.out.println("argument is not right:" + args.length + Arrays.toString(args) + " ,over...");
32         //            System.exit(1);
33         //            return;
34         //
35         //        }
36         // 對各類鏈接作初始化
37         ImportDataUtil.getConnet();
38         // 加載文件路徑到隊列中
39         ImportDataUtil.getXmlDirToQueue(needAnalysisFileDir);
40         // 執行將數據中的md5 列加載獲得內存中
41         ImportDataUtil.getSourecXmlList();
42 
43         // 建立初始化流
44         ImportDataUtil.createWriter(insertTempFile, updateTempFile);
45         logger.info("begin compare...");
46         // 讀xml到隊列中
47         XmlData.pool.execute(new ReadXmlThread(CreditCode.class));
48         // 從隊列中取值比較完後寫到txt中
49         XmlData.pool.execute(new CompareThread(CreditCode.class));
50         // 等待全部子線程執行完
51         while (true) {
52             if (XmlData.pool.isShutdown()) {
53                 // 將txt中的數據load到數據庫中
54                 loadData();
55                 Long endTime = System.currentTimeMillis();
56                 logger.info("compare over ,all read " + XmlData.atomicIntegerSC.get() + " ;");
57                 logger.info("Import the data complete, takes " + (endTime - starTime) / 1000 + " seconds;");
58                 return;
59             }
60         }
61     }
62 
63     // 都執行完執行
64     public static void loadData() {
65         // 將最後的新增長數據load到source文件中
66         ImportDataUtil.dumpInsertBuilder2File();
67 
68         // 將txt文件load到數據中  false未啓用
69         CreditCode creditCode = null;
70         logger.info("Start the load data for the last time....");
71         logger.info("The mission were increased: " + XmlData.currentAddNum.get() + " tiao data");
72         ImportDataUtil.loadFile(insertTempFile, CreditCode.class);
73 
74         // 清空md5表
75         ImportDataUtil.clearTmpTable();
76         // 將集合中剩餘的md5寫到文件中
77         logger.info("Began to set in the rest of the md5 written to a file ");
78         ImportDataUtil.dumpUpdateBuilder2File();
79         // load進數據 and 級聯刪除數據
80         logger.info("Delete data load and start cascading delete data...");
81         ImportDataUtil.sourceFile(updateTempFile);
82         XmlData.context.close();
83 
84     }
85 
86 }

 

類列表dom

1 BeanListHandler
2 CompareThread
3 ImportDataUtil
4 ReadXmlThread
5 WriteFileUtil
6 XmlColumn
7 XmlData
8 XmlSAXParse
相關文章
相關標籤/搜索