【入门】what is apache orc?提高hive存储效率?怎么和hive搭配使用?

ORC是一种针对Hadoop优化的列式存储文件格式,旨在提高Hive的存储效率和读取速度。它支持Hive的所有数据类型,包括复杂类型,并通过条带和内部索引实现高效查询。Hive通过添加STOREDASORC将表存储为ORC格式,利用tableproperties控制其行为。

一. 什么是orc

官网:https://orc.apache.org/docs/

1.ORC files目标为了提高hive的存储效率,以及减少文件大小。

Back in January 2013, we created ORC files as part of the initiative to massively speed up Apache Hive and improve the storage efficiency of data stored in Apache Hadoop. The focus was on enabling high speed processing and reducing file sizes.

 
2.ORC是一种为Hadoop工作负载设计的自描述type感知式(ing) 的列格式存储文件。

ORC is a self-describing type-aware columnar file format designed for Hadoop workloads. It is optimized for large streaming reads, but with integrated support for finding required rows quickly. Storing data in a columnar format lets the reader read, decompress, and process only the values that are required for the current query.

 
3.提高大型流数据的读效率

在写入文件时ORC这边会构建一个内部索引。
其中当谓词下推使用这些索引来确定需要为特定查询读取文件中的哪些stripes,而行索引可以将搜索范围缩小到10,000行的特定集合。

Because ORC files are type-aware, the writer chooses the most appropriate encoding for the type and builds an internal index as the file is written.
 
Predicate pushdown uses those indexes to determine which stripes in a file need to be read for a particular query and the row indexes can narrow the search to a particular set of 10,000 rows.

 
4.ORC支持Hive中的完整类型集,包括复杂类型:结构、列表、映射和联合。

ORC supports the complete set of types in Hive, including the complex types: structs, lists, maps, and unions.

 
5. stripes的概念

ORC文件被划分为条,默认情况下大约为64MB。文件中的条彼此独立,形成分布式工作的自然单元。
在每个条带中,列之间是分开的,因此writer只能读取所需的列。

ORC files are divided in to stripes that are roughly 64MB by default. The stripes in a file are independent of each other and form the natural unit of distributed work. Within each stripe, the columns are separated from each other so the reader can read just the columns that are required.

 
 

二. hive集成orc

if the table is ORC. Using show create table, you get this:

ORC is well integrated into Hive, so storing your istari table as ORC is done by adding “STORED AS ORC”.

CREATE TABLE istari (
  name STRING,
  color STRING
) STORED AS ORC;

具体的实现类

STORED AS INPUTFORMAT
  ‘org.apache.hadoop.hive.ql.io.orc.OrcInputFormat’
OUTPUTFORMAT
  ‘org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat’

 
To modify a table so that new partitions of the istari table are stored as ORC files:

ALTER TABLE istari SET FILEFORMAT ORC;

 
 

三. hive表属性

Tables stored as ORC files use table properties to control their behavior. By using table properties, the table owner ensures that all clients store data with the same options.
 
所有的客户端在储存数据时都会有按照相同的属性操作。

在这里插入图片描述

官网:hive-config

 
表中添加参数:

CREATE TABLE istari (
  name STRING,
  color STRING
) STORED AS ORC TBLPROPERTIES ("orc.compress"="NONE");
### 3.1 配置 Spark 环境以支持 Hive ORC 表读取 在使用 Spark 读取 Hive 中的 ORC 表之前,需要确保 Spark 环境正确配置了 Hive 支持。可以通过在 Spark 的配置文件 `spark-defaults.conf` 中添加以下内容启用 Hive 支持: ```properties spark.sql.catalogImplementation hive spark.sql.hive.convertMetastoreOrc true ``` 此外,需要将 Hive 的元数据库配置信息(如 metastore URI)添加到 Spark 的配置中,以确保 Spark 能够访问 Hive Metastore 中的元数据信息。例如: ```properties spark.hadoop.hive.metastore.uris thrift://hive-metastore-host:9083 ``` 这些配置确保 Spark SQL 能够正确解析 Hive 表的元数据,并在读取 ORC 表时自动识别其格式[^2]。 ### 3.2 使用 Spark SQL 读取 Hive ORC 表 Spark SQL 支持直接通过 SQL 查询读取 Hive 表。在 Spark Shell 或 Spark 应用程序中,可以使用如下方式读取 Hive 表数据: ```scala val spark = SparkSession.builder .appName("Read Hive ORC Table") .enableHiveSupport() .getOrCreate() spark.sql("SELECT * FROM hive_orc_table").show() ``` 该代码片段展示了如何启用 Hive 支持并查询 Hive ORC 表。`enableHiveSupport()` 方法会加载 Hive 的配置元数据,使得 Spark SQL 能够识别 Hive 表的结构存储格式。 ### 3.3 使用 DataFrame API 读取 Hive ORC 表 除了使用 Spark SQL,还可以使用 DataFrame API 读取 Hive ORC 表。这种方式更适用于需要进行复杂数据处理的场景: ```scala val df = spark.read.table("hive_orc_table") df.show() ``` 该方式通过 `read.table` 方法将 Hive 表加载为 DataFrame,从而可以使用 Spark 的 DataFrame API 进行数据转换分析操作。此外,DataFrame 提供了丰富的操作接口,如 `filter`、`groupBy`、`agg` 等,适用于大规模数据处理任务。 ### 3.4 数据处理与写回 Hive ORC 表 在读取 ORC 表数据后,可以对数据进行过滤、聚合等操作,并将处理后的结果写回到 Hive 表中。例如,执行按 key 分组的计数操作并写入新表: ```scala val processedDf = df.filter($"key".isNotNull) .groupBy("key") .count() processedDf.write .mode("overwrite") .format("orc") .saveAsTable("processed_orc_table") ``` 上述代码展示了如何对数据进行过滤分组统计,并将结果以 ORC 格式写入新的 Hive 表中。使用 `.format("orc")` 可确保输出数据为 ORC 格式,以便后续查询优化存储效率提升。 ### 3.5 常见问题与注意事项 在读取 Hive ORC 表时,可能会遇到 Spark 无法识别 ORC 文件格式的问题。可以通过设置 `spark.sql.hive.convertMetastoreOrc true` 来启用 Hive ORC 表的自动转换功能,从而避免格式兼容性问题[^2]。 此外,确保 Spark 的执行环境中有正确的 ORC 文件处理依赖,例如 `spark.sql.orc.impl` 配置项应设置为 `native`,以启用 Spark 原生的 ORC 文件读写支持: ```properties spark.sql.orc.impl native ``` 此配置可提升 ORC 文件的读写性能,并确保与 Hive 的兼容性。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

roman_日积跬步-终至千里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值