@Override public String convertToDatum(T value) { if (value == null) { return null; } if (thousandsSepChar != null || decimalSepChar != null) { return FormatterUtils.formatNumber(new BigDecimal(String.valueOf(value)).toPlainString(), thousandsSepChar, decimalSepChar); } else { if (value instanceof BigDecimal) { String precision = field.getProp(SchemaConstants.TALEND_COLUMN_PRECISION); if (precision != null) { return ((BigDecimal) value).setScale(Integer.valueOf(precision), RoundingMode.HALF_UP).toPlainString(); } else { return ((BigDecimal) value).toPlainString(); } } else if (AvroUtils.isSameType(AvroUtils._decimal(), AvroUtils.unwrapIfNullable(field.schema()))) { String precision = field.getProp(SchemaConstants.TALEND_COLUMN_PRECISION); if (precision != null) { return new BigDecimal(String.valueOf(value)).setScale(Integer.valueOf(precision), RoundingMode.HALF_UP) .toPlainString(); } } return String.valueOf(value); } }
/** * Checks {@link TalendType#convertFromAvro(Schema)} converts string avro type with java-class flag "java.math.BigDecimal" to * "id_BigDecimal" di type */ @Test public void testConvertFromAvroBigDecimal() { TalendType expectedType = TalendType.BIG_DECIMAL; Schema fieldSchema = AvroUtils._decimal(); assertEquals(expectedType, TalendType.convertFromAvro(fieldSchema)); }
/** * Checks {@link AvroTypeConverter#convertToAvro(String, String)} returns String avro schema with * "java-class"=java.math.BigDecimal in case TalendType.BIG_DECIMAL Talend type is passed */ @Test public void testConvertToAvroBigDecimal() { Schema expectedSchema = AvroUtils._decimal(); assertEquals(expectedSchema, AvroTypeConverter.convertToAvro(TalendType.BIG_DECIMAL, null)); }
/** * A helper method to convert the String representation of a datum in the DataPrep system to the Avro type that * matches the Schema generated for it. * * @param f is field in Avro Schema. * @return converter for a given type. */ public AvroConverter<String, ?> getConverterFromString(org.apache.avro.Schema.Field f) { Schema fieldSchema = AvroUtils.unwrapIfNullable(f.schema()); // FIXME use avro type to decide the converter is not correct if the user change the avro type, Date to String // for instance if (AvroUtils.isSameType(fieldSchema, AvroUtils._boolean())) { return new StringToBooleanConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._decimal())) { return new StringToDecimalConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._double())) { return new StringToDoubleConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._int())) { return new StringToIntegerConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._date())) { return new StringToDateConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._string())) { return super.getConverter(String.class); } throw new UnsupportedOperationException("The type " + fieldSchema.getType() + " is not supported."); //$NON-NLS-1$ //$NON-NLS-2$ }
return AvroUtils._character(); case BIG_DECIMAL: return AvroUtils._decimal(); case DATE: return AvroUtils._logicalTimestamp();
/** * A helper method to convert the String representation of a datum in the Salesforce system to the Avro type that * matches the Schema generated for it. * * @param f * @return */ public AvroConverter<String, ?> getConverterFromString(org.apache.avro.Schema.Field f) { Schema fieldSchema = AvroUtils.unwrapIfNullable(f.schema()); // FIXME use avro type to decide the converter is not correct if the user change the avro type, Date to String // for instance if (AvroUtils.isSameType(fieldSchema, AvroUtils._boolean())) { return new StringToBooleanConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._decimal())) { return new StringToDecimalConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._double())) { return new StringToDoubleConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._int())) { return new StringToIntegerConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._date())) { return new StringToDateConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._bytes())) { return new StringToBytesConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._string())) { return super.getConverter(String.class); } throw new UnsupportedOperationException("The type " + fieldSchema.getType() + " is not supported."); //$NON-NLS-1$ //$NON-NLS-2$ }
return AvroUtils._character(); case BIG_DECIMAL: return AvroUtils._decimal(); case DATE: return AvroUtils._logicalTimestamp();
return AvroUtils._character(); case BIG_DECIMAL: return AvroUtils._decimal(); case DATE: return AvroUtils._date();
return AvroUtils._character(); case BIG_DECIMAL: return AvroUtils._decimal(); case DATE: return AvroUtils._date();
@Test public void testWriteDecimal() throws Throwable { String resources = getResourceFolder(); String outputFile = resources + "/out/test_write_decimal.csv"; String refFilePath = resources + "/ref_test_write_decimal.csv"; LOGGER.debug("Test file path: " + outputFile); TFileOutputDelimitedProperties properties = createOutputProperties(outputFile, false); Schema outputSchema = SchemaBuilder.builder().record("Schema").fields().name("TestBigDecimal") .prop(SchemaConstants.TALEND_COLUMN_PRECISION, "10").prop(SchemaConstants.TALEND_COLUMN_PRECISION, "2") .type(AvroUtils._decimal()).noDefault().endRecord(); properties.main.schema.setValue(outputSchema); List<IndexedRecord> records = new ArrayList<>(); IndexedRecord r1 = new GenericData.Record(outputSchema); r1.put(0, "3.1415926"); IndexedRecord r2 = new GenericData.Record(outputSchema); r2.put(0, "9.1798"); records.add(r1); records.add(r2); // Delete generated empty file function not be checked doWriteRows(properties, records); assertTrue(FileRuntimeHelper.compareInTextMode(outputFile, refFilePath, getEncoding(properties.encoding))); assertTrue(deleteFile(outputFile)); }
break; case currency: base = AvroUtils._decimal(); break; case date:
break; case currency: base = AvroUtils._decimal(); break; case date:
} else if (AvroUtils.isSameType(fieldSchema, AvroUtils._decimal())) { return new StringToDecimalConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._double())) {
public AvroConverter<String, ?> getConverter(Schema.Field f, FileDelimitedProperties properties) { Schema fieldSchema = AvroUtils.unwrapIfNullable(f.schema()); if (AvroUtils.isSameType(fieldSchema, AvroUtils._boolean())) { return new BooleanConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._decimal())) { return new DecimalConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._double())) { return new DoubleConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._float())) { return new FloatConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._int())) { return new IntegerConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._date())) { return new DateConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._long())) { return new LongConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._bytes())) { return new BytesConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._byte())) { return new ByteConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._short())) { return new ShortConverter(f, properties); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._character())) { return new CharacterConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._string())) { return super.getConverter(String.class); } throw new UnsupportedOperationException("The type " + fieldSchema.getType() + " is not supported."); //$NON-NLS-1$ //$NON-NLS-2$ }
} else if (AvroUtils.isSameType(fieldSchema, AvroUtils._decimal())) { return new StringToDecimalConverter(f); } else if (AvroUtils.isSameType(fieldSchema, AvroUtils._double())) {
} else if (AvroUtils.isSameType(basicSchema, AvroUtils._date())) { return Types.DATE; } else if (AvroUtils.isSameType(basicSchema, AvroUtils._decimal())) { return Types.DECIMAL; } else if (AvroUtils.isSameType(basicSchema, AvroUtils._long())) {
} else if (AvroUtils.isSameType(basicSchema, AvroUtils._date())) { return Types.DATE; } else if (AvroUtils.isSameType(basicSchema, AvroUtils._decimal())) { return Types.DECIMAL; } else if (AvroUtils.isSameType(basicSchema, AvroUtils._long())) {
public static DTEConverter createConverter(final Field f, final String mappedName) { Schema basicSchema = AvroUtils.unwrapIfNullable(f.schema()); AzureStorageDTEConverters converters = new AzureStorageDTEConverters(); DTEConverter dteConverter; if (AvroUtils.isSameType(basicSchema, AvroUtils._string()) || AvroUtils.isSameType(basicSchema, AvroUtils._character())) { dteConverter = converters.new StringDTEConverter(f, mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._int()) || AvroUtils.isSameType(basicSchema, AvroUtils._short()) || AvroUtils.isSameType(basicSchema, AvroUtils._byte())) { dteConverter = converters.new IntegerDTEConverter(mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._date())) { dteConverter = converters.new DateDTEConverter(f, mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._decimal()) || AvroUtils.isSameType(basicSchema, AvroUtils._double()) || AvroUtils.isSameType(basicSchema, AvroUtils._float())) { dteConverter = converters.new DoubleDTEConverter(mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._long())) { dteConverter = converters.new LongDTEConverter(f, mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._boolean())) { dteConverter = converters.new BooleanDTEConverter(mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._bytes())) { dteConverter = converters.new ByteArrayDTEConverter(mappedName); } else { dteConverter = converters.new StringDTEConverter(f, mappedName); } return dteConverter; }
public static DTEConverter createConverter(final Field f, final String mappedName) { Schema basicSchema = AvroUtils.unwrapIfNullable(f.schema()); AzureStorageDTEConverters converters = new AzureStorageDTEConverters(); DTEConverter dteConverter; if (AvroUtils.isSameType(basicSchema, AvroUtils._string()) || AvroUtils.isSameType(basicSchema, AvroUtils._character())) { dteConverter = converters.new StringDTEConverter(f, mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._int()) || AvroUtils.isSameType(basicSchema, AvroUtils._short()) || AvroUtils.isSameType(basicSchema, AvroUtils._byte())) { dteConverter = converters.new IntegerDTEConverter(mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._date())) { dteConverter = converters.new DateDTEConverter(f, mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._decimal()) || AvroUtils.isSameType(basicSchema, AvroUtils._double()) || AvroUtils.isSameType(basicSchema, AvroUtils._float())) { dteConverter = converters.new DoubleDTEConverter(mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._long())) { dteConverter = converters.new LongDTEConverter(f, mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._boolean())) { dteConverter = converters.new BooleanDTEConverter(mappedName); } else if (AvroUtils.isSameType(basicSchema, AvroUtils._bytes())) { dteConverter = converters.new ByteArrayDTEConverter(mappedName); } else { dteConverter = converters.new StringDTEConverter(f, mappedName); } return dteConverter; }
public static Schema getBasicSchema(String pattern) { return SchemaBuilder.builder().record("Schema").fields() // .name("TestBoolean").type().booleanType().noDefault() // .name("TestByte").type(AvroUtils._byte()).noDefault() // .name("TestBytes").type(AvroUtils._bytes()).noDefault() // .name("TestChar").type(AvroUtils._character()).noDefault() // .name("TestDate").prop(SchemaConstants.TALEND_COLUMN_PATTERN, pattern)// .type(AvroUtils._date()).noDefault() // .name("TestDouble").type().doubleType().noDefault() // .name("TestFloat").type().floatType().noDefault() // .name("TestBigDecimal").type(AvroUtils._decimal()).noDefault()// .name("TestInteger").type().intType().noDefault() // .name("TestLong").type().longType().noDefault() // .name("TestObject").type(AvroUtils._bytes()).noDefault().endRecord(); }