Hadoop工程师面试题(1)--MapReduce实现单表汇总统计
数据源格式描述:
输入t1.txt源数据,数据文件分隔符”*&*”,字段说明如下:
| 字段序号 | 字段英文名称 | 字段中文名称 | 字段类型 | 字段长度 |
| 1 | TIME_ID | 时间(到时) | 字符型 | 12 |
| 2 | Session | 会话时长 | 数值型 | 8 |
| 3 | MSISDN | 用户号码 | 字符型 | 11 |
| 4 | SP_DOMAIN | SP域名 | 数值型 | 64 |
| 5 | USER_AGENT_ORIGN | 终端字串 | 字符型 | 128 |
| 6 | USER_AGENT | 终端类别 | 字符型 | 64 |
| 7 | UPSTREAM_VOL | 上行流量 | 数值型 | 8 |
| 8 | DOWNSTREAM_VOL | 下行流量 | 数值型 | 8 |
| 9 | URL_CNT | 访问次数 | 数值型 | 20 |
用mapreduce实现单表汇总:
在数据源的基础上,根据终端类型汇总出总流量及访问次数。汇总模型字段说明如下:
| 字段序号 | 字段英文名称 | 字段中文名称 | 字段类型 | 字段长度 |
| 1 | USER_AGENT | 终端类型 | 字符型 | |
| 2 | TOT_FLUX | 总流量 | 数值型 | 30 |
| 3 | URL_CNT | 访问次数 | 数值型 | 30 |
代码如下:
package mianshi;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;import com.google.protobuf.TextFormat;
public class Test1 {
/**
* @param args
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws Exception {
//创建配置文件
Configuration conf=new Configuration();
//创建job
Job job = new Job(conf,Test1.class.getName());
//设置jar包运行
job.setJarByClass(Test1.class);
//设置输入路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
//设置输入格式
job.setInputFormatClass(TextInputFormat.class);
//设置自定义Mapper
job.setMapperClass(MyMapper.class);
//设置Map输出的Value类型,也就是V2
job.setMapOutputValueClass(Model.class);
//设置Map输出的Key类型,也就是K2
job.setMapOutputKeyClass(Text.class);
//设置分区类型
job.setPartitionerClass(HashPartitioner.class);
//设置Rudece任务数
job.setNumReduceTasks(1);
//设置自定义Reduce类
job.setReducerClass(MyReducer.class);
//设置输出K3的类型
job.setOutputKeyClass(Text.class);
//设置输出的V3类型
job.setOutputValueClass(Model.class);
//设置输出的格式
job.setOutputFormatClass(TextOutputFormat.class);
//指定输出路径
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//提交job
job.waitForCompletion(true);}
static class MyMapper extends Mapper<LongWritable, Text, Text, Model>{
@Override
protected void map(LongWritable k1, Text v1,Context context)
throws IOException, InterruptedException {
/**
* 切割字符串有点意思!
* “*”是特殊字符,需要用[]
* "&"需要用\\转义
*
*
*/
String[] split = v1.toString().split("[*]\\&[*]");
Text user_agent = new Text(split[5]);
Long tot_flux = new Long(split[6])+new Long(split[7]);
Long url_cnt = new Long(split[8]);
Model v2 = new Model(tot_flux, url_cnt);
context.write(user_agent, v2);
}
}
static class MyReducer extends Reducer<Text, Model, Text, Model>{
@Override
protected void reduce(Text k2, Iterable<Model> v2s,Context context)
throws IOException, InterruptedException {
//定义计数器
long sum_flux =0L;
long sum_url = 0L;
for(Model model : v2s){
sum_flux += model.tot_flux;
sum_url += model.url_cnt;
}
Model v3 = new Model(sum_flux,sum_url);
context.write(k2, v3);
}
}}
/**
* 自定义类型必须实现Writable
* @author Sky
*
*/
class Model implements Writable{
long tot_flux;
long url_cnt;
public Model(){}
public Model(Long tot_flux,Long url_cnt){
this.tot_flux = tot_flux;
this.url_cnt = url_cnt;
}public void write(DataOutput out) throws IOException {
//序列化出去
out.writeLong(tot_flux);
out.writeLong(url_cnt);
}public void readFields(DataInput in) throws IOException {
//和序列化出去的一样
this.tot_flux = in.readLong();
this.url_cnt = in.readLong();
}
//必须覆写toString方法,否则输出的值是内存值
@Override
public String toString() {
return tot_flux+"\t"+url_cnt;
}
}
文章参考论坛:超人hadoop网络学院论坛
Hadoop工程师面试题(1)--MapReduce实现单表汇总统计的更多相关文章
- Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException
简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Ha ...
- Hadoop案例(七)MapReduce中多表合并
MapReduce中多表合并案例 一.案例需求 订单数据表t_order: id pid amount 1001 01 1 1002 02 2 1003 03 3 订单数据order.txt 商品信息 ...
- 20180518VSTO多簿单表汇总外接程序按钮
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- 20180518VSTO多簿单表汇总
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- Hadoop阅读笔记(三)——深入MapReduce排序和单表连接
继上篇了解了使用MapReduce计算平均数以及去重后,我们再来一探MapReduce在排序以及单表关联上的处理方法.在MapReduce系列的第一篇就有说过,MapReduce不仅是一种分布式的计算 ...
- MapReduce应用案例--单表关联
1. 实例描述 单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘. 实例中给出child-parent 表, 求出grandchild-grandparent表. ...
- Web前端开发工程师面试题
Web前端开发工程师面试题1.说说css的优先级?2.在移动端中,常常使用tap作为点击事件,好处是?会带来什么问题?3.原生JS的window,onload与Jquery的$(document).r ...
- Hadoop介绍及最新稳定版Hadoop 2.4.1下载地址及单节点安装
Hadoop介绍 Hadoop是一个能对大量数据进行分布式处理的软件框架.其基本的组成包括hdfs分布式文件系统和可以运行在hdfs文件系统上的MapReduce编程模型,以及基于hdfs和MapR ...
- MapReduce编程系列 — 5:单表关联
1.项目名称: 2.项目数据: chile parentTom LucyTom JackJone LucyJone JackLucy MaryLucy Ben ...
随机推荐
- leetcode解题—Longest Palindromic Substring
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- puppet中anchor的作用
anchor出现背景:Puppet Forge是一个网上的module仓库,许多人写的puppet module会传上去,供大家下载使用.大家下载了一个module可以直接使用,不应该再来改动里面ma ...
- JAVA技术体系发展路线
JAVA技术体系 1.1 Java程序员 ·高级特性 反射.泛型.注释符.自动装箱和拆箱.枚举类.可变参数.可变返回类型.增强循环.静态导入 ·核心编程 IO.多线程.实体类.集合类.正则表达式.XM ...
- 蜗牛历险记(二) Web框架(中)
上篇简单介绍了框架所使用的Autofac,采用Autofac提供的Ioc管理整个Web项目中所有对象的生命周期,实现框架面向接口编程.接下来介绍框架的日志系统. 一.介绍之前 框架日志是否有存在的必要 ...
- <三> SQL杂七杂八
批量插入测试数据 use Testdeclare @count INTset @count = 0while(@count < 10)begin waitfor delay '000:00:10 ...
- WordPress数据库中的表、字段、类型及说明
wp_categories: 用于保存分类相关信息的表.包括了5个字段,分别是: cat_ID – 每个分类唯一的ID号,为一个bigint(20)值,且带有附加属性auto_increment. c ...
- WCF 消息压缩性能问题及解决方法
最近使用WCF作为通迅框架开发一套信息系统,系统使用传统C/S框架,系统有可能会部署在互联网上,因此决定对传输的数据进行GZIP压缩,原来在使用.NET Remoting时,可以使用插入自定义的Cha ...
- Django里,如何更改ADMIN管理后台的显示
今天在慢慢完善管理后台的一些体验, 第一是要扩展默认显示类,就是现在弄的. 第二是要让一些显示框更自然友好,这是下一次要作的. 在各个app的admin.py里,加入相关的MIXIN类 class S ...
- 为tomcat启用nio机制
tomcat的运行模式有3种.修改他们的运行模式.3种模式的运行是否成功,可以看他的启动控制台,或者启动日志.或者登录他们的默认页面http://localhost:8080/查看其中的服务器状态. ...
- SQL跨表更新
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/4384039.html] 前提:两张表要更新的字段.关联字段结构一致 更新库:FJPDI_TZ ...