HIVE学习笔记:HIVECLI操作(2)
2017-09-07 09:24
507 查看
不进入HIVECLI的条件下执行SQL语句并避免打印多余信息(-S,Silent mode in interactive shell;-e:SQL from command line):
这种方式常用于shell脚本程序中。
在HIVE中设置自定义变量并使用:
HDFS上的文本内容:
HIVE打印表的内容显示(因为创建表时只定义了两个字段name与val):
注意,从HDFS中的文件加载数据时,HIVE会把指定加载的文件移动到相关表的目录下(仅对内部表成立)。执行上述操作后,HDFS文件系统发生了如下改变:
此时再扫描表test_location是没有数据的:
新建表并从已有表中选择数据插入新表:
扫描新表结果:
本地文本数据:
输出:
HIVE官方数据操作语法
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学习笔记:Hive CLI基本操作
- hive学习笔记——表的基本的操作
- Hive学习笔记-API简单操作
- hive 学习笔记——表的入门操作和命令
- hadoop学习笔记-hive安装及操作
- hive学习笔记-表操作
- hadoop学习笔记--12.hive DML操作
- (转载)Hive学习笔记--Hive Shell基本操作
- Hadoop学习笔记之操作hive
- Hive学习笔记 --- Hive分区表的操作
- hadoop学习笔记 Hive的cli与Commands
- Perl 字符串操作 以及 自定义排序学习笔记
- JMeter学习笔记4-修改Test Plan等相关操作
- AWS学习笔记(五)--启用CloudTrail记录AWS 账户操作日志
- hadoop-hive学习笔记
- Python 学习笔记——文件对象和操作
- 【小白笔记】PHP学习之路 (28) --图像操作+画线
- linux下c语言学习笔记——操作mysql
- 2016.06.17廖雪峰JS__学习笔记(操作DOM)__P12
- JavaWeb学习笔记——jquery中的dom操作