Hive中的DECIMAL类型

(Decimal)小数点

Hive中的DECIMAL类型与Java的Big Decimal格式相同。它用于表示不变的任意精度。语法和示例如下:

  •  
DECIMAL(precision, scale)decimal(10,0)

 

https://mp.weixin.qq.com/s/BUk8Y7-rOiBzUnseekqmGg

1、 基本类型

 

这里我们对DECIMAL类型做两点说明:
1)DECIMAL(9,8)代表最多9位数字,后8位是小数。此时也就是说,小数点前最多有1位数字,如果超过一位则会变成null。
2)如果不指定参数,那么默认是DECIMAL(10,0),即没有小数位,此时0.82会变成1。

 

这里我们通过一个SQL来进行说明:

创建表:

create table if not existsdatatype_test1(id int,col1 decimal,col2 decimal(9,8)) row format delimited fields terminated by ',';

我们从txt中读取数据:​​​​​​​

load data local inpath '/Users/meituan_sxw/Downloads/test1.txt' into table datatype_test1;
#txt中的内容1,0.82,83.22,1.06,9.22

接下来查看hive中的数据:

 select * from datatype_test1;

 

类型转换:

https://cwiki.apache.org/confluence/display/Hive/Tutorial

The Types are organized in the following hierarchy (where the parent is a super type of all the children instances):

  • Type
    • Primitive Type

      • Number

        • DOUBLE

          • FLOAT

            • BIGINT

              • INT

                • SMALLINT

                  • TINYINT

          • STRING

      • BOOLEAN

This type hierarchy defines how the types are implicitly converted in the query language. Implicit conversion is allowed for types from child to an ancestor. So when a query expression expects type1 and the data is of type2, type2 is implicitly converted to type1 if type1 is an ancestor of type2 in the type hierarchy. Note that the type hierarchy allows the implicit conversion of STRING to DOUBLE.

Explicit type conversion can be done using the cast operator as shown in the #Built In Functions section below.

 

 

 

 

 

 

 

 

 

  • 11
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 当在Hive查询decimal类型字段时报错,可能是由于以下原因导致的。首先,Hivedecimal字段默认精度为0,如果不指定小数点,数据将被四舍五入。例如,0.82在Hive会变成1。因此,在建表时最好指定精度,如decimal(10,2)。\[1\]其次,当使用Sqoop导出数据时,需要注意设置--input-null-string '\\N'和--input-null-non-string '\\N'这两个参数。因为Hive的null默认是\N,而Sqoop在导出数据时会解析\N。如果目标关系型数据库的表字段不是string类型,就会报错。通过设置这两个参数,Sqoop会知道\N表示null,而不会将其解析为字符。\[2\]最后,如果在修改了字段类型后,表的schema发生了变化,而之前的分区仍然使用旧的schema,就会导致表和分区的schema不一致,从而导致select报错。在这种情况下,需要更新分区的schema以与表的schema保持一致。\[3\] #### 引用[.reference_title] - *1* *2* [Hadoop记录——hivedecimal字段,shell的数据类型以及sqoop导出空string报错](https://blog.csdn.net/chenKFKevin/article/details/53199925)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [hive字段类型修改问题总结](https://blog.csdn.net/qq_36302506/article/details/117091191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值