Skip to content

Commit c101de7

Browse files
author
hongwei.quhw
committed
kv多模板
1 parent cabc2cc commit c101de7

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

rdf-file-core/src/test/java/com/alipay/rdf/file/codec/RowNosqlKVCodecWriterTest.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,107 @@ public void testKVSplit() throws Exception{
389389
fileReader.close();
390390
}
391391

392+
// 多模板测试
393+
@Test
394+
public void testMultiTemlate() throws Exception {
395+
String filePath = tf.getRoot().getAbsolutePath();
396+
FileConfig config = new FileConfig(new File(filePath, "test.txt").getAbsolutePath(),
397+
"/codec/kv/template/template7.json", new StorageConfig("nas"));
398+
399+
FileWriter fileWriter = FileFactory.createWriter(config);
400+
401+
Map<String, Object> head = new HashMap<String, Object>();
402+
head.put("totalCount", 2L);
403+
head.put("totalAmount", new BigDecimal("23.22"));
404+
fileWriter.writeHead(head);
405+
406+
Map<String, Object> body = new HashMap<String, Object>();
407+
body.put("seq", "seq12345");
408+
body.put("instSeq", "303");
409+
body.put("bol", true);
410+
body.put("memo", "memo1");
411+
fileWriter.writeRow(body);
412+
413+
body = new HashMap<String, Object>();
414+
body.put("seq", "seq14345");
415+
body.put("amount", new BigDecimal("1.09"));
416+
body.put("age", 33);
417+
body.put("longN", 125L);
418+
body.put("bol", false);
419+
body.put("memo", "memo2");
420+
fileWriter.writeRow(body);
421+
422+
body = new HashMap<String, Object>();
423+
body.put("seq", "seq4521");
424+
body.put("instSeq", "505");
425+
body.put("gmtApply", DateUtil.parse("2016-02-03 12:22:33", "yyyy-MM-dd HH:mm:ss"));
426+
body.put("date", DateUtil.parse("20160203", "yyyyMMdd"));
427+
body.put("dateTime", DateUtil.parse("20160203 12:22:33", "yyyyMMdd HH:mm:ss"));
428+
body.put("applyNumber", 12);
429+
body.put("amount", new BigDecimal("1.09"));
430+
body.put("age", 33);
431+
body.put("longN", 67L);
432+
body.put("bol", false);
433+
body.put("memo", "memo2");
434+
fileWriter.writeRow(body);
435+
436+
fileWriter.close();
437+
438+
//校验文件
439+
BufferedReader reader = new BufferedReader(
440+
new InputStreamReader(new FileInputStream(new File(config.getFilePath())), "UTF-8"));
441+
Assert.assertEquals("totalCount:2|totalAmount:23.22", reader.readLine());
442+
Assert.assertEquals("seq:seq12345|instSeq:303|bol:true|memo:memo1", reader.readLine());
443+
Assert.assertEquals("seq:seq14345|amount:1.09|age:33|longN:125|bol:false|memo:memo2", reader.readLine());
444+
Assert.assertEquals("seq:seq4521|instSeq:505|gmtApply:2016-02-03 12:22:33|date:20160203|dateTime:20160203 12:22:33|applyNumber:12|amount:1.09|age:33|longN:67|bol:false|memo:memo2", reader.readLine());
445+
Assert.assertNull(reader.readLine());
446+
447+
try {
448+
449+
FileReader fileReader = FileFactory.createReader(config);
450+
451+
head = fileReader.readHead(HashMap.class);
452+
Assert.assertEquals(new Long(2), head.get("totalCount"));
453+
Assert.assertEquals(new BigDecimal("23.22"), head.get("totalAmount"));
454+
455+
Map<String, Object> row = fileReader.readRow(HashMap.class);
456+
Assert.assertEquals("seq12345", row.get("seq"));
457+
Assert.assertEquals("303", row.get("instSeq"));
458+
Assert.assertEquals(true, row.get("bol"));
459+
Assert.assertEquals("memo1", row.get("memo"));
460+
461+
row = fileReader.readRow(HashMap.class);
462+
Assert.assertEquals("seq14345", row.get("seq"));
463+
Assert.assertEquals(new BigDecimal("1.09"), row.get("amount"));
464+
Assert.assertEquals(33, row.get("age"));
465+
Assert.assertEquals(new Long(125), row.get("longN"));
466+
Assert.assertEquals(false, row.get("bol"));
467+
Assert.assertEquals("memo2", row.get("memo"));
468+
469+
row = fileReader.readRow(HashMap.class);
470+
Assert.assertEquals("seq4521", row.get("seq"));
471+
Assert.assertEquals("505", row.get("instSeq"));
472+
Assert.assertEquals("2016-02-03 12:22:33",
473+
DateUtil.format((Date) row.get("gmtApply"), "yyyy-MM-dd HH:mm:ss"));
474+
Assert.assertEquals("20160203", DateUtil.format((Date) row.get("date"), "yyyyMMdd"));
475+
Assert.assertEquals("20160203 12:22:33",
476+
DateUtil.format((Date) row.get("dateTime"), "yyyyMMdd HH:mm:ss"));
477+
Assert.assertEquals(12, row.get("applyNumber"));
478+
Assert.assertEquals(new BigDecimal("1.09"), row.get("amount"));
479+
Assert.assertEquals(33, row.get("age"));
480+
Assert.assertEquals(new Long(67), row.get("longN"));
481+
Assert.assertEquals(false, row.get("bol"));
482+
Assert.assertEquals("memo2", row.get("memo"));
483+
484+
Assert.assertNull(fileReader.readRow(HashMap.class));
485+
486+
fileReader.close();
487+
Assert.fail();
488+
} catch (ArrayIndexOutOfBoundsException e) {
489+
// 多模板不应该使用nosql模型(kv), 要支持也可以, RdfFileRowConditionSpi 行路由要自己实现, 目前内置MatchRowCondition, 不支持
490+
}
491+
}
492+
392493
@After
393494
public void after() {
394495
tf.delete();

rdf-file-core/src/test/java/com/alipay/rdf/file/codec/RowRelationCodecTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public void test6() throws Exception {
285285

286286
}
287287

288+
// 多模板
288289
@Test
289290
public void test7() throws Exception {
290291
String filePath = File.class.getResource("/codec/relation/data/data5.txt").getPath();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"head":[
3+
"totalCount|总笔数|Required|Long",
4+
"totalAmount|总金额|BigDecimal|Required"
5+
],
6+
"multiBodys":[
7+
{
8+
"name":"columnTemplate1",
9+
"condition":"bol=true",
10+
"bodyColumns":[
11+
"seq|流水号",
12+
"instSeq|基金公司订单号|Required",
13+
"bol|布尔值|Boolean",
14+
"memo|备注",
15+
"newCol1|新字段"
16+
]
17+
}, {
18+
"name":"columnTemplate2",
19+
"condition":"match:bol=false|seq(3,5)=14|age=33",
20+
"bodyColumns":[
21+
"seq|流水号",
22+
"amount|金额|BigDecimal",
23+
"age|年龄|Integer",
24+
"longN|长整型|Long",
25+
"bol|布尔值|Boolean",
26+
"memo|备注",
27+
"newCol1|新字段"
28+
]
29+
}, {
30+
"name":"columnTemplate3",
31+
"condition":"callback:com.alipay.rdf.file.multiBodyTempalte.BizCallbackRowCondition",
32+
"bodyColumns":[
33+
"seq|流水号",
34+
"instSeq|基金公司订单号|Required",
35+
"gmtApply|订单申请时间|Date:yyyy-MM-dd HH:mm:ss",
36+
"date|普通日期|Date:yyyyMMdd",
37+
"dateTime|普通日期时间|Date:yyyyMMdd HH:mm:ss",
38+
"applyNumber|普通数字|Integer",
39+
"amount|金额|BigDecimal",
40+
"age|年龄|Integer",
41+
"longN|长整型|Long",
42+
"bol|布尔值|Boolean",
43+
"memo|备注",
44+
"newCol1|新字段"
45+
]
46+
}
47+
],
48+
"protocol":"sp",
49+
"rowCodecMode":"kv"
50+
}

0 commit comments

Comments
 (0)