hive执行命令的方式
hive -e "hql" 命令行执行hsql
hive -e "select * from mydb2.c1"
hive -S 静默模式,控制台不输出Logging信息
hive -S -e "select * from mydb2.c1"
hive -v 详细模式,会把执行的sql打印出来
hive -v -e "select * from mydb2.c1"
hive -f "path" 执行文件
hive -f "/home/lz/createtable.txt"
hive -i 执行初始化xx自己实现
list jar;显示缓存了哪些jar包
list file;显示缓存了哪些文件
hive 数据加载
创建表时加载
create table c3 as select * from c1;
创建表时指定数据位置
create table c4(id int,name string) location 'path';
本地数据加载
load data local inpath 'path' overwrite into table c5;
加载hdfs数据
load data inpath 'path' overwrite into table c6;
查询语句加载数据
create table c4 like c2;(复制表结构)
1.insert into/overwrite table c4
select id,name
from c2
where id=1;
2.from c2
insert into/overwrite c5
select id,name
where id=2;
注意:字段对于和关系型数据库不一样的
hive在加载数据的时候不做字段检查和字段类型检查的
在查询的时候才做检查
分区表加载数据
同表加载数据一样,只是需要指定分区分区
注意:数据存放的路径层次要和表的分区一致
如果分区表没有新增分区,即使目标路径下已经有数据了,但依然查不到数据
本地加载数据
create table if not exists d1(
id int,
name string,
age int
)
partitioned by(dt string)
row format delimited fields terminated by ','
stored as textfile;
1,tom,24
2,jack,25
3,lc,27
4,ljc,28
load data local inpath 'path' overwrite/into table d1 partition(dt='20180416');
加载hdfs数据
load data inpath 'path' overwrite/into table tablename partition(dt='20180416');
由查询语句加载数据
create table if not exists d3(
id int,
name string
)
partitioned by(dt string)
row format delimited fields terminated by ','
stored as textfile;
insert overwrite table d3 partition(dt='20180815') select name,id from d1;
insert overwrite table d2 partition(dt='20180815') select id,name,age from d1;
注意:由查询语句加载数据,字段必须一样多,d2表三个字段,那么select d1表的三个字段才能插入
hive数据加载需要注意的问题
1.分隔符问题,分隔符默认是单个字符(因为默认只取第一个字符)
2.load数据字段类型不能互相转化是,查询返回NULL
create table if not exists e1(id int,name string) row format delimited fields terminated by ',' stored as textfile;
1,tom
2,jack
load data local inpath '/home/lz/e1.txt' overwrite into table e1;
create table e2 like e1;
load data local inpath 'path' overwrite into table e2;
(这个查询那么字段为NULL,string转不了int)
3.select查询插入,字段类型不能互相转化时,插入数据为NULL
insert into table e2 select name,id from e1;
(这个id字段为NULL,string转不了int)
4.select查询插入分区数据,字段值顺序要预表中字段顺序一致,名称可不一致
5.外部分区表需要添加分区才能看到数据(alter table add partition(xx=xx))