Skip to content

写文件之汇总写

斩秋 edited this page Sep 7, 2019 · 2 revisions

一:数据定义模板

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

二:协议布局模板

SP组件内置协议

三:代码示例

FileConfig config = new FileConfig(new File(filePath, "test.txt").getAbsolutePath(),"/writer/template/sp_summary.json", new StorageConfig("nas"));
config.setSummaryEnable(true); //启动汇总功能
FileWriter fileWriter = FileFactory.createWriter(config);
try {
       // 头使用数据定义模板的常量
        Map<String, Object> head = new HashMap<String, Object>();
        fileWriter.writeHead(head);
        // 写入两条数据
        Map<String, Object> body = new HashMap<String, Object>();
        Date testDate = DateUtil.parse("2017-01-03 12:22:33", "yyyy-MM-dd HH:mm:ss")
        body.put("seq", "seq12345");
        body.put("instSeq", "303");
        body.put("gmtApply", testDate);
        body.put("date", testDate);
        body.put("dateTime", testDate);
        body.put("applyNumber", 12);
        body.put("amount", new BigDecimal("1.22"));
        body.put("age", new Integer(33));
        body.put("longN", new Long(33));
        body.put("bol", true);
        body.put("memo", "memo1");
        fileWriter.writeRow(body);
        testDate = DateUtil.parse("2016-02-03 12:22:33", "yyyy-MM-dd HH:mm:ss");
        body.put("seq", "seq234567");
        body.put("instSeq", "505");
        body.put("gmtApply", testDate);
        body.put("date", testDate);
        body.put("dateTime", testDate);
        body.put("applyNumber", 12);
        body.put("amount", new BigDecimal("1.09"));
        body.put("age", 66);
        body.put("longN", 125);
        body.put("bol", false);
        body.put("memo", "memo2");
        fileWriter.writeRow(body);

        // 根据汇总信息写入尾部
        fileWriter.writeTail(fileWriter.getSummary().summaryTailToMap());
} finally {
        fileWriter.close();
}

四:生成的文件内容

汇总文件测试
seq12345|303|2017-01-03 12:22:33|20170103|20170103 12:22:33|12|1.22|33|33|true|memo1
seq234567|505|2016-02-03 12:22:33|20160203|20160203 12:22:33|12|1.09|66|125|false|memo2
2|2.31
Clone this wiki locally