zoukankan      html  css  js  c++  java
  • java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】

    java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】

    说明如下,不懂的问题直接我【费元星Q9715234】

    1.反射的意义在于不将xml tag写死在sax 的代码中,直接配置到一个bean就可以了。

    2.多线程分工明确,线程各做各的任务,生产者只用了一个线程,使用多线程没有意义,瓶颈在IO

    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 }

    类列表

    1 BeanListHandler
    2 CompareThread
    3 ImportDataUtil
    4 ReadXmlThread
    5 WriteFileUtil
    6 XmlColumn
    7 XmlData
    8 XmlSAXParse
  • 相关阅读:
    xml转义字符在mybatis动态sql中的使用
    jdbc类型与java类型
    aop日志(记录方法调用日志)
    mysql数据库关联查询【lert join】常见使用
    maven项目基本配置
    mapper文件的参数传入与获取
    idea新建项目出现push rejected如何解决
    快速从2个List集合中找出相同/不同元素
    Windows 环境下安装RocketMQ
    RabbitMQ java客户端集成 Spring 开发环境
  • 原文地址:https://www.cnblogs.com/feiyuanxing/p/7270064.html
Copyright © 2011-2022 走看看