• 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

     

  • 相关阅读:
    一道B树的题目---先记一下, 还没看到B树
    一道二叉树的题目--后序遍历+中序遍历确定二叉树
    sonar 匿名内部类写法不推荐
    Springboot读取Jar文件中的resource
    一次单体测试的采坑--MatcherAssert.assertThat---org.hamcrest 和org.mockito
    B+树和B-树的区别
    一道二叉树题目--二叉树的顺序存储
    一道二叉树题目--根据先序序列和中序序列重画二叉树
    Jekins相关笔记
    POJ 1679 The Unique MST (最小生成树 Kruskal )
  • 原文地址:https://www.cnblogs.com/shimingjie/p/11944955.html
Copyright © 2020-2023  润新知