先看一下要处理的数据类型
19392963501,17816115082,2018-09-18 16:19:44,1431
14081946321,13094566759,2018-05-23 09:34:27,0610
13415701165,18939575060,2018-11-23 21:33:23,1031
15590483587,16303009156,2018-08-02 07:38:00,0487
15539613975,17882324598,2018-10-19 09:08:15,0948
数据字段分别为主叫号码,被叫号码,通话时间,通话时长
我们的需求是:将数据按号码的通话日期的年,月,日分别计算时长和次数,用一个MapReduce实现,在这里我处理的时候是忽略被叫号码的通话时长,仅以主叫号码为例。
代码部分
package mapReducePhone;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class PhoneNum {
public static void main(String[] args) throws Exception{
//获取连接
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
//设置要运行的 jar
job.setJarByClass(PhoneNum.class);
//指定map和reduce
job.setMapperClass(PhoneMapper.class);
job.setReducerClass(PhoneReducer.class);
//设置map输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//设置reduce 输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//设置分区与reduce task 的数量
job.setPartitionerClass(PhonePartitioner.class);
job.setNumReduceTasks(3);
//设置输入输出路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//答应结果是否成功
boolean result = job.waitForCompletion(true);
System.out.println(result);
}
}
//数据格式 17026053728,17816115082,2108-03-28 11:09:19,1792
class PhoneMapper extends Mapper<LongWritable,Text,Text,IntWritable>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// Text k = new Text();
String[] split = value.toString().split(",");
String caller =split[0];
// String callee =split[1];
// String date =split[2];
int statetime =Integer.parseInt(split[3]);
String yearData = split[2].replaceAll("-", "").substring(0, 4);
String monthData = split[2].replaceAll("-", "").substring(0, 6);
String dayData = split[2].replaceAll("-", "").substring(0,9);
//仅计算主叫号码的通话记录 若需求被叫号码则将被叫号码提取出来进行同样操作
context.write(new Text(caller+"-"+dayData),new IntWritable(statetime));
context.write(new Text(caller+"-"+yearData),new IntWritable(statetime));
context.write(new Text(caller+"-"+monthData),new IntWritable(statetime));
}
}
class PhoneReducer extends Reducer<Text,IntWritable,Text,Text> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
int i =0;
for(IntWritable value :values){
sum += value.get();
i++;
}
context.write(new Text(key),new Text(sum+" 次数"+ i));
}
}
class PhonePartitioner<K, V> extends Partitioner<K, V>{
@Override
//自定义partition的数量需要和reduce task数量保持一致
public int getPartition(K key, V value, int numPartitions) {
// TODO Auto-generated method stub
//根据key的长度进行分区
int datelongth=key.toString().length();
switch(datelongth)
{
case 16 :
return 0;
case 18 :
return 1;
}
return 2;
}
}