需要从我的mapper中发出两个键和两个值。你能不能给我提供信息,如何编写代码和数据类型。例如: -
key = { store_id : this.store_id,
product_id : this.product_id };
value = { quantity : this.quantity,
price : this.price,
count : this.count };
emit(key, value);
问候
答案 0 :(得分:1)
根据给定的例子,A B B C A R A D S D A C A R S D F A B
从mapper发射
key - A
value A, AB
key - B
value B,BB
key - B
value B, BC
key - C
value C, CA
依旧......
在reducer中,您将获得分组值
key - A
values A, AB, A, AR, A, AD, A, AC and so on
key - B
value - B, BB,B,BC and so on
在2个单词/字母
之间添加您选择的分隔符对于reducer中的每个键,您可以使用hashmap / mapwritable来跟踪每个值的出现次数
即例如
A - 5 times
AB - 7 times
等等
然后你可以计算比率
示例映射器实现
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]));
}
}
}
示例缩减器实现
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)));
}
}
}
}
输入
A A A B
reducer会发出
A , A~A 0.6666667
A , A~B 0.33333334
AA出现2次,AB 1次,A 3次。
因此,AA是2/3 因此AB为1/3