您的位置:首页 > 其它

HIVE学习笔记:HIVECLI操作(2)

2017-09-07 09:24 507 查看
不进入HIVECLI的条件下执行SQL语句并避免打印多余信息(-S,Silent mode in interactive shell;-e:SQL from command line):

hive -S -e "show tables"


这种方式常用于shell脚本程序中。

在HIVE中设置自定义变量并使用:

set name=wang;
select * from test_formatted where name='${hiveconf:name}';


创建表时加载数据

create table new as select name,addr from test_formatted;
desc new;
select * from new;


创建表时指定数据所在位置

创建表时指定location字段,表会自动从HDFS指定位置读取数据:

create table test_location(
name string,
val string
)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile
location '/data';


HDFS上的文本内容:



HIVE打印表的内容显示(因为创建表时只定义了两个字段name与val):



从HDFS中加载数据到表中

/data/test.txt是表test_location的依赖数据,而表student是

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


注意,从HDFS中的文件加载数据时,HIVE会把指定加载的文件移动到相关表的目录下(仅对内部表成立)。执行上述操作后,HDFS文件系统发生了如下改变:





此时再扫描表test_location是没有数据的:



选择插入数据

表test_formatted中的数据:



新建表并从已有表中选择数据插入新表:

create table test_insert(name string,addr string) stored as textfile;
from test_formatted insert overwrite table test_insert
select name,addr where name='liu';


扫描新表结果:



往分区表插入数据时指定分区

create table test_partition(
name string,
val string
)
partitioned by(dt string)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile
location '/data';
load data local inpath '/home/daya/test/test.txt' into table test_partition partition(dt='20170907');
show partitions test_partition;
select * from test_partition;


注意

当插入数据类型与表的字段定义类型不一致时,查询数据时会返回NULL,演示:

create table if not exists test_null(
name int,
val string)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile;
load data local inpath '/home/daya/test/test.txt' into table test_null;
select * from test_null;


本地文本数据:



输出:



HIVE官方数据操作语法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hive