!数据指标统计-直播时长Top
map阶段获取id和时长,reduce后cleanup函数对数据map集合进行排序
public class VideoInfoTop10Map extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(){
//Todo
}
}
public class VideoInfoTop10Reduce extends Reducer<Text, LongWritable, Text, LongWritable>{
HashMap<String, Long> map = new HashMap<>();
@Override
protected void reduce(){
//TODO
map.put(k2.toSrting(),lengthsum);
}
//reduce结束后执行
@Override
protected void cleanup(Context context){
//配置类中获取dt日期参数
Configuration conf = context.getConfiguration();
String dt = conf.get("dt");
//排序
Map<String,Long> sortedMap = MapUtils.sortValue(map);
Set<Map.Entry<String,Long>> entries = sortedMap.entrySet();
Iterator<Map.Entry<String, Long>> it = entries.iterator();
int count=1;
while(count<=10 && it.hasNext()){
Map.Entry<String, Long> entry = it.next();
String key = entry.getKey();
Long value = entry.getValue();
//封装k3,v3
Text k3 = new Text();
k3.set(key);
LongWritable v3 = new LongWritable();
v3.set(value);
context.write(k3,v3);
count++;
}
}
}
public class VideoInfoTop10Job{
public static void main(String[] args){
//从输入路径获取日期
String[] fields = args[0].split("/");
String tmpdt= fields[fields.length -1];
String dt = DataUtils.transDataFormat(tmpdt);
conf.set("dt",dt);
//因为context中存放conf信息↑
//Todo
}
}