MR案例:MR和Hive中使用Lzo压缩
在MapReduce中使用lzo压缩
1).首先将数据文件在本地使用lzop命令压缩。具体配置过详见配置hadoop集群的lzo压缩
//压缩lzop,解压缩lzop -d
[root@ncst word]# lzop words.txt
[root@ncst word]# ls
words.txt words.txt.lzo
2).将lzo文件上传到hdfs
[root@ncst word]# hadoop fs -put words.txt.lzo /test/in/words/
[root@ncst word]# hadoop fs -ls /test/in/words
Found 1 items
-rw-r--r-- 1 root supergroup 115 2015-08-28 21:13 /test/in/words/words.txt.lzo
3).给Lzo文件建立索引Index(两种方式)
//单机版
[root@ncst word]# hadoop jar \
> /usr/local/hadoop/hadoop-2.2.0/share/hadoop/common/lib/hadoop-lzo-0.4.20-SNAPSHOT.jar \
> com.hadoop.compression.lzo.LzoIndexer \
> /test/in/words
//集群版本
[root@ncst word]# hadoop jar \
> /usr/local/hadoop/hadoop-2.2.0/share/hadoop/common/lib/hadoop-lzo-0.4.20-SNAPSHOT.jar \
> com.hadoop.compression.lzo.DistributedLzoIndexer \
> /test/in/words
//索引文件以.index结尾
[root@ncst word]# hadoop fs -ls /test/in/words
Found 2 items
-rw-r--r-- 1 root supergroup 115 2015-08-28 21:13 /test/in/words/words.txt.lzo
-rw-r--r-- 1 root supergroup 8 2015-08-28 21:28 /test/in/words/words.txt.lzo.index
4).编写MapReduce程序(需要添加的额外包hadoop-lzo-0.4.13.jar)
package test0820; 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.Text;
import org.apache.hadoop.io.VLongWritable;
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; import com.hadoop.compression.lzo.LzopCodec;
import com.hadoop.mapreduce.LzoTextInputFormat; public class WordCount0826 { public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
//GenericOptionsParser类的getRemainingArgs()方法作用是从命令行获取配置信息
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
} Job job = Job.getInstance(conf);
job.setJarByClass(WordCount0826.class); job.setMapperClass(IIMapper.class);
job.setReducerClass(IIReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(VLongWritable.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(VLongWritable.class); //配置输入类型为Lzo
job.setInputFormatClass(LzoTextInputFormat.class);
//配置reduce结果压缩以及压缩格式
FileOutputFormat.setCompressOutput(job, true);
FileOutputFormat.setOutputCompressorClass(job, LzopCodec.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true)? 0:1);
}
//map
public static class IIMapper extends Mapper<LongWritable, Text, Text, VLongWritable>{
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException { String[] splited = value.toString().split(" "); for(String word : splited){
context.write(new Text(word),new VLongWritable(1L));
}
}
}
//reduce
public static class IIReducer extends Reducer<Text, VLongWritable, Text, VLongWritable>{
@Override
protected void reduce(Text key, Iterable<VLongWritable> v2s, Context context)
throws IOException, InterruptedException { long sum=0; for(VLongWritable vl : v2s){
sum += vl.get();
}
context.write(key, new VLongWritable(sum));
}
}
}
5).运行hadoop jar
[root@ncst test]# hadoop jar myjar.jar /test/in/words/ /test/out/0828/02
如若未在程序中配置输入和输出都为Lzo格式,可以在命令行通过 -D 开头的参数进行配置
hadoop jar myjar.jar \
-D mapred.reduce.tasks=2 \
-D mapreduce.job.inputformat.class=com.hadoop.mapreduce.LzoTextInputFormat \
-D mapred.output.compress=true \
-D mapred.output.compression.codec=com.hadoop.compression.lzo.LzopCodec \
/test/in/words /test/out/0828/001
6).查看结果文件
[root@ncst test]# hadoop fs -ls /test/out/0828/001
Found 3 items
-rw-r--r-- 1 root supergroup 0 2015-08-28 21:35 /test/out/0828/001/_SUCCESS
-rw-r--r-- 1 root supergroup 61 2015-08-28 21:35 /test/out/0828/001/part-r-00000.lzo
-rw-r--r-- 1 root supergroup 87 2015-08-28 21:35 /test/out/0828/001/part-r-00001.lzo
7).查看结果Lzo文件的内容(两种方式)
//以fs -get方式下载下来,再解压
[root@ncst test]# hadoop fs -get /test/out/0828/001 ~/out
[root@ncst test]# cd ~/out
//lzop -d *.lzo 是解压lzo文件的命令
[root@ncst out]# lzop -d part-*
[root@ncst out]# ls
part-r-00000 part-r-00000.lzo part-r-00001 part-r-00001.lzo _SUCCESS
//利用fs -text 结合Linux的重定向 > 命令
[root@ncst test]# hadoop fs -text /test/out/0828/001/part-* > out.txt
//查看out.txt结果
[root@ncst test]# more out.txt
hello 3
man 2
women 2
word 1
world 2
总结:
- lzo文件需要建立索引才能支持分块(split)。如果没有索引,lzo文件也是可以处理的,MapReduce会根据后缀名 “.lzo” 来对lzo文件解压,并且InputFormat也不需要特别指定,但是不支持分块,整个lzo文件只用一个map来处理。
- 对于输入文件为添加了索引的Lzo压缩文件,如若不在代码中指定 job.setInputFormatClass(LzoTextInputFormat.class); 则Mapreduce程序将会把索引文件.index也当作是数据文件!LzoTextInputFormat类需要引入相应的(hadoop-lzo-0.4.13.jar)包,如果你是使用Maven管理依赖,则需要在pom.xml文件中添加以下属性。
<dependency>
<groupId>com.hadoop.gplcompression</groupId>
<artifactId>hadoop-lzo</artifactId>
<version>0.4.19</version>
</dependency>
在Streaming程序中使用lzo压缩
把InputFormat设置为DeprecatedLzoTextInputFormat,还要设置参数 stream.map.input.ignoreKey=true。这样map的key值(key值是行在文件中的偏移量,value值是每行的文本)就不会传入reduce程序中,这个key值我们是不需要的。
hadoop jar
/opt/mapr/hadoop/hadoop-0.20.2/contrib/streaming/hadoop-0.20.2-dev-streaming.jar
-file /home/pyshell/map.py
-file /home/pyshell/red.py
-mapper /home/pyshell/map.py
-reducer /home/pyshell/red.py
-input /aojianlog/20120304/gold/gold_38_3.csv.lzo
-output /aojianresult/gold38
-inputformat com.hadoop.mapred.DeprecatedLzoTextInputFormat //没有此选项,map作业也不会分片
-jobconf mapred.output.compress=true //指定reduce输出压缩
-jobconf mapred.output.compression.codec=com.hadoop.compression.lzo.LzopCodec //没有此选项,reduce作业输出文件的格式为.lzo_deflate
在hive中使用lzo压缩
hadoop集群启用了Lzo压缩,就需要在Hive建表的时候指定压缩时所使用的编解码器,否则Hive无法正确读取数据。
1).Gzip和Bzip2由于是hadoop默认支持的,所以无需指定特殊的编解码器,只要指定Text类型即可。
create table lzo(
id int,
name string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;
2).LZO是外挂的第三方库,所以要指定输入和输出的编解码器。
create table lzo(
id int,
name string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS INPUTFORMAT 'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';
3).对于Hive表的数据文件,用lzop在本地压缩好了,直接上传到hdfs上就可以了。
[root@ncst test]# lzop -v lzo
compressing lzo into lzo.lzo
hive> load data local inpath '/root/test/lzo.lzo'
> overwrite into table lzo;
hive> select * from lzo;
OK
Tie
Coat
Hat
Scarf
Time taken: 0.218 seconds, Fetched: 4 row(s)
4).对于已经存在的表修改为Lzo
alter table后对已经load进表中的数据,需要重新load和创建索引,要不还是不能分块。
ALTER TABLE things
SET FILEFORMAT
INPUTFORMAT "com.hadoop.mapred.DeprecatedLzoTextInputFormat"
OUTPUTFORMAT "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat";
5).在做数据清洗的时候,假如源日志是lzo压缩的,输出的时候也希望使用lzo压缩。则在数据清洗的脚本中对hadoop的jobconf做一个指定。这样就可以做到,输入是lzo,输出也可以lzo。或者输入是text,输出是lzo。
-inputformat com.hadoop.mapred.DeprecatedLzoTextInputFormat
-jobconf mapreduce.output.compress=true
-jobconf mapreduce.output.compression.codec=com.hadoop.compression.lzo.LzopCodec
MR案例:MR和Hive中使用Lzo压缩的更多相关文章
- Hive中使用LZO
hive 中使用lzo 1 启动hive 错误Exception in thread "main" java.lang.NoClassDefFoundError: org/apac ...
- Hadoop、Hive【LZO压缩配置和使用】
目录 一.编译 二.相关配置 三.为LZO文件创建索引 四.Hive为LZO文件建立索引 1.hive创建的lzo压缩的分区表 2.给.lzo压缩文件建立索引index 3.读取Lzo文件的注意事项( ...
- 配置hadoop集群的lzo压缩
MR-Job中使用lzop详见MR案例:Job中使用Lzo压缩 1). 配置前的环境准备 # yum -y install lzo-devel zlib-devel gcc autoconf auto ...
- hadoop 支持 LZO 压缩配置
1)hadoop 本身并不支持 lzo 压缩,故需要使用 twitter 提供的 hadoop-lzo 开源组件.hadoop lzo 需依赖 hadoop 和 lzo 进行编译,编译步骤如下. 编译 ...
- MR案例:Reduce-Join
问题描述:两种类型输入文件:address(地址)和company(公司)进行一对多的关联查询,得到地址名(例如:Beijing)与公司名(例如:Beijing JD.Beijing Red Star ...
- MR案例:倒排索引
1.map阶段:将单词和URI组成Key值(如“MapReduce :1.txt”),将词频作为value. 利用MR框架自带的Map端排序,将同一文档的相同单词的词频组成列表,传递给Combine过 ...
- MR案例:小文件处理方案
HDFS被设计来存储大文件,而有时候会有大量的小文件生成,造成NameNode资源的浪费,同时也影响MapReduce的处理效率.有哪些方案可以合并这些小文件,或者提高处理小文件的效率呢? 1). 所 ...
- hbase使用MapReduce操作3(实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中)
Runner类 实现将 fruit 表中的一部分数据,通过 MR 迁入到 fruit_mr 表中. package com.yjsj.hbase_mr; import org.apache.hadoo ...
- MR案例:内连接代码实现
本文是对Hive中[内连接]的Java-API的实现,具体的HQL语句详见Hive查询Join package join.map; import java.io.IOException; import ...
随机推荐
- 170424、Mysql权限控制 - 允许用户远程连接
Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...
- Microsoft Excel 标题栏或首行锁定
Microsoft Excel 标题栏或首行锁定 在进行Excel编辑的时候,希望在浏览的时候,第一行或者第一列能够始终显示. 需要做的是:在Excel中选择 "视图"->& ...
- Orchard 与 ABP架构比较 (aspnetboilerplate)
前言: ABP框架经常在一些.NET群中听群友提起,以前也浏览过官网,大致了解它是一个框架,直到今天本人才正式下载源码入门 ... 经过两个小时的ABP中文文档入门(感谢各位辛勤的翻译者) ,大致了 ...
- [iOS微博项目 - 3.6] - 获取未读消息
github: https://github.com/hellovoidworld/HVWWeibo A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...
- Netty处理TCP拆包、粘包
Netty实践(二):TCP拆包.粘包问题-学海无涯 心境无限-51CTO博客 http://blog.51cto.com/zhangfengzhe/1890577 2017-01-09 21:56: ...
- pymsql与ORM--python操作MySQL之利器
pymsql 原生模块 pymsql是python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 下载安装 pip3 install pymysql 使用操作 1.执行SQL impor ...
- python看内存
打断点,跑起来 ps -ef | grep python 找到PID(两个数的第一列) cat /proc/PID/status 内存主要看四个字段: vmpeak 虚拟内存历史峰值 vmsize ...
- python 递归深度优先搜索与广度优先搜索算法模拟实现
一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件2.找出这一次和上一次关系3.假设当前 ...
- H5开发APP入门
一.MUI MUI是一个最接近原生APP体验的高性能前端框架.我们用它来排版布局. 官方网站:http://dev.dcloud.net.cn/mui/ 二.HTML5PLUS html5+是HBul ...
- Spring-Spring Bean后置处理器
Spring Bean后置处理器 BeanPostProcessor接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等.你也可以在Spring容器通过插入一个或多个BeanPos ...