Skip to content

读文件之单文件有序读

quhongwei edited this page Mar 28, 2018 · 1 revision

对一个文件先排序后读取

一:文件内容

总笔数:100|总金额:300.03
流水号|基金公司订单号|订单申请时间|普通日期|普通日期时间|普通数字|金额|年龄|长整型|布尔值|备注
seq_17|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注1de2
seq_23|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注2de2
seq_12|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注3de2
seq_80|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注4de2
seq_77|inst_seq_77|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注5de2
seq_56|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注6de2
seq_55|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注7de2
seq_33|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注8de2
seq_7|inst_seq_0|2013-11-09 12:34:56|20131109|20131112 12:23:34|23.33|10.22|22|12345|true|备注9de2

二:数据定义模板

{
 	"head":[
	     "totalCount|总笔数|Required|BigDecimal",
	     "totalAmount|总金额|BigDecimal|Required"
 	],
 	"body":[
	     "seq|流水号",
	     "instSeq|基金公司订单号|Required",
	     "gmtApply|订单申请时间|Date:yyyy-MM-dd HH:mm:ss",
	     "date|普通日期|Date:yyyyMMdd",
	     "dateTime|普通日期时间|Date:yyyyMMdd HH:mm:ss",
	     "applyNumber|普通数字|BigDecimal",
	     "amount|金额|BigDecimal",
	     "age|年龄|Integer",
	     "longN|长整型|Long",
	     "bol|布尔值|Boolean",
	     "memo|备注"
    ],
    "protocol":"DE",
    "lineBreak":"\r\n",
    "summaryColumnPairs":[
    	"totalAmount|amount"
    ]
}

三: 代码示例

        // filepath指定了要排序文件的路径
        FileConfig fileConfig = new FileConfig(filepath,"/sortedreader/de/de.json", new StorageConfig("nas"));
        // 多文件排序类型设置
        fileConfig.setType(FileCoreToolContants.PROTOCOL_MULTI_FILE_SORTER);
        // 创建有序读reader
        ProtocolFilesSortedReader reader = (ProtocolFilesSortedReader) FileFactory
            .createReader(fileConfig);
        FileSorter fileSorter = (FileSorter) reader;
        // 分片不合并
        SortConfig sortConfig = new SortConfig(sortTempPath, SortTypeEnum.ASC, executor,
            ResultFileTypeEnum.SLICE_FILE_PATH);
        sortConfig.setSliceSize(256);
        sortConfig.setSortIndexes(new int[] { 0, 1 });  // 指定排序索引
        // 先排序
        fileSorter.sort(sortConfig);
        // 读取数据
        Map<String, Object> head = reader.readHead(HashMap.class);
        System.out.println(head);
        Assert.assertEquals(new BigDecimal("300.03"), head.get("totalAmount"));
       // 结果验证
        String[] sortedSeq = new String[] { "seq_12", "seq_17", "seq_23", "seq_33", "seq_55",
                                            "seq_56", "seq_7", "seq_77", "seq_80" };
        Map<String, Object> row = null;
        int i = 0;
        while (null != (row = reader.readRow(HashMap.class))) {
            Assert.assertEquals(sortedSeq[i++], row.get("seq"));
        }
Clone this wiki locally