https://cwiki.apache.org/confluence/display/Hive/LanguageManual

一、创建表

create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

describe formatted student;

load data local inpath '/opt/datas/student.txt' into table test.student;

二、函数

show functions;

describe function extended upper;

select id, upper(name) from test.student;

三、设置

cat .hiverc
set hive.cli.print.header=true;
set hive.cli.print.current.db=true;

hive --hiveconf hive.root.logger=INFO,console

四、hive数据仓库位置配置

默认位置 /user/hive/warehouse

注意:

在仓库目录下,没有对默认的数据库default创建文件夹;

如果某张表属于default数据库,直接在数据仓库目录下创建一个文件夹,目录名称为表名;

在hive中使用dfs命令: dfs -rm -R /user/hive/warehouse/bf_log;

查看设置 set system:user.name;

执行设置 set system:user.name=beifeng;

此种方式进行设置,仅仅在当前session生效。

五、hive的几种交互方式

1. hive -e "select * from test.student;"

2. hive -f <filename>

hive -f hivef.sql > result.txt

3. hive -i <filename>

在hive cli命令窗口如何查看hdfs文件系统:

dfs -ls /

在hive cli命令窗口如何查看本地文件系统:

!ls /

六、数据类型

datatype:

1. primitive_type:

(1)Numeric Types:

TINYINT,SMALLINT,INT,BIGINT,FLOAT,DOUBLE,DECIMAL

(2)Date/Time Types

TIMESTARMP,DATE

(3)String Types

STRING,VARCHAR,CHAR

(4)Misc Types

BOOLEAN, BINARY

(5)Complex Types

arrays,maps,structs,union

2. array_type

3. map_type

4. struct_type

5. union_type

python

>> content

<< ip, req_url, http_ref

使用脚本语言

1)table,load   E

2) select, python T

3) sub table     L

七、表类型

1. MANAGED_TABLE

2. EXTERNAL_TABLE

3. 分区表

分区表实际上就是对应一个HDFS系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成更小的数据集。

在查询时通过where子句中的表达式来选择查询所需要的指定的分区,这样的查询效率会提高很多。

create table dept_partition

(

deptno int,

deptname string,

loc string

)

partitioned by (event_month string)

row format delimited fileds terminated by '\t';

msck repair table dept_partition;

alter table dept_partition add partition(day='20160720');

show partitions dept_partition;

八、导入数据的六大方式

1. 加载本地文件到hive表

load data local inpath 'filepath' into table default.emp;

2. 加载hdfs文件到hive中

load data inpath 'filepath' into table default.emp;

3. 加载数据覆盖表中已有的数据

load data inpath 'filepath' overwrite into table default.emp;

4. 创建表时通过insert加载

create table default.emp_cli like emp;

insert into table default.emp_cli select * from default.emp;

5. 创建表时通过select 加载

6. 创建表时通过location指定加载

九、导出数据的几种方式

1.

insert overwrite local directory '/opt/datas/hive_exp_emp' select * from default.emp;

2.

insert overwrite local directory '/opt/datas/hive_exp_emp3' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY '\n' select * from default.emp;

3.

hive -e "select * from default.emp" > /opt/datas/exp_res.txt

4.

insert overwrite directory '/user/hive/warehouse/hive_exp_emp4' select * from default.emp;

十、Hive常见查询

1. 常见select

select t.empno, t.ename, t.deptno from emp t;

select * from emp limit 5;

select t.empno, t.ename, t.deptno from emp t where t.sal between 800 and 1500;

select t.empno, t.ename, t.deptno from emp t where t.comm is null;

2.  max/min/count/sum/avg

desc function extended max;

select count(*) cnt from emp;

select max(sal) max_sal from emp;

select sum(sal) from emp;

select avg(sal) from emp;

3. group by / having 分组

(1)每个部门的平均工资

select t.deptno, avg(t.sal) as avg_sal from emp t group by t.deptno;

(2)每个部门中每个岗位的最高薪水

select t.deptno, t.job, avg(t.sal) as avg_sal from emp t group by t.deptno, t.job;

(3)having

where 是针对单条记录进行筛选

having 是针对分组结果进行筛选

(4)求每个部门的平均薪水大于2000的部门

select deptno, avg(sal) avg_sal from emp group by deptno having avg_sal > 2000;

4. join

(1)

等值join

m n

m表中一条记录和n表中的一条记录组成一条记录

join ...on

select e.empno, e.ename, e.deptno, d.dname from emp e join dept d on e.deptno = d.deptno;

(2)左连接

select e.empno, e.ename, e.deptno, d.dname from emp e left join dept d on e.deptno = d.deptno;

(3)右连接

select e.empno, e.ename, e.deptno, d.dname from emp e right join dept d on e.deptno = d.deptno;

十一、导入导出

1.  EXPORT:

导出,将Hive表中的数据,导出到外部;

EXPORT TABLE tablename TO 'export_target_path'

export_target_path: HDFS上的路径

EXPORT TABLE default.emp to '/user/hive/warehouse/export/emp_exp';

2. IMPORT:

导入,将外部数据导入Hive表中;

IMPORT [EXTENAL] TABLE new_orginal

create table db_hive_01.emp like default.emp;

import table db_hive_01.emp from '/user/hive/warehouse/export/emp_exp';

十二、order by、sort by、distribute by、cluster by

1. order by

全局排序,一个Reduce

select * from emp order by empno desc;

2. sort by

每个reduce内部进行排序,全局结果集不是排序

select * form emp sort by empno asc;

set mapreduce.job.reduces=3;

insert overwrite local directory '/opt/datas/sortby-res' select * from emp sort by empno asc;

3. distribute by

类似MR中partition,进行分区,结合sort by使用

insert overwrite local directory '/opt/datas/distbyby-res' select * from emp distribute by deptno sort by empno asc;

注意事项:

distribute by必须要在sort by 之前

4. cluster by

当distribute by和sort by字段相同时,可以使用cluster by;

insert overwrite local directory '/opt/datas/clusterby-res' select * from emp cluster by empno;

十三、Hive中自带Function使用和自定义UDF编程及使用

《OD学hive》第五周0723的更多相关文章

  1. 《OD学hive》第六周20160731

    一.hive的压缩 1. hadoop的压缩 1)为什么需要压缩 MapReduce的性能瓶颈:网络IO.磁盘IO 数据量:对于MapReduce的优化,最主要.根本就是要能够减少数据量 Combin ...

  2. 《OD学Hive》第六周20160730

    一.Hive的JDBC连接 日志分析结果数据,存储在hive中 <property> <name>hive.server2.thrift.port</name> & ...

  3. 《OD学hive》第四周0717

    一.Hive基本概念.安装部署与初步使用 1. 后续课程 Hive 项目:hadoop hive sqoop flume hbase 电商离线数据分析 CDH Storm:分布式实时计算框架 Spar ...

  4. 《OD学hadoop》第二周0703

    hdfs可视化界面: http://beifeng-hadoop-01:50070/dfshealth.html#tab-overview yarn可视化界面: http://beifeng-hado ...

  5. 《OD学hadoop》第二周0702

    大数据离线计算hadoop2.x 三周(6天) markdown文本剪辑器 罗振宇--跨年演讲,时间的朋友 http://tech.163.com/16/0101/11/BC87H8DF000915B ...

  6. 《OD学hadoop》第一周0626 作业二:Linux基础

    一.打包压缩 知识点: tar -zxvf -C PATH tar -jxvf tar -zcvf tar -jcvf tar:打包命令 -z 打包同时gzip压缩 -j 打包同时bzip2 -c 打 ...

  7. 《OD学hadoop》第一周0625

    一.实用网站 1. linux内核版本 www.kernel.org 2. 查看网站服务器使用的系统  www.netcraft.com 二.推荐书籍 1. <Hadoop权威指南> 1- ...

  8. 《OD学hadoop》第一周0625 LINUX作业一:Linux系统基本命令(一)

    1. 1) vim /etc/udev/rules.d/-persistent-net.rules vi /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE= ...

  9. 《OD学hadoop》第一周0626

    一.磁盘管理 Linux添加新硬盘.分区.格式化.自动挂载 http://lxsym.blog.51cto.com/1364623/321643 给Linux系统新增加一块硬盘 http://www. ...

随机推荐

  1. jquery $.ajax方法

    $.ajax({ timeout: 3000, cache:false, global:false, type: "POST", url: WEB_URL+"/logic ...

  2. [usaco2009febgold]道路翻新 最短路+dp

    这道题居然卡SPFA,难受,写了这么长时间的SPFA,都快把dij忘光了: 设d[i][j]为修j条路到i的最短距离,然后跑堆优化dij就行了: 实测中SPFA两组大数据超时严重: dij约300ms ...

  3. 关于IE下AJAX的问题探讨

    今天JS练手的时候,想封装一个发送AJAX请求的对象,当然,是想要兼容全浏览器的.代码如下: var Ajax = { xhr: null, callback: null, XMLHttp: func ...

  4. Unity3D 运营商支付 安卓打包的陷阱 libunicomsdk.jar

    原地址:http://blog.csdn.net/alking_sun/article/details/36624491 想想做Unity3D SDK集成已经快2年了,遇到过不少很棘手的SDK,其中以 ...

  5. PHP 中 Date 函数与实际时间相差8小时的解决方法

    PHP 中的 date() 函数显示的时间是格林威治时间,和北京时间正好相差8个小时,其他时间相关的函数,如 strtotime() 也有相同的问题,同样可以通过下面的方法来解决: 1. 修改php. ...

  6. jstl 的应用 java

    JSTL :JSP Standard Tag Library,JSP标准标签库 1.导入包 jstl.jar standard.jar 2.页面中添加标识 <%@taglib uri=" ...

  7. App接口设计思路

    http://www.techweb.com.cn/network/system/2016-01-11/2256859.shtml http://www.woshipm.com/pmd/172952. ...

  8. poj 3114(强连通缩点+SPFA)

    题目链接:http://poj.org/problem?id=3114 思路:题目要求很简单,就是求两点之间的花费的最短时间,不过有一个要求:如果这两个city属于同一个国家,则花费时间为0.如何判断 ...

  9. IDTHook 深入学习

    在之前的一篇文章中介绍了替换IDT向量表中的地址来达到Hook的目的 IDT hook KiTrap03 但是这样很容易就可以被检测了.接下来要学习就是通过patch GDT来达到Hook IDT的目 ...

  10. JavaWeb笔记——JSTL标签

    JSTL 1. jstl的概述   * apache的东西,依赖EL   * 使用jstl需要导入jstl.jar.standard.jar   * 四大库:     > core:核心库,重点 ...