Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //DurchschnittCombiner
- @Override
- public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
- double sum=0;
- ArrayList <Double> savedValues = new ArrayList<Double>();
- String oldKey = "";
- String oldValue = "";
- double total = 0;
- for(Text x: values){
- if(oldKey.equals(key.toString())){
- for (Double dbl: savedValues) {
- total += dbl;
- }
- sum = (total + Double.parseDouble(x.toString()))/(savedValues.size()+1);//arr.le
- total = 0;
- } else {
- sum+=Double.parseDouble(x.toString());
- savedValues.clear();
- }
- oldKey = key.toString();
- oldValue = x.toString();
- savedValues.add(Double.parseDouble(oldValue));
- }
- String toDbl = String.valueOf(sum);
- context.write(key, new Text(toDbl));
- }
- //DurchschnittMapper
- @Override
- public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
- String line = value.toString();
- StringTokenizer tokenizer = new StringTokenizer(line, "\n");
- while (tokenizer.hasMoreTokens()) {
- String str = tokenizer.nextToken();
- String [] col = str.split("\\|");
- String id = col[1];
- value.set(id);
- String amount = col[4];
- context.write(value, new Text(amount));
- }
- }
- //DurchscnittReducer
- public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
- double sum=0;
- ArrayList<Double> savedValues = new ArrayList<Double>();
- String oldKey = "";
- String oldValue = "";
- double total = 0;
- for(Text x: values){
- if(oldKey.equals(key.toString())){
- for (Double dbl: savedValues) {
- total += dbl;
- }
- sum = (total + Double.parseDouble(x.toString()))/(savedValues.size()+1);//arr.le
- total = 0;
- } else {
- sum+=Double.parseDouble(x.toString());
- savedValues.clear();
- }
- oldKey = key.toString();
- oldValue = x.toString();
- savedValues.add(Double.parseDouble(oldValue));
- }
- String toDbl = String.valueOf(sum);
- context.write(key, new DoubleWritable(sum));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement