org.apache.hadoop.fs-PositionedReadable

本文介绍了Hadoop中PositionedReadable接口的功能与用法,包括如何从指定位置读取文件而不改变当前偏移量,以及其线程安全性。该接口提供了两种主要的方法:read和readFully,用于直接从文件的特定位置读取数据。

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


 

 

 1 package org.apache.hadoop.fs;
 2 
 3 import java.io.*;
 4 import org.apache.hadoop.fs.*;
 5 
 6 /** Stream that permits positional reading. */
 7 //定位流式读取
 8 public interface PositionedReadable {
 9   /**
10    * Read upto the specified number of bytes, from a given
11    * position within a file, and return the number of bytes read. This does not
12    * change the current offset of a file, and is thread-safe.
13    */
14   public int read(long position, byte[] buffer, int offset, int length)
15     throws IOException;
16   //读取文件流中最多到length大小的字节,到字节缓冲区buffer中,它是从给定的position位置开始读取的。  
17   //该读取方式不改变文件的当前偏移位置offset,并且该方法是线程安全的
18   /**
19    * Read the specified number of bytes, from a given
20    * position within a file. This does not
21    * change the current offset of a file, and is thread-safe.
22    */
23   public void readFully(long position, byte[] buffer, int offset, int length)
24     throws IOException;
25   /** 
26    * 读取文件流中length大小的字节,到字节缓冲区buffer中,它是从给定的position位置开始读取的。 
27    * 该读取方式不改变文件的当前偏移位置offset,并且该方法是线程安全的。 
28    */ 
29   /**
30    * Read number of bytes equalt to the length of the buffer, from a given
31    * position within a file. This does not
32    * change the current offset of a file, and is thread-safe.
33    */
34   public void readFully(long position, byte[] buffer) throws IOException;
35   /** 
36    * 读取文件流中buffer长度的字节,到字节缓冲区buffer中,它是从给定的position位置开始读取的 
37    * 该读取方式不改变文件的当前偏移位置offset,并且该方法是线程安全的。 
38    */  
39 
40 }

 


 

转载于:https://www.cnblogs.com/admln/p/PositionedReadable.html

### 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、付费专栏及课程。

余额充值