3

我在 HDFS 中存储了大量数据,但单个文件非常小(KB)。所以 MapReduce 处理需要很多时间。

我可以减少处理时间吗?SequenceFile 会是一个不错的选择吗?

请提供一些 Java 或 MR 代码以将多个较小的文本文件转换为 SequenceFile。

4

2 回答 2

4

在这种情况下,SequenceFile 将是一个不错的选择。你可以做这样的事情:

public class TextToSequenceConverter {
    /**
     * @param args
     * @throws IOException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException,
            InstantiationException, IllegalAccessException {
        // TODO Auto-generated method stub

        Configuration conf = new Configuration();
        conf.addResource(new Path("/hadoop/projects/hadoop-1.0.4/conf/core-site.xml"));
        conf.addResource(new Path("/hadoop/projects/hadoop-1.0.4/conf/hdfs-site.xml"));
        FileSystem fs = FileSystem.get(conf);
        Path inputFile = new Path("/infile");
        FSDataInputStream inputStream = fs.open(inputFile);
        Path outputFile = new Path("/outfile");
        IntWritable key = new IntWritable();
        int count = 0;
        Text value = new Text();    
        String str;
        SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf,outputFile, key.getClass(), value.getClass());
        while (inputStream.available() > 0) {
            key.set(count++);
            str = inputStream.readLine();
            value.set(str);
            writer.append(key, value);
        }
        fs.close();
        IOUtils.closeStream(writer);
        System.out.println("SEQUENCE FILE CREATED SUCCESSFULLY........");
    }
}

您可能还想看看 HAR 文件。

您可能会觉得这是一本好书:http: //blog.cloudera.com/blog/2009/02/the-small-files-problem/


要将 HDFS 目录中的所有文件转换为单个序列文件:

package my.pack;

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.IOUtils;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;

public class BundleSeq {

    /**
     * @param args
     * @throws IOException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static void main(String[] args) throws IOException,
            InstantiationException, IllegalAccessException {
        // TODO Auto-generated method stub

        Configuration conf = new Configuration();
        conf.addResource(new Path(
                "/hadoop/projects/hadoop-1.0.4/conf/core-site.xml"));
        conf.addResource(new Path(
                "/hadoop/projects/hadoop-1.0.4/conf/hdfs-site.xml"));
        FileSystem fs = FileSystem.get(conf);
        Path inputFile = new Path("/bundleinput");
        Path outputFile = new Path("/outfile");
        FSDataInputStream inputStream;
        Text key = new Text();
        Text value = new Text();
        SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf,
                outputFile, key.getClass(), value.getClass());
        FileStatus[] fStatus = fs.listStatus(inputFile);

        for (FileStatus fst : fStatus) {
            String str = "";
            System.out.println("Processing file : " + fst.getPath().getName() + " and the size is : " + fst.getPath().getName().length());
            inputStream = fs.open(fst.getPath());
            key.set(fst.getPath().getName());
            while(inputStream.available()>0) {
                str = str+inputStream.readLine();
            }
            value.set(str);
            writer.append(key, value);

        }
        fs.close();
        IOUtils.closeStream(writer);
        System.out.println("SEQUENCE FILE CREATED SUCCESSFULLY........");
    }
}

这里文件名是键,文件内容是值。

于 2013-06-10T11:57:44.153 回答
0

您可以覆盖org.apache.hadoop.mapred.lib.CombineFileInputFormat并创建您的CombinedInputFormat. 有关实施,请参阅我的答案here。通过设置参数mapred.max.split.size,您可以控制您希望将输入文件组合成的大小。

更多信息请阅读这里

于 2013-06-12T18:25:47.247 回答