Hive query

Official website

Basic query (Select…From) 

  1. Full table and specific field query

    1) Full table query hive (default)> select * from emp;
    2) Select specific column query hive (default)> select empno, ename from emp;
    3) Pay attention The HQL language is case insensitive. 
    HQL can be written on one or more lines.
    Keywords cannot be abbreviated or divided into lines
    . Generally, clauses should be written in lines.
    Use indentation to improve the readability of the statement.
  2. The column alias
    follows the column name, and the keyword 'AS' can also be added between the column name and the alias 
    .

    Look up name and department hive (default)> select ename AS name, deptno dn from emp;
  3. Arithmetic operator


    case practice: hive (default)> select sal +1 from emp;
  4. Common functions

    1) Find the total number of rows (count) hive (default)> select count(*) cnt from emp;
    2) Find the maximum salary (max) hive (default)> select max(sal) max_sal from emp;
    3) Find the minimum wage (min) hive (default)> select min(sal) min_sal from emp;
    4) Find the sum of wages (sum) hive (default)> select sum(sal) sum_sal from emp; 
    5) Find the average salary (avg) hive (default)> select avg(sal) avg_sal from emp;

  5. A typical query with a Limit statement will return multiple rows of data. The LIMIT clause is used to limit the number of rows returned.
    Case practice: hive (default)> select * from emp limit 5;

Where statement

Use the WHERE clause to filter out rows that do not meet the conditions. The WHERE clause immediately follows the FROM clause. Case practice:

Query all employees with salary greater than 1000 ===>hive (default)> select * from emp where sal >1000;

  1. Comparison Operators (Between/In/ Is Null)
    The following table describes the predicate operators, which can also be used in JOIN...ON and HAVING statements.

    Case practice
    (1) Query all employees whose salary is equal to 5000 hive (default)> select * from emp where sal =5000;
    (2) Query the information of employees whose salary is between 500 and 1000 hive (default)> select * from emp where sal between 500 and 1000;
    (3) Query the information of all employees whose comm is empty hive (default)> select * from emp where comm is null;
    (4) Query the information of employees whose salary is 1500 and 5000 hive (default)> select * from emp where sal IN (1500, 5000);
  2. Like和RLike

    1) Use the LIKE operation to select similar values  
    2) Selection criteria can contain characters or numbers: % represents zero or more characters (any number of characters). _ represents a character.
    3) The RLIKE clause is an extension of this function in Hive, which can specify matching conditions through the more powerful language of Java's regular expressions.  
    4) Case practice

    (1) Find the information of employees whose salary starts with 2 hive (default)> select * from emp where sal LIKE '2%';
    (2)查找第二个数值为2的薪水的员工信息 hive (default)> select * from emp where sal LIKE '_2%';
    (3)查找薪水中含有2的员工信息 hive (default)> select * from emp where sal RLIKE '[2]';

  3. 逻辑运算符(And/Or/Not)


    案例实操:

    (1)查询薪水大于1000,部门是30 hive (default)> select * from emp where sal>1000 and deptno=30;
    (2)查询薪水大于1000,或者部门是30 hive (default)> select * from emp where sal>1000 or deptno=30;
    (3)查询除了20部门和30部门以外的员工信息 hive (default)> select * from emp where deptno not IN(30, 20);

分组

  1. Group By语句
    GROUP BY语句通常会和聚合函数一起使用,按照一个或者多个列队结果进行分组,然后对每个组执行聚合操作。
    案例实操:

    (1)计算emp表每个部门的平均工资 hive (default)> select t.deptno, avg(t.sal) avg_sal from emp t group by t.deptno;
    (2)计算emp每个部门中每个岗位的最高薪水 hive (default)> select t.deptno, t.job, max(t.sal) max_sal from emp t group by
    t.deptno, t.job;
  2. Having语句
    having与where不同点
        1)where针对表中的列发挥作用,查询数据;having针对查询结果中的列发挥作用,筛选数据。
        2)where后面不能写分组函数,而having后面可以使用分组函数。
        3)having只用于group by分组统计语句。
    案例实操:

    求每个部门的平均薪水大于2000的部门 求每个部门的平均工资
    hive (default)> select deptno, avg(sal) avg_sal from emp group by deptno;
    求每个部门的平均薪水大于2000的部门
    hive (default)> select deptno, avg(sal) avg_sal from emp group by deptno
    having avg_sal > 2000;

Join语句

  1. 等值Join
    Hive支持通常的SQL JOIN语句,但是只支持等值连接,不支持非等值连接。
    案例操作

    根据员工表和部门表中的部门编号相等,查询员工编号、员工名称和部门编号; hive (default)> select e.empno, e.ename, d.deptno, d.dname from emp e join dept d on e.deptno = d.deptno;
  2. 表的别名
    好处:使用别名可以简化查询。使用表名前缀可以提高执行效率。
    案例实操

    合并员工表和部门表 hive (default)> select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;
  3. 内连接
    内连接:只有进行连接的两个表中都存在与连接条件相匹配的数据才会被保留下来。
    hive (default)> select e.empno, e.ename, d.deptno from emp e join dept d on e.deptno = d.deptno;
  4. 左外连接
    JOIN操作符左边表中符合WHERE子句的所有记录将会被返回。
    hive (default)> select e.empno, e.ename, d.deptno from emp e left join dept d on e.deptno = d.deptno;
  5. 右外连接
    JOIN操作符右边表中符合WHERE子句的所有记录将会被返回。
    hive (default)> select e.empno, e.ename, d.deptno from emp e right join dept d on e.deptno = d.deptno;
  6. 满外链接
    将会返回所有表中符合WHERE语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用NULL值替代。
    hive (default)> select e.empno, e.ename, d.deptno from emp e full join dept d on e.deptno = d.deptno;
  7. 多表连接
    注意:连接 n个表,至少需要n-1个连接条件。例如:连接三个表,至少需要两个连接条件。
    0)数据准备:location.txt
    1)创建位置表
        
    create table if not exists default.location(
    loc int,
    loc_name string
    )
    row format delimited fields terminated by '\t';
    2)导入数据

    hive (default)> load data local inpath '/opt/module/datas/location.txt' into table default.location;
    3)多表连接查询

    hive (default)>SELECT e.ename, d.deptno, l. loc_name
    FROM   emp e 
    JOIN   dept d
    ON     d.deptno = e.deptno 
    JOIN   location l
    ON     d.loc = l.loc;
    大多数情况下,Hive会对每对JOIN连接对象启动一个MapReduce任务。本例中会首先启动一个MapReduce job对表e和表d进行连接操作,然后会再启动一个MapReduce job将第一个MapReduce job的输出和表l;进行连接操作。
    注意:为什么不是表d和表l先进行连接操作呢?这是因为Hive总是按照从左到右的顺序执行的。
  8. 笛卡尔积 JOIN
    笛卡尔集会在下面条件下产生:
        1)省略连接条件
        2)连接条件无效
        3)所有表中的所有行互相连接
    案例实操
    hive (default)> select empno, deptno from emp, dept;

排序

  1. 全局排序(Order By)
    全局排序,一个MapReduce
    1)使用 ORDER BY 子句排序
        ASC(ascend): 升序(默认)
        DESC(descend): 降序
    2)ORDER BY 子句在SELECT语句的结尾
    3)案例实操

    (1)查询员工信息按工资升序排列 hive (default)> select * from emp order by sal;
    (2)查询员工信息按工资降序排列 hive (default)> select * from emp order by sal desc;
  2. 按照别名排序

    按照员工薪水的2倍排序 hive (default)> select ename, sal*2 twosal from emp order by twosal;
  3. 多个列排序

    按照部门和工资升序排序 hive (default)> select ename, deptno, sal from emp order by deptno, sal ;
  4. 每个MapReduce内部排序(Sort By)
    每个MapReduce内部进行排序,对全局结果集来说不是排序
    将查询结果导入到文件中(按照部门编号降序排序)
    hive (default)> insert overwrite local directory '/opt/module/datas/sortby-result' select * from emp sort by deptno desc;
  5. 分区排序(Distribute By)
    类似MR中partition,进行分区,结合sort by使用。注意,Hive要求DISTRIBUTE BY语句要写在SORT BY语句之前
    先按照部门编号分区,再按照员工编号降序排序。
    hive (default)> insert overwrite local directory '/opt/module/datas/distby-desc' select * from emp distribute by deptno sort by empno desc;
  6. Cluster By
    当distribute by和sorts by字段相同时,可以使用cluster by方式。cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒序排序,不能指定排序规则为ASC或者DESC。
    以下两种写法等价
    select * from emp cluster by deptno;
    select * from emp distribute by deptno sort by deptno;
    注意:按照部门编号分区,不一定就是固定死的数值,可以是20号和30号部门分到一个分区里面去。

分桶及抽样查询

  1. 分桶表数据存储
    1)先创建分桶表,通过直接导入数据文件的方式
        
    (0)数据准备
    student.txt
    (1)创建分桶表 create table stu_buck(id int, name string)
    clustered by(id) 
    into 4 buckets
    row format delimited fields terminated by '\t';
    (2)查看表结构 hive (default)> desc formatted stu_buck;
    Num Buckets:            4     
    (3)导入数据到分桶表中 hive (default)> load data local inpath '/opt/module/datas/student.txt' into table stu_buck;
    (4)查看创建的分桶表中是否分成4个桶 发现并没有分成4个桶。是什么原因呢?(往后面看,需要设置参数)
    2)创建分桶表时,数据通过子查询的方式导入

    (1)先建一个普通的stu表 create table stu(id int, name string)
    row format delimited fields terminated by '\t';
    (2)向普通的stu表中导入数据 load data local inpath '/opt/module/datas/student.txt' into table stu;
    (3)清空stu_buck表中数据     truncate table stu_buck;
    select * from stu_buck;
    (4)导入数据到分桶表,通过子查询的方式 insert into table stu_buck
    select id, name from stu cluster by(id);
    (5)需要设置一个属性 hive (default)>set hive.enforce.bucketing=true;
    hive (default)> set mapreduce.job.reduces=-1;
    hive (default)>insert into table stu_buck
    select id, name from stu cluster by(id);
    (6)查询分桶的数据 hive (default)> select * from stu_buck;
    OK
    stu_buck.id     stu_buck.name
    1001    ss1
    1005    ss5
    1009    ss9
             .
             .
             .
  2. 分桶抽样查询
    对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求。
    查询上表stu_buck的数据。
    hive (default)> select * from stu_buck TABLESAMPLE(bucket 1 out of 4 on id);
    注:tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y) 。
    y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了4份,当y=2时,抽取(4/2=)2个bucket的数据,当y=8时,抽取(4/8=)1/2个bucket的数据。
    x表示从哪个bucket开始抽取。例如,table总bucket数为4,tablesample(bucket 4 out of 4),表示总共抽取(4/4=)1个bucket的数据,抽取第4个bucket的数据。
    注意:x的值必须小于等于y的值,否则
    FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck
  3. 数据块抽样
    Hive提供了另外一种按照百分比进行抽样的方式,这种事基于行数的,按照输入路径下的数据块百分比进行的抽样。
    hive (default)> select * from stu tablesample(0.1 percent);
    提示:这种抽样方式不一定适用于所有的文件格式。另外,这种抽样的最小抽样单元是一个HDFS数据块。因此,如果表的数据大小小于普通的块大小128M的话,那么将会返回所有行。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325853470&siteId=291194637