根据公司需要使用mapreduce实现用户一天内使用总时长,代码如下:
package com.online.time;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class UserOnlineTime {
// mapper
public static class OnlineMapper extends Mapper{
private Text info = new Text();
private Text otherInfo = new Text();
// 数据输入格式:手机号(空格)数据访问时间(空格)动作(空格)访问的页面
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String[] userInfo = value.toString().split(" ");
String phone = userInfo[1];
String other = timeToSecond(userInfo[0]) + "-" + userInfo[2] + "-" + userInfo[3];
info.set(phone);
otherInfo.set(other);
// map的输出为:<手机号, 数据访问时间-动作标识(共有两个动作:In-进入页面,Out-退出页面)-访问的页面>
context.write(info, otherInfo);
}
}
// reduce
public static class OnlineReduce extends Reducer{
private Text result = new Text();
// 获取昨天日期
String yesterday = getYesterday();
public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException{
List list = new ArrayList();
for (Text val : values) {
list.add(val.toString());
}
// 将集合转换为数组
String[] valueArray = (String[]) list.toArray(new String[list.size()]);
String[] resultInfo = sortReduceValue(valueArray);
// 36666-In-HouseController
int resultTime = 0;
for(int i = 0; i < resultInfo.length; i++){
String[] tls = resultInfo[i].split("-");
if("In".equals(tls[1])){
// 数据已按照时间升序排序
int inTime = Integer.parseInt(tls[0]);
String inLocal = tls[2];
for(int j = i + 1; j < resultInfo.length; j++){
String[] ols = resultInfo[j].split("-");
String outLocal = ols[2];
// 判断是否是同一页面
if(inLocal.equals(outLocal)){
if("Out".equals(ols[1])){
int outTime = Integer.parseInt(ols[0]);
// 在本页面停留的时间
int stayTime = outTime - inTime;
// 在本页面停留时间大于10分钟,则抛弃此数据
if(stayTime < 600){
resultTime = resultTime + stayTime;
break;
}else{
break;
}
}else{
break;
}
}
}
}
}
if(resultTime == 0){
resultTime = 1;
}
// 数据分隔符为 Tab键,方便交给hive管理
result.set(String.valueOf(resultTime) + " " + yesterday);
context.write(key, result);
}
}
// 将时间转换为秒
public static int timeToSecond(String time){
int second = 0;
if(time != null && !"".equals(time)){
String[] times = time.split(":");
int hourSecond = Integer.parseInt(times[1]) * 60 * 60;
int minuteSecond = Integer.parseInt(times[2]) * 60;
second = hourSecond + minuteSecond + Integer.parseInt(times[3]);
}
return second;
}
// 对reduce数据按照时间从小到大排序(冒泡排序)
public static String[] sortReduceValue(String[] values){
for (int i = 0; i < values.length -1; i++){
for(int j = 0 ;j < values.length - i - 1; j++){
if(Integer.parseInt(values[j].split("-")[0]) > Integer.parseInt(values[j + 1].split("-")[0])){
String temp = values[j];
values[j] = values[j + 1];
values[j + 1] = temp;
}
}
}
return values;
}
// 获取昨天的日期
public static String getYesterday(){
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE,-1);
Date time=cal.getTime();
String yesterday = new SimpleDateFormat("yyyyMMdd").format(time);
return yesterday;
}
// main函数
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if(otherArgs.length != 2){
System.err.println("Usage: wordcount ");
System.exit(2);
}
Job job = new Job(conf, "User Online Job");
job.setNumReduceTasks(1);
job.setJarByClass(UserOnlineTime.class);
job.setMapperClass(OnlineMapper.class);
job.setReducerClass(OnlineReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}