@Override public Boolean convertToAvro(String value) { return ParserUtils.parseToBoolean(value); } }
@Override public Integer convertToAvro(String value) { // String to Byte (datum type), then Byte (datum type to Integer (Avro-compatible types) // Because of migration issue, decode flag is always 'true' for byte type return StringUtils.isEmpty(value) ? null : ParserUtils.parseToByte(ParserUtils.transformNumberString(value, thousandsSepChar, decimalSepChar), true) .intValue(); } }
@Override public String convertToAvro(String value) { // String to Character (datum type), then Character (datum type to String (Avro-compatible types) return StringUtils.isEmpty(value) ? null : ParserUtils.parseToCharacter(value).toString(); } }
@Override public Long convertToAvro(String value) { return StringUtils.isEmpty(value) ? null : ParserUtils.parseToLong(ParserUtils.transformNumberString(value, thousandsSepChar, decimalSepChar), isDecode()); } }
@Override public Integer convertToAvro(String value) { // String to Integer (datum type), then Integer (datum type to Integer (Avro-compatible types) return StringUtils.isEmpty(value) ? null : ParserUtils .parseToShort(ParserUtils.transformNumberString(value, thousandsSepChar, decimalSepChar), isDecode()) .intValue(); } }
@Override public Integer convertToAvro(String value) { return StringUtils.isEmpty(value) ? null : ParserUtils.parseToInteger(ParserUtils.transformNumberString(value, thousandsSepChar, decimalSepChar), isDecode()); } }
@Override public Double convertToAvro(String value) { return StringUtils.isEmpty(value) ? null : Double.parseDouble(ParserUtils.transformNumberString(value, thousandsSepChar, decimalSepChar)); } }
@Override public Long convertToAvro(String value) { Date date = null; if (!StringUtils.isEmpty(value)) { date = ParserUtils.parseToDate(value, pattern, !isLenient); } if (date != null) { return date.getTime(); } else { return null; } }
@Test public void testParseToLong() throws Exception { assertNull(ParserUtils.parseToLong(null, false)); assertThat(10L, equalTo(ParserUtils.parseToLong("010", false))); assertThat(8L, equalTo(ParserUtils.parseToLong("010", true))); assertThat(10L, equalTo(ParserUtils.parseToLong("10", false))); assertThat(10L, equalTo(ParserUtils.parseToLong("10", true))); try { ParserUtils.parseToLong("0X10", false); fail("Except get NumberFormatException"); } catch (Exception e) { assertEquals(NumberFormatException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } assertThat(16L, equalTo(ParserUtils.parseToLong("0X10", true))); assertThat(74565L, equalTo(ParserUtils.parseToLong("0X12345", true))); }
@Test public void testParseToInteger() throws Exception { assertNull(ParserUtils.parseToInteger(null, true)); assertThat(10, equalTo(ParserUtils.parseToInteger("010", false))); assertThat(8, equalTo(ParserUtils.parseToInteger("010", true))); assertThat(10, equalTo(ParserUtils.parseToInteger("10", false))); assertThat(10, equalTo(ParserUtils.parseToInteger("10", true))); try { ParserUtils.parseToInteger("0X10", false); fail("Except get NumberFormatException"); } catch (Exception e) { assertEquals(NumberFormatException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } assertThat(16, equalTo(ParserUtils.parseToInteger("0X10", true))); assertThat(74565, equalTo(ParserUtils.parseToInteger("0X12345", true))); }
@Test public void testParseToByte() throws Exception { assertNull(ParserUtils.parseToByte(null, true)); assertEquals(Byte.valueOf("10"), ParserUtils.parseToByte("010", false)); assertEquals(Byte.valueOf("8"), ParserUtils.parseToByte("010", true)); assertEquals(Byte.valueOf("10"), ParserUtils.parseToByte("10", false)); assertEquals(Byte.valueOf("10"), ParserUtils.parseToByte("10", true)); try { ParserUtils.parseToByte("0X10", false); fail("Except get NumberFormatException"); } catch (Exception e) { assertEquals(NumberFormatException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } assertEquals(Byte.valueOf("16"), ParserUtils.parseToByte("0X10", true)); try { ParserUtils.parseToByte("0X123", true); } catch (Exception e) { assertEquals(NumberFormatException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } }
@Test public void testParseToShort() throws Exception { assertNull(ParserUtils.parseToShort(null, false)); assertEquals(Short.valueOf("10"), ParserUtils.parseToShort("010", false)); assertEquals(Short.valueOf("8"), ParserUtils.parseToShort("010", true)); assertEquals(Short.valueOf("10"), ParserUtils.parseToShort("10", false)); assertEquals(Short.valueOf("10"), ParserUtils.parseToShort("10", true)); try { ParserUtils.parseToShort("0X10", false); fail("Except get NumberFormatException"); } catch (Exception e) { assertEquals(NumberFormatException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } assertEquals(Short.valueOf("16"), ParserUtils.parseToShort("0X10", true)); try { ParserUtils.parseToShort("0X12345", true); } catch (Exception e) { assertEquals(NumberFormatException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } }
@Test public void testParseListToString() throws Exception { assertNull(ParserUtils.parseListToString(null, ",")); List<String> list = new ArrayList<String>(); list.add("e1"); list.add("e2"); list.add("e3"); list.add("e4"); assertEquals("[e1,e2,e3,e4]", ParserUtils.parseListToString(list, ",")); }
@Test public void testParseToDouble() throws Exception { assertNull(ParserUtils.parseToDouble(null)); assertNull(ParserUtils.parseToDouble(null)); assertThat(12.54, equalTo(ParserUtils.parseToDouble("12.54"))); assertThat(12.5111, equalTo(ParserUtils.parseToDouble("12.5111"))); }
@Override public Float convertToAvro(String value) { return StringUtils.isEmpty(value) ? null : Float.parseFloat(ParserUtils.transformNumberString(value, thousandsSepChar, decimalSepChar)); } }
@Test public void testParseToDate() throws Exception { // Parse date lenient. The wrong month number would be ignore Date date1 = ParserUtils.parseToDate("1988-02-30", "yyyy-MM-dd", true); assertEquals("1988-03-01", FormatterUtils.formatDate(date1, "yyyy-MM-dd")); try { // Parse date strict. "1988-20" with wrong month number would throw a exception ParserUtils.parseToDate("1988-20", "yyyy-MM", false); fail("Except get exception: Unparseable date: \"1988-20\""); } catch (Exception e) { assertEquals(RuntimeException.class, e.getClass()); LOG.debug("Except exception:" + e.getMessage()); } Date date2 = ParserUtils.parseToDate("1987-06-21", "yyyy-MM-dd", true); assertEquals("1987-06-21", FormatterUtils.formatDate(date2, "yyyy-MM-dd")); Date date3 = ParserUtils.parseToDate("1987-06-21 12:21:22", "yyyy-MM-dd hh:mm:ss", true); assertEquals("1987-06-21 12:21:22", FormatterUtils.formatDate(date3, "yyyy-MM-dd hh:mm:ss")); Date date4 = ParserUtils.parseToDate("1987-06-21 12:21:22.123", "yyyy-MM-dd hh:mm:ss.SSS", true); assertEquals("1987-06-21 12:21:22.123", FormatterUtils.formatDate(date4, "yyyy-MM-dd hh:mm:ss.SSS")); }
@Override public String convertToAvro(String value) { return StringUtils.isEmpty(value) ? null : new BigDecimal(ParserUtils.transformNumberString(value, thousandsSepChar, decimalSepChar)).toPlainString(); } }
NumberConverter(Schema.Field field, FileDelimitedProperties properties) { this.field = field; this.properties = properties; if (properties.advancedSeparator.getValue()) { this.thousandsSepChar = ParserUtils.parseToCharacter(properties.thousandsSeparator.getValue()); this.decimalSepChar = ParserUtils.parseToCharacter(properties.decimalSeparator.getValue()); } if ((properties instanceof TFileInputDelimitedProperties) && ((TFileInputDelimitedProperties) properties).enableDecode.getValue()) { Object values = ((TFileInputDelimitedProperties) properties).decodeTable.decode.getValue(); if (values != null && values instanceof List) { decodeList = (List<Boolean>) values; } } }
@Test public void testParseToBoolean() throws Exception { assertNull(ParserUtils.parseToBoolean(null)); // "1" would return true assertTrue(ParserUtils.parseToBoolean("1")); // Others flow to the boolean parser assertFalse(ParserUtils.parseToBoolean("0")); assertTrue(ParserUtils.parseToBoolean("true")); assertFalse(ParserUtils.parseToBoolean("false")); assertTrue(ParserUtils.parseToBoolean("TRUE")); assertFalse(ParserUtils.parseToBoolean("FALSE")); assertFalse(ParserUtils.parseToBoolean("a")); }
@Test public void testTransformNumberString() throws Exception { // Transform xx[thousandsSeparator]xxx[thousandsSeparator]xxx[decimalSeparator]xx to xxxxxxxx.xx assertEquals("123456789.123", ParserUtils.transformNumberString("123,456,789.123", ',', '.')); assertEquals("123456789.123", ParserUtils.transformNumberString("123.456.789,123", '.', ',')); }