zoukankan      html  css  js  c++  java
  • [SequenceFile_4] SequenceFile 配置压缩


    0. 说明

       SequenceFile 配置压缩编解码器 && 压缩类型的选型


     1. SequenceFile 配置压缩编解码器

    package hadoop.compression;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.SequenceFile;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.io.compress.BZip2Codec;
    import org.apache.hadoop.io.compress.CompressionCodec;
    import org.apache.hadoop.io.compress.Lz4Codec;
    import org.apache.hadoop.util.ReflectionUtils;
    import org.junit.Test;
    
    /**
     * SequenceFile 配置压缩编解码器
     */
    public class TestSeqFileCompression {
        /**
         * 测试 SequenceFile 配置压缩编解码器进行压缩
         */
        @Test
        public void testWriteSeq() throws Exception {
    
            Configuration conf = new Configuration();
    
            // 设置文件系统为本地模式
            conf.set("fs.defaultFS", "file:///");
    
            FileSystem fs = FileSystem.get(conf);
    
            // 通过反射获取 CompressionCodec 对象
            // BZip2Codec.class  /  Lz4Codec.class
    //        BZip2Codec codec = ReflectionUtils.newInstance(BZip2Codec.class, conf);
            Lz4Codec codec = ReflectionUtils.newInstance(Lz4Codec.class, conf);
    
    //        Path path = new Path("E:/test/bz2.seq");
            Path path = new Path("E:/test/lz4.seq");
    
            // 块压缩
            SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, path, IntWritable.class, Text.class, SequenceFile.CompressionType.BLOCK, codec);
    
    
            for (int i = 1; i <= 1000000; i++) {
                IntWritable key = new IntWritable(i);
                Text value = new Text("helloworld" + i);
    
                writer.append(key, value);
    
            }
    
            writer.close();
        }
    
       
    }

     2. 压缩类型的选型

      1. 使用 SequenceFile 等容器文件格式(SequenceFile | Avro 数据文件 | ORCFiles | Parquet 文件) + 快速压缩工具(LZO | LZ4 | Snappy)

      效率最高


      2. 使用支持切分的压缩格式(bzip2 | LZO),即支持逻辑切割的压缩格式
        LZO只有在添加索引的时候才支持切割,即 LZO 文件的预处理

    package hadoop.compression;
    
    import com.hadoop.compression.lzo.LzoIndexer;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    
    /**
     * LZO 文件的预处理,即在使用 LZO 文件之前添加索引
     */
    public class TestLzoIndex {
        public static void main(String[] args) throws Exception {
            Configuration conf = new Configuration();
            // 压缩编解码器必须是此类或其子类
            conf.set("io.compression.codecs","com.hadoop.compression.lzo.LzopCodec");
            LzoIndexer indexer = new LzoIndexer(conf);
            indexer.index(new Path("file:///E:/test/codec/sdata.txt.lzo"));
        }
    }

      3. 将文本文件(待处理的文件)进行预切割,在每个文件段下进行压缩

      4. 存储未压缩的文件,效率最低


  • 相关阅读:
    linux
    算法
    算法
    数据结构 与 算法
    mysql
    mysql
    mysql
    mysql
    【解决】Could not get JDBC Connection、java.lang.InterruptedException问题和排查过程
    git: unable to checkout working tree error: unable to create file Filename too long on windows
  • 原文地址:https://www.cnblogs.com/share23/p/9895393.html
Copyright © 2011-2022 走看看