hive 的存储格式

本文详细介绍了Hive中四种不同的存储格式:textfile、sequencefile、rcfile和orc。每种格式的特点、适用场景及如何在Hive中创建对应格式的表进行了深入探讨。文章还解释了不同格式在数据加载过程中的限制,例如sequencefile和rcfile不支持使用load方式加载数据,而需采用insert into方式。
部署运行你感兴趣的模型镜像

默认存储格式textfile


textfile:普通的文本文件存储,不压缩
sequencefile:hive为用户提供的二进制存储,本身就压缩,不能使用load的方式加载数据
rcfile:hive提供的行列混合存储,hive在该格式下,会尽量将附近的行和列得块存储到一起,仍然是压缩格式,查询效率比较高
orc:orf是rcfile的升级版

首先是默认的:textfile

create table if not exists text2(
uid int,
uname string,
sex string,
age int,
step string
)
row format delimited
fields terminated by ','
stored as textfile
;

加载数据:

load data local inpath '/root/students.txt' into table text2;

然后创建存储格式为sequencefile的

create table if not exists seq1(
uid int,
uname string,
sex string,
age int,
step string
)
row format delimited
fields terminated by ','
stored as sequencefile
;

加载数据:

load data local inpath '/root/students.txt' into table seq1;

注意,这种方式不能load加载数据的!

提示:

需要用insert into的方式来加载数据:

insert into seq1
select * from text2;

结果为:

然后是rcfile格式的:

create table if not exists rc1(
uid int,
uname string,
sex string,
age int,
step string
)
row format delimited
fields terminated by ','
stored as rcfile
;

加载数据的时候也是不能load数据

不可以使用这种方式加载数据
load data local inpath '/root/students.txt' into table rc1;#报错!!!

应该使用insert into的方式加载数据

insert into rc1
select * from text2;

 

 

 

 

您可能感兴趣的与本文相关的镜像

Facefusion

Facefusion

AI应用

FaceFusion是全新一代AI换脸工具,无需安装,一键运行,可以完成去遮挡,高清化,卡通脸一键替换,并且Nvidia/AMD等显卡全平台支持

<think>我们专注于用户的新问题:关于Hive存储格式中InputFormat的相关信息。 根据引用[1]:Hive利用Hadoop的InputFormat API从不同数据源读取数据,OutputFormat API将数据写成不同格式。不同的数据源或输出格式需要不同的InputFormat和OutputFormat实现。 因此,我们可以这样组织回答: 1. 解释InputFormatHive中的作用:它是Hadoop的一个核心抽象,负责读取数据并分割成键值对,供Hive处理。 2. 说明不同存储格式对应的不同InputFormat实现类。 3. 举例常见存储格式的InputFormat类。 4. 简要说明如何自定义InputFormat。 同时,我们注意到引用[2]中列出了Hive支持的存储格式,我们可以对应给出这些格式的InputFormat类。 另外,引用[3]提到的一些优化点虽然与存储格式相关,但用户的问题集中在InputFormat上,所以我们主要围绕InputFormat展开。 回答结构: 1. InputFormatHive中的角色 2. 常见存储格式的InputFormat实现 3. 自定义InputFormat的简要说明 4. 注意事项 然后,生成相关问题。 注意:在回答中,我们引用参考内容时,需要在末尾添加引用标识,如[^1]。 </think>### Hive存储格式中的InputFormat详解 #### 1. **InputFormat的核心作用** 在Hive中,`InputFormat`是Hadoop的核心API组件,负责: - **数据读取**:从不同存储系统(HDFS/S3等)读取原始数据块 - **数据分片**:将大数据集分割成可并行处理的`InputSplit` - **记录解析**:将原始字节流转换成键值对`<key, value>`格式 - **格式适配**:支持不同存储格式的解码逻辑 > *Hive的强大之处在于不强制数据转换格式,而是通过`InputFormat`适配多种数据源[^1]* #### 2. **常见存储格式的InputFormat实现** | 存储格式 | InputFormat类 | 核心功能特性 | |---------------|----------------------------------------|------------------------------------------| | **TEXTFILE** | `TextInputFormat` | 按行读取文本,键为字节偏移量,值为文本行 | | **ORC** | `OrcInputFormat` | 读取ORC文件索引,支持谓词下推和列裁剪 | | **PARQUET** | `MapredParquetInputFormat` | 处理Parquet页式结构,优化列访问 | | **SEQUENCEFILE** | `SequenceFileInputFormat` | 读取二进制KV格式,支持块压缩 | | **AVRO** | `AvroContainerInputFormat` | 解析Avro Schema,支持模式演进 | #### 3. **InputFormat工作流程** 1. **分片阶段** `InputFormat.getSplits()`将文件按块/行边界分割 ```java // 示例:TextInputFormat分片逻辑 splits = file.getBlockLocations().map(block => new FileSplit(block.getPath(), block.getOffset(), block.getLength()) ) ``` 2. **记录读取阶段** 每个Map任务调用`InputFormat.createRecordReader()`逐条解析记录 $$ \text{RecordReader} \rightarrow \langle \text{LongWritable(offset)}, \text{Text(line)} \rangle $$ 3. **格式解码** 特定格式的`RecordReader`实现数据反序列化(如ORC使用`OrcStruct`对象) #### 4. **自定义InputFormat开发** 当需要读取特殊数据源时,需实现: ```java public class CustomInputFormat extends FileInputFormat<LongWritable, Text> { @Override public RecordReader<LongWritable, Text> createRecordReader( InputSplit split, TaskAttemptContext context) { return new CustomRecordReader(); } // 示例自定义RecordReader static class CustomRecordReader extends RecordReader<LongWritable, Text> { public boolean nextKeyValue() { // 实现自定义解析逻辑 } } } ``` 在Hive中注册: ```sql CREATE TABLE custom_table(...) STORED AS INPUTFORMAT 'com.example.CustomInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' ``` #### 5. **关键注意事项** 1. **性能影响** ORC/Parquet的列式`InputFormat`比Textfile的行式读取减少70%以上I/O[^3] 2. **格式兼容** 修改存储格式需同步更新`INPUTFORMAT`类(如TEXTFILE→ORC) 3. **压缩支持** InputFormat需与压缩编解码器协同工作(如`OrcInputFormat+SNAPPY`) 4. **谓词下推** 列式格式的InputFormat可将过滤条件下推到存储层[^3] $$ \text{Query: } \sigma_{age>30}(table) \Rightarrow \text{ InputFormat仅读取满足条件的行组} $$ > *InputFormat的选型直接影响查询效率,列式格式在分析场景中通常优于行式格式[^2][^3]* --- ### 相关问题 1. **如何验证自定义InputFormatHive中的正确性?有哪些调试技巧?** 2. **ORC格式的InputFormat如何实现谓词下推?它与Parquet的实现机制有何差异?** 3. **在读取加密数据时,InputFormat需要哪些额外配置?如何保证数据安全性?** 4. **Hive的InputFormat如何处理嵌套数据结构(如JSON/Protobuf)?**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值