org.apache.hadoop.io.compress源码解读

本文详细介绍了Hadoop中压缩器接口Compressor的功能与使用方法,包括设置输入数据、检查输入缓冲区状态、设定预设词典等操作,并阐述了如何获取压缩数据及管理压缩流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hadoop.io.compress;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

/**
 * Specification of a stream-based 'compressor' which can be  
 * plugged into a {@link CompressionOutputStream} to compress data.
 * This is modelled after {@link java.util.zip.Deflater}
 *
 */
public interface Compressor {
  /**
   * Sets input data for compression.
   * This should be called whenever #needsInput() returns
   * <code>true</code> indicating that more input data is required.
   *
   * @param b Input data
   * @param off Start offset
   * @param len Length
   */
    //通过setInput()接收数据到内部缓存区  可以多次调用该方法
  public void setInput(byte[] b, int off, int len);
 
  /**
   * Returns true if the input data buffer is empty and
   * #setInput() should be called to provide more input.
   *
   * @return <code>true</code> if the input data buffer is empty and
   * #setInput() should be called in order to provide more input.
   */
 
  //返回false表示内部缓存已经满了  此时必须通过compress()方法获取压缩后的数据
  public boolean needsInput();
 
  /**
   * Sets preset dictionary for compression. A preset dictionary
   * is used when the history buffer can be predetermined.
   *
   * @param b Dictionary data bytes
   * @param off Start offset
   * @param len Length
   */
  public void setDictionary(byte[] b, int off, int len);

  /**
   * Return number of uncompressed bytes input so far.
   */
  //获得compressor()输入没有压缩字节的总数
  public long getBytesRead();

  /**
   * Return number of compressed bytes output so far.
   */
  //输出压缩自己的总数
  public long getBytesWritten();

  /**
   * When called, indicates that compression should end
   * with the current contents of the input buffer.
   */
  //调用finish()就开始压缩  
  public void finish();
 
  /**
   * Returns true if the end of the compressed
   * data output stream has been reached.
   * @return <code>true</code> if the end of the compressed
   * data output stream has been reached.
   */
  //判断压缩器里是不是还有没有压缩的数据
  public boolean finished();
 
  /**
   * Fills specified buffer with compressed data. Returns actual number
   * of bytes of compressed data. A return value of 0 indicates that
   * needsInput() should be called in order to determine if more input
   * data is required.
   *
   * @param b Buffer for the compressed data
   * @param off Start offset of the data
   * @param len Size of the buffer
   * @return The actual number of bytes of compressed data.
   */
  public int compress(byte[] b, int off, int len) throws IOException;
 
  /**
   * Resets compressor so that a new set of input data can be processed.
   */
  //用与重置压缩器 以处理新的输入数据集合
  public void reset();
 
  /**
   * Closes the compressor and discards any unprocessed input.
   */
  //关闭解压缩器并放弃所有没有处理的输入
  public void end();

  /**
   * Prepare the compressor to be used in a new stream with settings defined in
   * the given Configuration
   *
   * @param conf Configuration from which new setting are fetched
   */
  //更进一步允许使用hadoop的配置系统 重新配置压缩器
  public void reinit(Configuration conf);
}

### Hudi 中 `org.apache.hudi.hadoop.realtime` 功能介绍 `org.apache.hudi.hadoop.realtime` 是 Apache Hudi 提供的一个核心类,主要用于支持实时数据查询的功能。它允许用户在 Merge-on-Read (MOR) 类型的 Hudi 表中实现高效的实时查询能力[^1]。以下是关于其功能和使用方法的详细介绍。 #### 1. 实时查询的核心原理 Hudi 的 MOR 表结构分为两部分:一个是存储已提交数据的基础文件(Base File),另一个是存储增量更新的日志文件(Log Files)。当启用实时查询时,`org.apache.hudi.hadoop.realtime` 会动态合并基础文件和日志文件的内容,从而返回最新的记录状态[^2]。 这种设计使得用户无需等待所有的更新都落地到基础文件中就可以获取最新数据,非常适合需要低延迟查询的场景。 #### 2. 主要功能特点 - **高效的数据合并**:通过内部优化算法,快速扫描 Log Files 和 Base Files,并将它们的结果合并在一起。 - **兼容标准输入格式**:实现了 `org.apache.hadoop.mapreduce.InputFormat` 接口,可以直接被 Hive、Presto/Trino 等引擎识别并使用[^3]。 - **支持多种压缩方式**:能够处理不同类型的压缩文件(如 LZO、Gzip 等),确保与现有大数据生态系统的无缝对接[^4]。 #### 3. 使用方法 要在应用程序中使用 `org.apache.hudi.hadoop.realtime.RealtimeRecordReader` 或者其他相关组件,请按照以下步骤操作: ##### (1)引入依赖项 确保项目中包含了正确的 Maven 或 Gradle 依赖声明。例如,在 Maven POM 文件中添加如下片段: ```xml <dependency> <groupId>org.apache.hudi</groupId> <artifactId>hudi-hadoop-mr</artifactId> <version>${hudi.version}</version> </dependency> ``` ##### (2)配置 InputFormat 为了让下游框架知道如何解析 Hudi 数据表,需要显式指定 Realtime InputFormat。这通常是在创建 JobConf 或 Configuration 对象的时候完成: ```java Configuration conf = new Configuration(); conf.set("mapreduce.input.format.class", "org.apache.hudi.hadoop.realtime.HoodieRealtimeInputFormat"); Job job = Job.getInstance(conf); FileInputFormat.addInputPath(job, new Path(hudiTablePath)); ``` ##### (3)处理潜在问题 如果遇到类似于 “Unable to create input format org.apache.hadoop.mapred.TextInputFormat” 这样的错误消息,则可能是由于缺失必要库引起的。此时应检查是否有所有必需的第三方插件已被正确安装至相应节点上的 plugin 目录下[^5]。 --- ### 示例代码展示 下面给出一段简单的 Java 示例程序演示如何读取一个基于 HDFS 存储路径下的 Hudi MOR 表内容: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; public class HudiRealTimeQueryExample { public static void main(String[] args) throws Exception { String hudiTablePath = "/path/to/hudi/mor/table"; Configuration conf = new Configuration(); conf.set("mapreduce.input.format.class", "org.apache.hudi.hadoop.realtime.HoodieRealtimeInputFormat"); Job job = Job.getInstance(conf); job.setOutputKeyClass(LongWritable.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(hudiTablePath)); System.out.println("Starting real-time query on Hudi table..."); boolean success = job.waitForCompletion(true); if (!success) { throw new RuntimeException("Job failed!"); } } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值