gbs74
gbs74

Reputation: 1

mapreduce - coding for multiple key and values

In need to emit two keys and two values from my mapper. could you please provide me info , how to write code and data type for that. for example :-

    key = { store_id : this.store_id, 
         product_id : this.product_id }; 

  value = { quantity : this.quantity, 
            price : this.price, 
            count : this.count }; 

   emit(key, value); 

regards

Upvotes: 0

Views: 849

Answers (1)

Arun A K
Arun A K

Reputation: 2225

As per the given example, A B B C A R A D S D A C A R S D F A B

From the mapper emit

key - A
value A, AB

key - B 
value B,BB

key - B
value B, BC

key - C
value C, CA

and so on...

In the reducer, you get the grouped values

key - A 
values A, AB, A, AR, A, AD, A, AC and so on

key - B 
value - B, BB,B,BC and so on

Add a delimiter of your choice between the 2 words/alphabets

for each key in reducer, you can use a hashmap/mapwritable to track the occurrence count of each value

ie for example

A - 5 times
AB - 7 times 

etc etc

Then you can calculate the ratio

Sample Mapper Implementation

public class TestMapper extends Mapper<LongWritable, Text, Text, Text> {

    @Override
    public void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        String[] valueSplits = value.toString().split(" ");
        for(int i=0;i<valueSplits.length;i++){
            if(i!=valueSplits.length-1){
                context.write(new Text(valueSplits[i]),new Text(valueSplits[i]+"~"+valueSplits[i+1]));
            }
            context.write(new Text(valueSplits[i]), new Text(valueSplits[i]));
        }
    }

}

Sample Reducer Implementation

public class TestReducer extends Reducer<Text, Text, Text, Text> {

    public void reduce(Text key, Iterable<Text> values, Context context)
            throws IOException, InterruptedException {
        Map<String,Integer> countMap= new HashMap<String,Integer>();
        for(Text t : values){
            String value = t.toString();
            int count =0;
            if(countMap.containsKey(value)){
                count = countMap.get(value);
                count+=1;
            }else{
                count =1;
            }
            countMap.put(value, count);
        }

        for(String s : countMap.keySet()){
            if(s.equalsIgnoreCase(key.toString())){

            }else{
                int keyCount = countMap.get(s.split("~")[0]);
                int occurrence = countMap.get(s);
                context.write(new Text(key.toString()+" , "+s), new Text(String.valueOf((float)occurrence/(float)keyCount)));
            }
        }
    }

}

For an input of

A A A B 

the reducer would emit

A , A~A 0.6666667
A , A~B 0.33333334

AA appears 2 times, AB 1 time and A 3 times.

AA is hence 2/3 AB is hence 1/3

Upvotes: 1

Related Questions