zoukankan      html  css  js  c++  java
  • Hive 使用总结

    1 带分区列的表更改列类型

    常见的一个场景是Hive里面一个带分区的表,原来是int类型的字段,后来发现数据超过了int的最大值,要改成bigint。或者是bigint要改string或decimal。无论如何,对于带分区的表,要改列类型,有一个坑:

    如果使用alter table t change column oldcol newcol bigint,即把int类型的oldcol改为bigint类型的newcol
    
    这个时候,去读数据,应该还是NULL的。
    
    这是因为每个分区Hive还会存一份元数据,于是两种解决方案:
    
    一个是alter table t change column oldcol newcol bigint cascade
    
    一个是alter table t change column oldcol newcol bigint, alter table t partition(...) change column oldcol newcol bigint

    2 left join 问题(hive/mysql通用)

    在left join语句中,左表过滤必须放where条件中,右表过滤必须放on条件中,这样结果才能不多不少,刚刚好。

    3 行转列

    曾经一个场景使用过的
    select plan_id,id,
    concat_ws('',collect_set(if(user_type=1,user_id,''))) teacher_id,
    concat_ws('',collect_set(if(user_type=0,user_id,''))) student_id,
    concat_ws('',collect_set(if(user_type=1,count,''))) t_count,
    concat_ws('',collect_set(if(user_type=0,count,''))) s_count
    from tmp.table_name
    group by plan_id,id

    4 json字符串解析

    好像是网上找的
    一个Map结构嵌套了Map,再嵌套了一个数组结构。 {"username":"king","actionInfo":{"id":
    1,"age":"22","partList":[{"code":"123","uname":"king"},{"code":"0012","uname":"king"}]}} 通过json_tuple, get_json_object,explode等函数将string类型解析出来,使用正则的方式,将中括号替换掉,然后在转化为数组 select username,ai.id,ai.age,p.uname,p.code from test1 lateral view json_tuple(actioninfo,'id','age','partlist') ai as id,age,partlist lateral view explode(split(regexp_replace(regexp_extract(partlist,'^\[(.+)\]$',1),'\}\,\{', '\}\|\|\{'),'\|\|')) partlist as p lateral view json_tuple(p,'code','uname') p as code,uname

     5 四舍五入函数

    round() 遵循四舍五入把原值转化为指定小数位数,如:round(1.45,0) = 1;round(1.55,0)=2
    floor()向下舍入为指定小数位数 如:floor(1.45,0)= 1;floor(1.55,0) = 1
    ceiling()向上舍入为指定小数位数 如:ceiling(1.45,0) = 2;ceiling(1.55,0)=2

     

  • 相关阅读:
    Java实现蓝桥杯模拟组织晚会
    Java实现蓝桥杯模拟组织晚会
    ffmpeg+rtsp+dss
    开发ffmpeg/live555常见问题错误及解决方法
    SxsTrace工具使用方法
    移植strace调试工具到arm平台
    Linux on Power 上的调试工具和技术
    使用 Strace 和 GDB 调试工具的乐趣
    自助Linux之问题诊断工具strace
    通用Makefile
  • 原文地址:https://www.cnblogs.com/shimingjie/p/11944955.html
Copyright © 2011-2022 走看看