处理小文件的时候,可以通过org.apache.hadoop.io.SequenceFile.Writer类将所有文件写出到一个seq文件中。

大致流程如下:

实现代码:

package study.smallfile.sequence_one;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.CompressionType;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.SequenceFile.Writer.Option;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class MapperDemo { private static final String INPUT_PATH = "hdfs://cluster1/smallfile/blankfile";
private static final String OUT_PATH = "hdfs://cluster1/smallfile/combined/map";
static FileSystem fileSystem; public void CombinedFile() throws Exception {
Job job = Job.getInstance(); job.setJarByClass(MapperDemo.class);
job.setJobName(MapperDemo.class.getSimpleName()); // 设置map类
job.setMapperClass(MapperDemo.CombinedMapper.class);
// 设置输出
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(BytesWritable.class);
// 设置reduce任务数量
job.setNumReduceTasks(0);
// 设置输入路径
FileInputFormat.setInputPaths(job, new Path(INPUT_PATH));
// 检查输出路径
Path outdir = new Path(OUT_PATH);
fileSystem = FileSystem.get(job.getConfiguration());
if (fileSystem.exists(outdir)) {// 如果已经存在删除
fileSystem.delete(outdir, true);
} // 设置输出路径
FileOutputFormat.setOutputPath(job, outdir); job.waitForCompletion(true); } static class CombinedMapper extends
Mapper<LongWritable, Text, Text, BytesWritable> {
Writer writer = null;
FileStatus[] files; Text outKey = new Text();
BytesWritable outValue = new BytesWritable(); FSDataInputStream in;
byte[] buffer = null; @Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, Text, BytesWritable>.Context context)
throws IOException, InterruptedException {
// for (FileStatus file : files) {
// outKey.set(file.getPath().toString());
//
// in = fileSystem.open(file.getPath());
// buffer = new byte[(int) file.getLen()];
// IOUtils.read(in, buffer, 0, buffer.length);
// outValue.set(new BytesWritable(buffer));
// writer.append(outKey, outValue);
// } } @Override
protected void cleanup(
Mapper<LongWritable, Text, Text, BytesWritable>.Context context)
throws IOException, InterruptedException {
for (FileStatus file : files) {
outKey.set(file.getPath().toString()); in = fileSystem.open(file.getPath());
buffer = new byte[(int) file.getLen()];
IOUtils.readFully(in, buffer, 0, buffer.length);
outValue.set(new BytesWritable(buffer));
writer.append(outKey, outValue);
}
IOUtils.closeStream(writer);
} @Override
protected void setup(
Mapper<LongWritable, Text, Text, BytesWritable>.Context context)
throws IOException, InterruptedException {
// 输出文件项
Option fileOption = SequenceFile.Writer.file(new Path(OUT_PATH
+ "/mapper.seq"));
// 压缩选项
Option compressionOption = SequenceFile.Writer
.compression(CompressionType.BLOCK);
// SequeneFile key类型设置
Option keyClassOption = SequenceFile.Writer.keyClass(Text.class);
// SequeneFile value类型设置
Option valueClassOption = SequenceFile.Writer
.valueClass(BytesWritable.class);
// 构建输出流文件
Configuration conf = new Configuration();
writer = SequenceFile.createWriter(conf, fileOption,
compressionOption, keyClassOption, valueClassOption);
if (fileSystem == null) {
fileSystem = FileSystem.get(conf);
}
files = fileSystem.listStatus(new Path("hdfs://cluster1/smallfile/logs")); }
}
}

注意事项:

  我原本的逻辑是放到map函数中,将所有文件通过Writer写到HDFS中,但是map在整个mr的执行中被调用的次数是根据输入文件情况确定的,通过控制输入文件的情况,可以通过map函数实现

发现问题:

原本在实现之前,定义了一个FileSystem类型的静态字段,在提交job前已经赋值了,但是,在mapper类中访问到的fileSystem字段,是空值,有知道的大虾,多多指导小弟

SequenceFile介绍:

http://wiki.apache.org/hadoop/SequenceFile

http://www.cnblogs.com/zhenjing/archive/2012/11/02/File-Format.html

 

HDFS小文件处理——Mapper处理的更多相关文章

  1. 合并hive/hdfs小文件

    磁盘: heads/sectors/cylinders,分别就是磁头/扇区/柱面,每个扇区512byte(现在新的硬盘每个扇区有4K) 文件系统: 文件系统不是一个扇区一个扇区的来读数据,太慢了,所以 ...

  2. hadoop 小文件 挂载 小文件对NameNode的内存消耗 HDFS小文件解决方案 客户端 自身机制 HDFS把块默认复制3次至3个不同节点。

    hadoop不支持传统文件系统的挂载,使得流式数据装进hadoop变得复杂. hadoo中,文件只是目录项存在:在文件关闭前,其长度一直显示为0:如果在一段时间内将数据写到文件却没有将其关闭,则若网络 ...

  3. 解决HDFS小文件带来的计算问题

    hive优化 一.小文件简述 1.1. HDFS上什么是小文件? HDFS存储文件时的最小单元叫做Block,Hadoop1.x时期Block大小为64MB,Hadoop2.x时期Block大小为12 ...

  4. HDFS 小文件处理——应用程序实现

    在真实环境中,处理日志的时候,会有很多小的碎文件,但是文件总量又是很大.普通的应用程序用来处理已经很麻烦了,或者说处理不了,这个时候需要对小文件进行一些特殊的处理——合并. 在这通过编写java应用程 ...

  5. Hadoop小文件存储方案

    原文地址:https://www.cnblogs.com/ballwql/p/8944025.html HDFS总体架构 在介绍文件存储方案之前,我觉得有必要先介绍下关于HDFS存储架构方面的一些知识 ...

  6. hadoop 使用map将SequenFile里的小文件解压出来

    上例中将HDFS里小文件通过mapper压缩到一个文件中,本例将这些小文件解压出来. mapreduce可以按SequenceFile的key进行分片. 1.mapper public class M ...

  7. MR案例:小文件处理方案

    HDFS被设计来存储大文件,而有时候会有大量的小文件生成,造成NameNode资源的浪费,同时也影响MapReduce的处理效率.有哪些方案可以合并这些小文件,或者提高处理小文件的效率呢? 1). 所 ...

  8. Spark SQL 小文件问题处理

    在生产中,无论是通过SQL语句或者Scala/Java等代码的方式使用Spark SQL处理数据,在Spark SQL写数据时,往往会遇到生成的小文件过多的问题,而管理这些大量的小文件,是一件非常头疼 ...

  9. Hadoop HDFS编程 API入门系列之合并小文件到HDFS(三)

    不多说,直接上代码.  代码 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs7; import java.io.IOException;import ja ...

随机推荐

  1. GOOGLE影像地图

    卫星地图高清 //  

  2. Daject初探 - 从Table模型得到Record模型

    前言: 如果你还不知道Daject是什么,如何使用,可以浏览 http://www.cnblogs.com/kason/p/3577359.html github地址:https://github.c ...

  3. ThinkPHP学习笔记 实例化模型的四种方法

    创建Action类   [php]   <?php   class NewObjectAction extends Action{       public function index(){ ...

  4. 常用汉字的Unicode码表

    \u96d5\u864e\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u77 ...

  5. BZOJ 1854: [Scoi2010]游戏 无向图判环

    题目链接: 题目 1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB 问题描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装 ...

  6. JS中的className含义

    问题描述:     JS中的className含义 问题解决:     className说明:         className属性可以设置和返回元素的class属性 可以有两种方法来获取对象的c ...

  7. z-index兼容问题:关于ie6/7下的z-index

    z-index这个属性其实在挺多地方都会用到,在百度上搜索也有大量关于z-index的篇幅去阐述这个属性,特别是在ie6下的z-index处理有更多的相关文章,本文就不再围绕z-index这一属性的基 ...

  8. vs2010 mvc3创建的razor引擎模板页,子页面引用后出现当前上下文中不存在名称“ViewBag”

    View文件夹下缺少Web.config

  9. hdu 2604 Queuing(动态规划—>矩阵快速幂,更通用的模版)

    题目 最早不会写,看了网上的分析,然后终于想明白了矩阵是怎么出来的了,哈哈哈哈. 因为边上的项目排列顺序不一样,所以写出来的矩阵形式也可能不一样,但是都是可以的 //愚钝的我不会写这题,然后百度了,照 ...

  10. hdu 1596 find the safest road(最短路,模版题)

    题目 这是用Dijsktra做的,稍加改动就好,1000ms..好水.. #define _CRT_SECURE_NO_WARNINGS #include<string.h> #inclu ...