RecordConverter { public Record parse(byte[][] line) throws ParseException, UnsupportedEncodingException { if (line == null) { return null; } int cols = schema.getColumns().size(); if (isStrictSchema && line.length != cols) { throw new ParseException(Constants.ERROR_INDICATOR + "column mismatch, expected " + schema.getColumns().size() + " columns, " + line.length + " columns found, please check data or delimiter\n"); } int idx = 0; for (byte[] v : line) { if (idx >= cols && !isStrictSchema) { break; } TypeInfo typeInfo = schema.getColumn(idx).getTypeInfo(); try { r.set(idx, parseValue(typeInfo, v)); } catch (Exception e) { String val; String vStr; if (Util.isIgnoreCharset(charset)) { vStr = new String(v, Constants.REMOTE_CHARSET); } else { vStr = new String(v, charset); } if (vStr.length() > 20) { val = vStr.substring(0, 17) + "..."; } else { val = vStr; } throw new ParseException(Constants.ERROR_INDICATOR + "format error - " + ":" + (idx + 1) + ", " + typeInfo + ":'" + val + "' " + e.getMessage()); } idx++; } return r; } RecordConverter(TableSchema schema, String nullTag, String dateFormat, String tz,
                         String charset, boolean exponential);  RecordConverter(TableSchema schema, String nullTag, String dateFormat, String tz,
                         String charset, boolean exponential, boolean isStrictSchema);  RecordConverter(TableSchema schema, String nullTag, String dateFormat, String tz,
                         String charset);  RecordConverter(TableSchema schema, String nullTag, String dateFormat, String tz); byte[][] format(Record r); Record parse(byte[][] line);  }
@Test public void testNullReuse() throws Exception { TableSchema rs = new TableSchema(); rs.addColumn(new Column("i1", OdpsType.BIGINT)); String[] l0 = new String[] {"2"}; String[] l1 = new String[] {"NULL"}; String[] l2 = new String[] {"1"}; RecordConverter cv = new RecordConverter(rs, "NULL", "yyyyMMddHHmmss", null); Record r = cv.parse(toByteArray(l0)); assertEquals(r.getBigint(0), new Long(2L)); r = cv.parse(toByteArray(l1)); assertEquals(r.getBigint(0), null); r = cv.parse(toByteArray(l2)); assertEquals(r.getBigint(0), new Long(1L)); }
@Test(expected = ParseException.class) public void testNullSet() throws Exception { TableSchema rs = new TableSchema(); rs.addColumn(new Column("i1", OdpsType.BIGINT)); String[] l = new String[] {"2", "NULL"}; RecordConverter cv = new RecordConverter(rs, "NULL", "yyyyMMddHHmmss", null); Record r = cv.parse(toByteArray(l)); }
@Test public void testBigint()throws Exception{ TableSchema rs = new TableSchema(); rs.addColumn(new Column("i1", OdpsType.BIGINT)); rs.addColumn(new Column("i2", OdpsType.BIGINT)); String[] l = new String[] {"9223372036854775807", "-9223372036854775807"}; RecordConverter cv = new RecordConverter(rs, "NULL", "yyyyMMddHHmmss", null); Record r = cv.parse(toByteArray(l)); assertEquals("max value", Long.valueOf("9223372036854775807"), r.getBigint(0)); assertEquals("min value", Long.valueOf("-9223372036854775807"), r.getBigint(1)); try { l = new String[] {"9223372036854775807", "-9223372036854775808"}; r = cv.parse(toByteArray(l)); } catch (Exception e) { assertTrue("big int min value", e.getMessage().startsWith("ERROR: format error - :2, BIGINT:'-9223372036854775808'")); } try { l = new String[] {"9223372036854775808", "-9223372036854775807"}; r = cv.parse(toByteArray(l)); } catch (Exception e) { assertTrue("big int min value", e.getMessage().startsWith("ERROR: format error - :1, BIGINT:'9223372036854775808'")); } }