第一个项目——Day1 手机流量统计项目
手机流量统计项目可以帮助用户跟踪和管理其手机的数据使用情况。下面是一个简单的手机流量统计项目的示例:
1.项目概述
Hadoop是一个开源的分布式计算框架,主要用于处理大规模数据集。在Hadoop中进行手机流量日志分析项目,主要是为了收集、处理和分析移动设备产生的海量网络流量数据。
2.项目需求
统计每个手机号上行流量和、下行流量和、总流量和(上
行流量和+下行流量和),并且:将统计结果按照手机号的前缀
进行区分,并输出到不同的输出文件中去。
13* ==> …
15* ==> …
other ==> …
其中,access.log数据文件
第二个字段:手机号
倒数第三个字段:上行流量
倒数第二个字段:下行流量
日志内容access如下图所示:
项目思路
1.根据手机号进行分组,然后把该手机号对应的上下行流量加
起来
2.Mapper: 把手机号、上行流量、下行流量拆开
把手机号作为key,把Access作为value写出去
3.Reducer形如:(“手机号”,<Access,Access>)
4.自定义分区类(需要继承Partitioner抽象类),并覆写
getPartition()方法
开发步骤
(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号分区
代码
代码结构如下
Access类
// An highlighted block
package org.mapreduce;
import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
//1实现writable方法
public class Access implements Writable{
private long upflow;
private long downflow;
private long sumflow;
//必须要有空参构造,为了以后反射用
public Access() {
super();
}
public Access(long upflow, long downflow) {
super();
this.upflow = upflow;
this.downflow = downflow;
this.sumflow = upflow+downflow;
}
public void set(long upflow, long downflow) {
this.upflow = upflow;
this.downflow = downflow;
this.sumflow = upflow+downflow;
}
//序列化的方法
@Override
public void write(DataOutput out) throws IOException {
out.writeLong(upflow);
out.writeLong(downflow);
out.writeLong(sumflow);
//反序列化方法
//注意序列化方法和反序列化方法顺序必须保持一致
}
@Override
public void readFields(DataInput in) throws IOException {
this.upflow=in.readLong();
this.downflow=in.readLong();
this.sumflow=in.readLong();
}
@Override
public String toString() {
return upflow + "\t" + downflow + "\t" + sumflow;
}
public void setUpflow(long upflow) {
this.upflow = upflow;
}
public long getUpflow() {
return upflow;
}
public long getDownflow() {
return downflow;
}
public void setDownflow(long downflow) {
this.downflow = downflow;
}
public long getSumflow() {
return sumflow;
}
public void setSumflow(long sumflow) {
this.sumflow = sumflow;
}
}
FlowDriver 类
// An highlighted block
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.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);
}
}
FlowMapper类
// An highlighted block
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);
}
}
FlowReducer类
// An highlighted block
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 flowBean: values) {
sumUpFlow+=flowBean.getUpflow();
sumDownFlow+=flowBean.getDownflow();
}
Access v=new Access(sumUpFlow,sumDownFlow);
context.write(key, v);
}
}
PhonePartitioner类
// An highlighted block
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;
}
}
}
FlowHDFSApp类
// An highlighted block
package org.bigdata.wc;
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.88.104:9000");
// 创建一个Job
Job job = Job.getInstance(configuration);
// 设置Job对应的参数: 主类
job.setJarByClass(FlowHDFSApp.class);
// 设置Job对应的参数: 设置自定义的Mapper和Reducer处理类
job.setMapperClass(WCMapper.class);
job.setReducerClass(WCReducer.class);
// 添加Combiner的设置即可
job.setCombinerClass(WCReducer.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("/wordcount/input"));
FileOutputFormat.setOutputPath(job, outputPath);
// 提交job
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : -1);
}
}
启动集群:
结果
将结果上传到hdfs中: 通过192.168.10.131访问
“13"开头的手机号最终输出到0号分区
“15"开头的手机号最终输出到1号分区
其余手机号最终输出到2号分区
详细见git