处理小文件的时候,可以通过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. phonegap ios插件开发及无限后台运行解决

    1.首先开发插件:因为我的项目前需要所以要做(根据情况) 在项目的plugins文件中新建obj c文件.如 Demo,此时会产生出Demo.h和Demo.m两个文件. .h文件主要就是定义一些方法, ...

  2. iOS xcode 8 注释快捷键

    单行注释:在方法的地方按 Command+/ 标注的功能,快捷键是Command + Option + / 需要在方法名的上面(空白)的地方按 Command + Option + /      才管 ...

  3. Careercup - Facebook面试题 - 6299074475065344

    2014-05-01 01:00 题目链接 原题: Given a matrix with 's. What is the maximum area of the rectangle. In . Ho ...

  4. [转载+原创]Emgu CV on C# (三) —— Emgu CV on 均衡化

    本文简要描述了均衡化原理及数学实现等理论问题,最终利用emgucv实现图像的灰度均衡. 直方图的均衡化,这是图像增强的常用方法. 一.均衡化原理及数学实现(转载) 均衡化原理及数学实现可重点参看——& ...

  5. windows最基本命令行

    7:计算机运行命令全集 winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构 wupdmgr--------windows更新程序 win ...

  6. centos nginx,php添加到Service

    SHELL脚本: nginx vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx da ...

  7. 框架优化系列文档:SVN中非版本控制文件忽略上传的设置

    对于SVN代码库,只应该上传源代码.资源文件等内容进行版本管理,通常编译后的二进制文件.程序包等生成产物是不应该放到SVN上做版本管理的.因此在svn的客户端工具中设置svn的属性:svn:ignor ...

  8. action间传多个参数时注意问题

    通常我们action之间传参可以有多种形式,举例说明:示例1: <result name="test" type="redirect-action"> ...

  9. linux vi修改后如何保存

    linux vi修改后如何保存 按ESC键去到命令模式,然后: :w?? 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :wq 保存文件并 ...

  10. 01-05-01-2【Nhibernate (版本3.3.1.4000) 出入江湖】立即加载实现--NHibernateUtil.Initialize()和添加fetch关键字的HQL查询

    相关资料: http://www.cnblogs.com/lyj/archive/2008/10/29/1322373.html 问题的提出: 1.延迟加载,可能会引起session已经关闭的异常,例 ...