java - why value can not be set in mapreduce -


i beginner mapreduce. want reduce process like: given set of input of same key (key1, 100), (key1, #50), (key1, #25), want output (key1, 50/100), (key1, 25/100). value of 1 '#' divided value of 1 without '#'.

because think location (key1,100) may unknown after map process. iterate through whole set (key1, 100) , store #50,#25 list. iterate through list calculate 50/100, 25/100. ok? here code:

public static class myreducer extends reducer<text,text,text,text> {         private text reskey = new text();         private text resvalue = new text();         private list<string> arr = new arraylist<string>();          public void reduce(text key, iterable<text> values,                            context context         ) throws ioexception, interruptedexception {             try {                 string prefix = new string(key.tostring());                  int total = 0;                 (text val : values) {                     string value = new string(val.tostring());                     if (!value.contains("#")) {                         total = integer.parseint(value);                     }else{                         arr.add(value);                     }                 }                  (int = 0 ; < arr.size() ; ++i) {                     string value = arr.get(i);                     if (value.contains("#")) {                         string[] words = value.split("#");                         int number = integer.parseint(words[1]);                         double probability = 1.0 * number / total;                         reskey.set(prefix);                         resvalue.set(double.tostring(probability));                         context.write(reskey, resvalue);                     }                 }                 arr.clear();             }catch (exception e)             {                 e.printstacktrace();             }         }     } 

now problem variable 'total' in second loop remains 0. think should assigned in first loop because when print in first loop not 0. in second loop becomes 0(the initial value). reason? thanks!


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -