1.项目需求:
统计每个手机号上行流量和、下行流量和、总流量和(上
行流量和+下行流量和),并且:将统计结果按照手机号的前缀
进行区分,并输出到不同的输出文件中去。
13* ==> …
15* ==> …
other ==> …
其中,access.log数据文件
第二个字段:手机号
倒数第三个字段:上行流量
倒数第二个字段:下行流量
2.项目总体思路
根据手机号进行分组,然后把该手机号对应的上下行流量加起来
(1)Mapper: 把手机号、上行流量、下行流量拆开
把手机号作为key,把Access作为value写出去
(2)Reducer形如:(“手机号”,<Access,Access>)
(3)自定义分区类(需要继承Partitioner抽象类),并覆写
getPartition()方法
3.开发步骤:
(1)自定义Access类
包括属性:手机号、上行流量、下行流量、总流量
(2)自定义Map任务类(Map Task)
对每一行日志内容进行拆分,Map输出数据为:
phone==>Access(手机号,该行手机号的上行流量,该行手机号的
下行流量)
(3)编写Reduce任务类(Reduce Task)
对每个手机号的流量进行汇总,Map输出数据为:
phone==>Access(手机号,上行流量和,下行流量和)
也可以优化为:
phone==>Access(NullWritable对象,上行流量和,下行流量和)
(4)编写分区处理类
继承org.apache.hadoop.mapreduce.Partitioner
类,"13"开头的手机号交给第一个ReduceTask任务处理,最终
输出到0号分区,"15"开头的手机号交给第二个ReduceTask任
务处理,最终输出到1号分区,其余手机号交给第三个
ReduceTask任务处理,最终输出到2号分区。
4.代码:
下面是代码目录。
并导入hadoop相关的jar包
// acess类;
Access类
package org.mapreduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.net.URI;
public class FlowHDFSApp {
public static void main(String[] args) throws Exception{
System.setProperty("HADOOP_USER_NAME", "root");
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://192.168.10.104:9000");
// 创建一个Job
Job job = Job.getInstance(configuration);
// 设置Job对应的参数: 主类
job.setJarByClass(FlowHDFSApp.class);
// 设置Job对应的参数: 设置自定义的Mapper和Reducer处理类
job.setMapperClass(FlowMapper.class);
job.setReducerClass(FlowReducer.class);
// 添加Combiner的设置即可
job.setCombinerClass(FlowReducer.class);
// 设置Job对应的参数: Mapper输出key和value的类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 设置Job对应的参数: Reduce输出key和value的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 如果输出目录已经存在,则先删除
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.10.131:9000"),configuration, "root");
Path outputPath = new Path("/wordcount/output");
if(fileSystem.exists(outputPath)) {
fileSystem.delete(outputPath,true);
}
// 设置Job对应的参数: Mapper输出key和value的类型:作业输入和输出的路径
FileInputFormat.setInputPaths(job, new Path("/home/alice/桌面"));
FileOutputFormat.setOutputPath(job, outputPath);
// 提交job
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : -1);
}
};
FlowMapper类
package org.mapreduce;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class FlowMapper extends Mapper<LongWritable, Text, Text, Access>{
Text k=new Text();
Access v=new Access();
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line=value.toString();
String[] fields=line.split("\t");
String phNum=fields[1];
long upFlow=Long.parseLong(fields[fields.length-3]);
long downFlow=Long.parseLong(fields[fields.length-2]);
k.set(phNum);
v.set(upFlow,downFlow);
context.write(k, v);
}
}
FlowReduce类
package org.mapreduce;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class FlowReducer extends Reducer<Text, Access, Text, Access>{
@Override
protected void reduce(Text key, Iterable<Access> values, Context context)
throws IOException, InterruptedException {
long sumUpFlow=0;
long sumDownFlow=0;
System.out.println(values);
for (Access access : values) {
sumUpFlow+= access.getUpflow();
sumDownFlow+= access.getDownflow();
}
Access v=new Access(sumUpFlow,sumDownFlow);
context.write(key, v);
}
}
FlowDriver类
package org.mapreduce;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
public class FlowDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
if (args.length < 2) {
System.err.println("Usage: FlowDriver <inputPath> <outputPath>");
System.exit(1);
}
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration, "Flow Calculation");
job.setJarByClass(FlowDriver.class);
job.setMapperClass(FlowMapper.class);
job.setReducerClass(FlowReducer.class);
job.setPartitionerClass(PhonePartitioner.class);
job.setNumReduceTasks(3);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Access.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Access.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
PhonePartitioner类
package org.mapreduce;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
public class PhonePartitioner extends Partitioner<Text, Access> {
@Override
public int getPartition(Text key, Access value, int numPartitions) {
String phonePrefix = key.toString().substring(0, 2);
switch (phonePrefix) {
case "13":
return 0;
case "15":
return 1;
default:
return 2;
}
}
}
5.结果
运行FlowDriver
运行FlowHDFSApp
![请添加图片描述](https://img-blog.csdnimg.cn/direct/0f1e35abb1b74616a1668d49e20f85bb.png
这里不影响结果上传到HDFS
启动集群,在HDFS中可以看到:
![请添加图片描述](https://img-blog.csdnimg.cn/direct/d362eaba55d74153b45f1775a08b8d4c.png
"13"开头的手机号最终输出到0号分区,四个数字对应:手机号,对应手机号的上行流量,下行流量,总流量
"15"开头的手机号最终输出到1号分区
其他开头的手机号最终输出到2号分区
详细见https://gitee.com/xie-xinlan/test/tree/master/