没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
25页
22、MapReduce使用Gzip压缩、Snappy压缩和Lzo压缩算法写文件和读取相应的文件 网址:https://blog.csdn.net/chenwewi520feng/article/details/130456088 本文的前提是hadoop环境正常。 本文最好和MapReduce操作常见的文件文章一起阅读,因为写文件与压缩往往是结合在一起的。 相关压缩算法介绍参考文章:HDFS文件类型与压缩算法介绍。 本文介绍写文件时使用的压缩算法,包括:Gzip压缩、Snappy压缩和Lzo压缩。 本文分为3部分,即Gzip压缩文件的写与读、Snappy压缩文件的写与读和Lzo压缩文件的写与读。 ———————————————— 版权声明:本文为CSDN博主「一瓢一瓢的饮 alanchan」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/chenwewi520feng/article/details/130456088
资源推荐
资源详情
资源评论
@TOC
本文的前提是hadoop环境正常。
本文最好和MapReduce操作常见的文件文章一起阅读,因为写文件与压缩往往是结合在一起的。
相关压缩算法介绍参考文章:HDFS文件类型与压缩算法介绍。
本文介绍写文件时使用的压缩算法,包括:Gzip压缩、Snappy压缩和Lzo压缩。
本文分为3部分,即Gzip压缩文件的写与读、Snappy压缩文件的写与读和Lzo压缩文件的写与读。
一、源文件:TextFile文件
以下示例是基于该文件作为源文件,换成不同的压缩算法。
源数据记录条数:12606948条
clickhouse系统存储文件大小:50.43 MB
逐条读出存成文本文件大小:1.08G(未压缩)
逐条读出存成ORC文件大小:105M(默認壓縮算法是ZLIB)
二、Gzip压缩文件的写与读
1、写Gzip文件
读取Text文件写为压缩后的Text文件。
//配置输出结果压缩为Gzip格式,可以不用reduce。如果不用reduce,由于文件比较大,map有9个,所以
会输出9个文件。本示例使用了reducer
// conf.set("mapreduce.output.fileoutputformat.compress","true");
//
conf.set("mapreduce.output.fileoutputformat.compress.codec","org.apache.hadoop.i
o.compress.GzipCodec");
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.springframework.util.StopWatch;
/**
* @author alanchan
*
*/
public class WriteFromTextFileToTextFileByGzip extends Configured implements
Tool {
static String in = "D:/workspace/bigdata-component/hadoop/test/in/seq";
static String out = "D:/workspace/bigdata-
component/hadoop/test/out/compress/gzip";
static String flag = "1";
@Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
Job job = Job.getInstance(conf, this.getClass().getSimpleName());
job.setJarByClass(this.getClass());
FileInputFormat.addInputPath(job, new Path(args[0]));
Path outputDir = new Path(args[1]);
outputDir.getFileSystem(this.getConf()).delete(outputDir, true);
FileOutputFormat.setOutputPath(job, outputDir);
job.setMapperClass(WriteFromTextFileToTextFileByGzipMapper.class);
job.setMapOutputKeyClass(NullWritable.class);
job.setMapOutputValueClass(Text.class);
job.setReducerClass(WriteFromTextFileToTextFileByGzipReducer.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
// job.setNumReduceTasks(0);
return job.waitForCompletion(true) ? 0 : 1;
2、读Gzip文件
与读取一般txtfile文件没有区别。
}
public static void main(String[] args) throws Exception {
StopWatch clock = new StopWatch();
clock.start(WriteFromTextFileToTextFileByGzip.class.getSimpleName());
Configuration conf = new Configuration();
// 配置输出结果压缩为Gzip格式
if (flag.equals(args[2])) {
conf.set("mapreduce.output.fileoutputformat.compress", "true");
conf.set("mapreduce.output.fileoutputformat.compress.codec",
"org.apache.hadoop.io.compress.GzipCodec");
}
int status = ToolRunner.run(conf, new
WriteFromTextFileToTextFileByGzip(), args);
clock.stop();
System.out.println(clock.prettyPrint());
System.exit(status);
}
static class WriteFromTextFileToTextFileByGzipMapper extends
Mapper<LongWritable, Text, NullWritable, Text> {
protected void map(LongWritable key, Text value, Context context) throws
IOException, InterruptedException {
context.write(NullWritable.get(), value);
}
}
static class WriteFromTextFileToTextFileByGzipReducer extends
Reducer<NullWritable, Text, NullWritable, Text> {
protected void reduce(NullWritable key, Iterable<Text> values, Context
context)
throws IOException, InterruptedException {
for (Text value : values) {
context.write(NullWritable.get(), value);
}
}
}
}
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
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.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.springframework.util.StopWatch;
public class ReadFromGzipFileToTextFile extends Configured implements Tool {
static String out = "D:/workspace/bigdata-
component/hadoop/test/out/compress/gzipread";
static String in = "D:/workspace/bigdata-
component/hadoop/test/out/compress/gzip";
@Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
Job job = Job.getInstance(conf, this.getClass().getSimpleName());
job.setJarByClass(this.getClass());
FileInputFormat.addInputPath(job, new Path(args[0]));
Path outputDir = new Path(args[1]);
outputDir.getFileSystem(this.getConf()).delete(outputDir, true);
FileOutputFormat.setOutputPath(job, outputDir);
job.setMapperClass(ReadFromGzipFileToTextFileMapper.class);
job.setMapOutputKeyClass(NullWritable.class);
job.setMapOutputValueClass(Text.class);
job.setReducerClass(ReadFromGzipFileToTextFileReducer.class);
job.setOutputKeyClass(NullWritable.class);
job.setOutputValueClass(Text.class);
// job.setNumReduceTasks(0);
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) throws Exception {
StopWatch clock = new StopWatch();
clock.start(ReadFromGzipFileToTextFile.class.getSimpleName());
Configuration conf = new Configuration();
int status = ToolRunner.run(conf, new ReadFromGzipFileToTextFile(),
args);
clock.stop();
System.out.println(clock.prettyPrint());
System.exit(status);
}
剩余24页未读,继续阅读
资源评论
一瓢一瓢的饮alanchanchn
- 粉丝: 2827
- 资源: 69
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功