MapReduce基础开发之三字段处理并输出Hive表
1)设计:
输入:用户名 | 数字ip | 时间戳 | url
MR处理:正则表达式匹配url,满足则解析url并转换ip和时间戳,
输出:用户名|点分制IP|日期时间|URL域名|URL参数;
2)开发,见源码;
测试文件:/tmp/fjs/in/testdata.txt
输出文件:/tmp/fjs/out/part-r-00000.bz2
注意:开发中务必避免陷入两个误区,一个是本地文件处理和hdfs文件处理不同; 另一个是Map和Reduce函数是在不同节点上执行,参数要能共享;
2、Hive表:外部表tmp_fjs_adslurl,location=/tmp/fjs/out/
1)建表脚本:hive>
CREATE EXTERNAL TABLE `tmp_fjs_adslurl`(`account` string,`ip` string,`time_stamp` string,`domain` string,`para` string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION 'hdfs://nameservice-ha/tmp/fjs/out';
2)查询脚本:hive>select count(*) from tmp_fjs_adslurl;
1)MapReduce代码
其中URLLocation类解析URL提取域名和参数串采用字符串截取,可参考网上通用方法,如http://volunteer521.iteye.com/blog/1685942,这里是直接用了别人代码,未经授权不便发布。
import java.io.IOException;
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 AdslUrl {public static class UrlMapper extends Mapper<Object, Text, Text, Text>{private Text oKey = new Text();private Text oValue=new Text();Util util=new Util();public void map(Object key, Text value, Context context)throws IOException, InterruptedException {String[] iValue=value.toString().split("\\|");//获取行,并按照|分隔符提取oKey.set(iValue[0].trim());//用户名作为输出keyString ip=util.Long2IP(iValue[1].trim());//数字ip转化为点分制String dt=util.timestamp2date(iValue[2].trim());//时间戳转日期时间//解析url,提取域名和参数及其值if(util.regular(iValue[3])){//url正则匹配URLocation parser = new URLocation(" "+iValue[3].trim());//头部加空格String domain=parser.getPath();//域名String para=parser.getQuery();//参数串,&分割oValue.set(ip+"|"+dt+"|"+domain+"|"+para);context.write(oKey, oValue);}} }public static class UrlReducer extends Reducer<Text,Text,Text,Text> {private Text oKey = new Text();public void reduce(Text key, Iterable<Text> values,Context context) throws IOException, InterruptedException {for (Text val : values) {String str=key.toString()+"|"+val.toString();oKey.set(str);context.write(oKey, new Text(""));}}}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: AdslUrl <in> <out>");System.exit(2);}Job job = new Job(conf, "parse url");job.setJarByClass(AdslUrl.class);job.setNumReduceTasks(1);//设置reduce输入文件一个,方便查看结果job.setMapperClass(UrlMapper.class);job.setCombinerClass(UrlReducer.class);job.setReducerClass(UrlReducer.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);}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Util {//数字型IP转为点分十进制IPpublic String Long2IP(String _ipLong){StringBuffer ipInfo = new StringBuffer(); long mask[] = {0x000000FF,0x0000FF00,0x00FF0000,0xFF000000}; long num = 0; try{Long ipLong = Long.parseLong(_ipLong);ipInfo.setLength(0);for(int i=0;i<4;i++){ num = (ipLong & mask[i])>>(i*8); if(i>0) ipInfo.insert(0,"."); ipInfo.insert(0,Long.toString(num,10)); }return ipInfo.toString(); } catch (Exception ex){return "";}}//时间戳转日期时间public String timestamp2date(String _timeStamp){String dateFormat = "yyyyMMddHHmmss";SimpleDateFormat fm = new SimpleDateFormat(dateFormat);;if (_timeStamp.equals("")){return "";}try{long timeStamp = Long.parseLong(_timeStamp);String dt = fm.format(new Date(timeStamp*1000));return dt;} catch (Exception ex){return "";}}//正则过滤域名和参数public boolean regular(String url){//正则表达式String regEx="baidu|sina|qq";Pattern pattern = Pattern.compile(regEx,Pattern.CASE_INSENSITIVE);Matcher matcher = pattern.matcher(url);if(matcher.find()) return true;return false;}
}
MapReduce基础开发之三字段处理并输出Hive表相关推荐
- 【转载】salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解...
salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解 建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema ...
- MapReduce基础开发之二数据去重和排序
因Hadoop集群平台网络限制,只能在eclipse里先写好代码再提交jar到集群平台namenode上执行,不能实时调试,所以没有配置eclipse的hadoop开发环境,只是引入了hadoop的l ...
- MapReduce基础开发之七Hive外部表分区
MapReduce输出的数据在实际应用中会加载在不同子目录下,比如按照日期.地区等,然后统一到外部表,这就需要用到hive表的分区. 假设输出的数据放在/tmp/fjs/dpi父目录下,下面分别有三个 ...
- MapReduce基础开发之十读写ORC File
1.ORC File Orc是Hive特有的一种列式存储的文件格式,它有着非常高的压缩比和读取效率,因此很快取代了之前的RCFile,成为Hive中非常常用的一种文件格式. 2.编译ORC Jar包 ...
- MapReduce基础开发之六Map多输入
在实际MapReduce开发中,会遇到需要数据多输入路径并有对应的Map函数来处理,这需要MultipleInputs.addInputPath(job, path, inputFormatClass ...
- MapReduce基础开发context.write注意new text()多出一列的问题
1.问题描述:在MapReduce中代码中,Map输出context.write(okey,new text("")),Reduce也是context.write(okey,new ...
- MapReduce基础开发之四参数传递
Map和Reduce函数是在各节点进行,如果要在MapReduce数据加工中使用共同参数,要如何传参呢?方法有二: 1.Configuration类的set和get的方法读取xml/txt文件设置或自 ...
- salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解
建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema Builder查看表结构以及多表之间的关联关系,可以登录后点击setup在左侧搜索框输入schema ...
- MapReduce基础开发之十二ChainMapper和ChainReducer使用
1.需求场景: 过滤无意义的单词后再进行文本词频统计.处理流程是: 1)第一个Map使用无意义单词数组过滤输入流: 2)第二个Map将过滤后的单词加上出现一次的标签: 3)最后Reduce输出词 ...
最新文章
- 你的肠道菌群是遗传自你父母,还是后天环境塑造的?
- 黑马程序员----银行业务调度系统面试题
- 超级计算机预测南方下雪,今冬南方就不下雪了?超级计算机:可能性确实在减小,但还要观察...
- bootstrap select2 php,JS组件Bootstrap Select2使用方法详解
- 岳阳学计算机软件,岳阳学java专业学校排名
- Win7下Maven的安装与配置
- Day3-Spring的事务管理、Spring框架的JDBC模板
- NPM 简单实用说明
- 【讲座预告】Processing Learner Texts: from Annotation to ...
- Neo4j之下载安装:windows
- SQL Server 2016 完全卸载(安装)全教程
- java中怎么自己画地图_用 4 行代码画一幅中国地图
- 双十一会场体验 SSR 优化 - 走向更复杂的渲染架构
- Unicode(全世界每个国家字符的唯一编码0x000000 到 0x10FFFF)与UTF-8的区别
- Linux Capability探索实验
- 计算机管理中误删用户,win10管理员账户删除了怎么解决?
- 王牌战争服务器维护中多少才能玩,王牌战争最低配置要求一览 什么手机可以玩...
- 多多视频带货最容易出单的4个品类!
- openFOAM中时间步长计算参数是怎样读进程序的——初步理解框架
- 冰雹猜想不一样的算法