美文网首页
Hive小技巧

Hive小技巧

作者: xxlee | 来源:发表于2020-08-12 16:00 被阅读0次

转载请在文章起始处注明出处,谢谢。


1、取字符串某个位置的值

比如,我要取‘abcd’的b,有两种取法:

select split('abcd','')[1]
select substr('abcd',2,1)

2、我要取a表的用户,b表的用户,还有a、b重复的用户

这里可以用sum() over(partition by) 来取,下面举个例子:
假如a表和b表结构一样,都有dt,pin,type。


取数代码示意如下:

set hive.merge.mapfiles = true;
set hive.merge.mapredfiles = true;
set hive.merge.size.per.task = 512000000;
set hive.merge.smallfiles.avgsize = 512000000;
set mapred.max.split.size = 512000000;
set mapred.min.split.size.per.node = 512000000;
set mapred.min.split.size.per.rack = 512000000;
set hive.input.format = org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;
set hive.hadoop.supports.splittable.combineinputformat = true;
set hive.exec.dynamic.partition = true;
set hive.exec.dynamic.partition.mode = nostrict;
set hive.exec.max.dynamic.partitions.pernode = 1000;
set hive.exec.parallel = true;
set hive.exec.parallel.thread.number = 16;
set hive.new.job.grouping.set.cardinality = 5000;
select
    t2.dt,
    count(distinct case
        when t2.t = 1 then t2.pin
        end) a的人数,
    count(distinct case
        when t2.t = 2 then t2.pin
        end) b的人数,
    count(distinct case
        when t2.t >= 3 then t2.pin
        end) ab重复人数
from 
    (
        select
            t1.dt,
            t1.pin,
            t1.type,
            sum(t1.type) over(partition by t1.pin) t
        from 
            (
                表a a 
                union all
                表b b 
            )
            t1 
        group by 
            t1.dt,
            t1.pin,
            t1.type
    )
    t2
group by 
    t2.dt

注意:t2子查询里的t1.type是一定要写的,不然会报错:

FAILED: SemanticException Failed to breakup Windowing invocations into Groups. At least 1 group must only depend on input columns. Also check for circular dependencies.

Underlying error: org.apache.hadoop.hive.ql.parse.SemanticException: Line 4:16 Invalid column reference 'type'

相关文章

  • hive 小技巧

    show create table [表名];获取已经存在表的建表ddl

  • Hive小技巧

    转载请在文章起始处注明出处,谢谢。 1、取字符串某个位置的值 比如,我要取‘abcd’的b,有两种取法: 2、我要...

  • HIVE技巧

    参考[https://blog.csdn.net/erinapple/article/details/837454...

  • 阿里云搭建CDH(Step 3: 搭建Hive)

    安装 官方文档 PS. 找一台压力小的机器 安装hive 修改Hive配置文件 vim /etc/hive/co...

  • 【转载】hive使用技巧

    自动化动态分配表分区及修改hive表字段名称 1、自动化动态分配表分区 set hive.exec.dynamic...

  • 数据仓库Hive

    Hive产生背景 Hive概述 HIve体系架构 Hive部署架构 Hive和RDBMS区别 Hive部署以及快速...

  • 数据查询-Hive基础

    outline 什么是Hive 为什么需要Hive Hive的架构 Hive的常用操作 什么是Hive Hive由...

  • hive虚拟列

    火山日常啰嗦 讲讲hive的小知识点--虚拟列 hive虚拟列有两种: 1)INPUT__FILE__NAME 输...

  • hive小总结

    hive 是Hadoop上的组件,是一种数据仓库,是在HDFS和mapreduce 两个引擎上的。 数据仓库 数据...

  • 大数据知识 | hive初识

    hive简介 hive架构 hive是什么 官网这样说:https://hive.apache.org/ hive...

网友评论

      本文标题:Hive小技巧

      本文链接:https://www.haomeiwen.com/subject/bpfmdktx.html