当前位置:   article > 正文

Hive中的数据类型和存储格式总结

Hive中的数据类型和存储格式总结

1.数据类型

Hive 支持多种数据类型,分为原始数据类型和复杂数据类型两类。以下是 Hive 支持的数据类型:

原始数据类型:

        1.整数类型:

                tinyint: 1字节有符号整数
                smallint: 2字节有符号整数
                int: 4字节有符号整数
                bigint: 8字节有符号整数
                float: 4字节单精度浮点数
                double: 8字节双精度浮点数
                decimal: 高精度数字类型,可以指定精度和标度,例如 decimal(10,2)

        字节(Byte):计算机中最基本的存储单元之一,1字节占8位(bit)  ,数据范围:负数范围: -128 到 -1,正数范围: 0 到 127   

        2.字符串类型:

                string: 可变长度字符串
                varchar: 具有最大长度限制的可变长度字符串,例如 varchar(255)
                char: 固定长度字符串,例如 char(10)

        3.日期/时间类型:

                timestamp: 包含日期和时间的时间戳,精确到纳秒
                date: 仅包含日期部分,不包含时间部分
                interval: 时间间隔,用于表示两个日期或时间之间的差值

        4.Boolean类型:

                boolean: 布尔值,取值为 true 或 false

        5.二进制类型:

                binary: 任意长度的字节数组

复杂数据类型:
        1.数组类型

        array<T>: 包含多个相同类型元素的有序列表,例如 array<int>

        2.映射类型

        map<K, V>: 键值对的无序集合,其中键和值可以是任意数据类型,例如 map<string, int>


        3.结构类型

        struct<col1: type1, col2: type2, ...>: 包含多个字段的记录,每个字段可以是不同的数据类型,例如 struct<name: string, age: int>

  1. CREATE TABLE example_table (
  2. tinyint_col tinyint,
  3. smallint_col smallint,
  4. int_col int,
  5. bigint_col bigint,
  6. float_col float,
  7. double_col double,
  8. decimal_col decimal(10, 2),
  9. string_col string,
  10. varchar_col varchar(255),
  11. char_col char(10),
  12. timestamp_col timestamp,
  13. date_col date,
  14. boolean_col boolean,
  15. binary_col binary,
  16. array_col array<int>,
  17. map_col map<string, int>,
  18. struct_col struct<name: string, age: int>,
  19. union_col uniontype<int, string>
  20. );

2.Hive的文件存储格式

hive的存储格式分为两大类:

一类纯文本文件:textfile,不压缩,也是hive的默认存储格式

一类是二进制文件存储:    

sequencefile:会压缩,不能使用load方式加载数据

orcfile:会压缩,不能使用load方式加载数据

parquet:会压缩,不能使用load方式加载数据

rcfile:会压缩,不能使用load方式加载数据,是orcfile的低配

textfile和sequencefile的存储格式都是基于行存储的;orc和parquet是基于列式存储的,rcfile是行列混合存储。

在创建表格的时候可以使用stored as parquet指定表格的存储格式,例:

  1. create table if not exists stocks_parquet (
  2. track_time string,
  3. url string,
  4. session_id string,
  5. referer string,
  6. ip string,
  7. end_user_id string,
  8. city_id string
  9. )
  10. stored as parquet;

修改hive的默认存储格式:

  1. <property>
  2. <name>hive.default.fileformat</name>
  3. <value>TextFile</value>
  4. <description>
  5. Expects one of [textfile, sequencefile, rcfile, orc].
  6. Default file format for CREATE TABLE statement. Users can explicitly override it by CREATE TABLE ... STORED AS [FORMAT]
  7. </description>
  8. </property>
  9. 也可以使用set方式修改:
  10. set hive.default.fileformat=TextFile

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/817126
推荐阅读
相关标签
  

闽ICP备14008679号