1. 进入hive
直接在命令行执行hive即可
2. 执行hive的两种方式
2.1 在hive里面执行。
可以通过-i 命令指定hive的初始化状态。在配置文件里面写上:
use default;
set hive.cli.print.current.db=true;
2.2 可以通过-f命令直接执行里面的hql语句
会执行里面的语句,但是不会进入hive环境
2.3 可以通过-e命令执行具体的sql命令,但是保持不进入hive环境。
hive -v -e "show databases;"
3. hql的具体使用
3.1 . 创建数据库
create database frog_db;
drop database frog_db;
3.2 创建数据表
create table student(
id int comment 'id',
name string comment '姓名',
score decimal(30,6) comment '成绩')
stored as textfile;
3.3 显示数据表的一些属性
可以查看有那些表:show tables;显示某个表的信息 desc student;显示建表语句(在create之前加上一句show即可)
show create table student;
可以自己指定分隔符。
create table student(
id int comment '识别码',
name string comment '姓名',
score string comment '成')
row format delimited fields terminated by ','
lines terminated by '\n'
stored as textfile;
3.3 导入数据
数据内容:
1,zhangsan,56.7
2,lisi,78.9
3,wagnwu,90.8
4,赵六,100
导入用到的命令:
load data local inpath '/home/frog005/sxing/source_data.txt' into table student;
3.4 查看hive建表路径
需要在命令行模式查看,就是看看show create table里面的那个hdfs路径。
hadoop fs -ls hdfs://localhost:9000/user/hive/warehouse/sxing__db.db/student
hadoop fs -cat hdfs://localhost:9000/user/hive/warehouse/sxing__db.db/student/source_data.txt
hive模式下也可以直接查看:
dfs -ls hdfs://localhost:9000/user/hive/warehouse/sxing__db.db/student
dfs -cat hdfs://localhost:9000/user/hive/warehouse/sxing__db.db/student/source_data.txt
3.5、内部表和外部表
我们说表格式是建表会有默认值,如果我们不指定 location 那么创建的表就是内部表,如果指定了 location呢,那就是外部表。上面我们分析 load data 命令可以知道,其实就是把数据从 linux 上放到了 hdfs 路径上。
那我们来看一下外部表要怎么创建
create external table student( id int comment '识别码', name string comment '姓名')
row format delimited fields terminated by ','
location 'tmp/sxing';
在命令模式下使用hadoop创建目录,并且put文件
hadoop fs -mkdir /tmp/sxing
hadoop fs -put student.txt /tmp/sxing
hadoop fs -ls /tmp/student
此时没有loaddata,但是hive会自动把location下面的文件按照表的规格解析,进行查询。神奇。
删除表时,内部表中的数据和元数据将会被同时删除,而外部表只删除元数据,不删除数据。
对于内部表,删除table的时候数据也就没有了。
3.6 分区表
在表目录中为数据文件创建分区子目录,以便于在查询时,MR 程序可以针对分区子目录中的数据进行处理,缩减读取数据的范围(不然就要全部读取)。
比如:网站每天产生的浏览记录,浏览记录应该建一个表来存放,但是,有时候,我们可能只需要对某一天的浏览记录进行分析,这时,就可以将这个表建为分区表,每天的数据导入其中的一个分区,当然,每日的分区目录,应该有一个目录名(分区字段)。
# partitioned by(day string)就是分区的依据
create table pv_log(
ip string,
url string,
visit_time string)
partitioned by(day string)
row format delimited fields terminated by ',';
# 建表语句,只会建表目录,分区的目录是在放数据的时候建立,先建表再放数据
27.38.32.58,http://www.baidu.com.cn,2006-12-13 12:34:16
27.38.32.59,http://www.baidu.com.cn,2011-08-13 08:37:16
27.38.32.60,http://www.baidu.com.cn,2006-12-13 12:24:16
27.38.32.61,http://www.baidu.com.cn,2016-06-13 06:34:16
27.38.32.54,http://www.baidu.com.cn,2012-12-15 12:34:16
27.38.32.55,http://www.baidu.com.cn,2009-08-13 09:24:16
27.38.32.57,http://www.baidu.com.cn,2005-12-13 12:14:16
27.38.32.50,http://www.baidu.com.cn,2003-07-16 10:04:16
27.38.32.52,http://www.baidu.com.cn,2007-12-13 12:34:16
# hive 执行导入数据,需要指定分区是什么
load data local inpath '/home/froghd/pv.log' into table pv_log partition (day='20150620');
3.7 CTAS 建表语法
通过已存在的表来建表
# 创建一个和 table1 一样字段的 table2 表,格式一样但是数据过不来
create table table2 like table1
create table pv_log1 like pv_log;
# 创建一个表类似与已有表,不仅字段一样而且还带有数据(好像不会有分区),查出来是什么字段名新表就是什么字段名
create table pv_log2 as select * from pv_log where visit_time>'2006-12-13';
注意ctas只是内容过来了,格式没有过来的。