我在 HDFS 中存储了大量数据,但单个文件非常小 (KB)。因此 MapReduce 处理需要花费大量时间。

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

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

最佳答案

在这种情况下,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 目录中的所有文件转换为单个 Sequence 文件:
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........");
    }
}

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

关于hadoop - 如何将大小为 KB 的文本文件转换为序列文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17022850/

10-14 14:06
查看更多