Skip to Content
author's profile photo Former Member
Former Member

Udf for addition

Hi

I will get the values in a field where i need to,

1. add if the value has duplication(ex: 18 occurs twice - add it and get 36)

2. in addition to 36, find the maximum of values in the queues and pass to target..

Regards

Jansi

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Sep 14, 2016 at 02:56 PM

    Hi Jansi,

    Can you give us an example?

    Regards,

    Praveen.

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Jansirani,

      Mapping:

      UDF:

      Execution Type : All Value Of Queue

      public void sumByKey(String[] key, String[] value, ResultList keyResult, ResultList valueResult, Container container)
        throws StreamTransformationException {
        double qty = 0, valueDouble = 0;
        boolean isFirst = true;
        Map<String, Double> map = new HashMap<String, Double>();
        for (int i = 0; i < key.length; i++) {
        if (!key[i].equals(ResultList.CC)) {
        valueDouble = Double.parseDouble(value[i]);
        qty = (map.containsKey(key[i])) ? map.get(key[i]) + valueDouble : valueDouble;
        map.put(key[i], qty);
        }
        }
        for (Map.Entry<String, Double> mapEntry : map.entrySet()) {
        if (isFirst)
        isFirst = false;
        else {
        keyResult.addContextChange();
        valueResult.addContextChange();
        }
        keyResult.addValue(mapEntry.getKey());
        valueResult.addValue(mapEntry.getValue());
        }
        }
      

      Test:

      Regards,

      Praveen.

      mapping.png (17.5 kB)
      test1.png (33.1 kB)

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.