分享

hql中的数据类型转换

回帖奖励 12 云币      回复本帖可获得 3 云币奖励! 每人限 1 次
我的数据格式是
machine_id,channel_id, start_time, end_time
31415926,100,20160201142334,20160201145832
31415926,103,20160201145839,20160201165832
31415926,108,20160201172334,20160201195832
31415928,103,20160201012334,20160201145832
31415928,100,20160201172334,20160201225832
31415928,108,20160201232334,20160201235832


都是String类型。

那么现在要写hql语句去查 表中 从20160201140000 到 20160201160000 观看了 103 的机器有多少个,

select count(distinct  machine_id) from table_name where  ( (20160201140000 < cast(start_time as int)) And (cast(start_time as int)< 20160201160000) ) OR ((20160201140000 < cast(end_time as int)) And (cast(end_time as int)<20160201160000));

悍然报错,NoViableAltException(292@[])
FAILED: ParseException line 1:30 cannot recognize input near 'where' '(' '(' in join source

我在网上找的 hql 得数据类型转换,从String 类型转到 int 类型是这样转吧? cast(string as int)

那么我这个hql语句写错了没呢?    有没有关于hql 语句这方面的资料可以推荐的呢?

多谢!

已有(3)人评论

跳转到指定楼层
tntzbzc 发表于 2016-6-22 17:31:11

回帖奖励 +3 云币

20160201140000 这个是字符串,还是整形,他们之间需要类型一致。另外可以通过union的方式将结果聚合
回复

使用道具 举报

liuzhixin137 发表于 2016-6-23 09:33:34
tntzbzc 发表于 2016-6-22 17:31
20160201140000 这个是字符串,还是整形,他们之间需要类型一致。另外可以通过union的方式将结果聚合

String类型。

您是指,将语句分开,然后用union聚合起来吗?

select  *  from table_name where   (20160201140000 < cast(start_time as int)) And (cast(start_time as int)< 20160201160000)  union   select *  from table_name where (20160201140000 < cast(end_time as int)) And (cast(end_time as int)<20160201160000);

这样吗,我来试试


如图中,我分开测试,还是不行,就是String类型转换成 int 型为什么一直不能成功,而且
类似于这样的hql语句
select * from table_name where start_time = 20160201143523;
为什么不转化为一个 mapreduce 执行,而是直接给出了结果?

hive

hive


回复

使用道具 举报

liuzhixin137 发表于 2016-6-23 10:15:50
我修改了表的格式,将
start_time   end_time 格式改为了 bigint 测试成功。由此想到int能表示的数据范围。

int型:使用此数据类型时,存储数据的范围是-2的31次方到2的31次方-1,占用4个字节的储存空间。
bigint型:使用此数据类型时,存储数据的范围的是从-2的63次方到2的63次方-1,占用8个自己的储存空间。

14位的日期超过了int能表示的数据范围。修改为bigint就可以了。那么,之前的hql语句也没有错,只要将int 改为 bigint 就OK了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条