0

In the CSV file that I use for loading ActivePivot, I have 2 fields that need to be multiplied together to compute my record's value: price * quantity.

I am using the CSV source with topics and channels. Where can I perform this computation?

Jack
  • 145
  • 1
  • 1
  • 11

1 Answers1

0

you should override the compute method of the ColumnParser, see below. In the following example we get the QuantitySold and the SellingPricePerUnit and we add the result in the Sales column, do not forget to add the Sales column in your store definition:

    @Bean
@DependsOn(value="csvSource")
public CSVMessageChannelFactory csvChannelFactory() {
    CSVMessageChannelFactory channelFactory = new CSVMessageChannelFactory(csvSource(), datastore);

    channelFactory.setCalculatedColumns(ORDERS_TOPIC, DatastoreConfig.ORDERS, Arrays.<IColumnCalculator>asList(
            //derive new fields
            new ColumnParser("Sales", "double"){
                @Override
                public Object compute(IColumnCalculationContext context) {
                    Long qty =  (Long) context.getValue("QuantitySold");
                    Double price = (Double) context.getValue("SellingPricePerUnit");
                    return  (qty == null || price == null) ? null: qty*price;
                }
            }
            )); 

    return channelFactory;  }
tuxmobil
  • 238
  • 3
  • 10
  • This will work, but why subclassing a "column parser"? There is no parsing involved in the calculation of the column. You can simply extend the abstract class "com.qfs.msg.csv.translator.impl.AColumnCalculator" for that use case. – Antoine CHAMBILLE Sep 03 '14 at 09:50