HIVE学习笔记:Hive CLI基本操作

来源:互联网 发布:淘宝手机充值利润 编辑:程序博客网 时间:2024/04/28 23:38

Hive Command Line

进入shell

直接键入hive进入Hive CLI:

hive

新建表

新建表(comment为备注):

create table if not exists test(name string comment 'name value',addr string comment 'addr value');

显示创建表时的相关信息

show create table test;

打印表信息

显示表的详细信息:

desc formatted test;

外部表

创建外部表并打印表信息:

create external table if not exists test_ext(name string comment 'name value',addr string comment 'addr value');

内外表的区别

官方给出的信息:
A table created without the EXTERNAL clause is called a managed table because Hive manages its data.
内部表(managed table)的数据是由HIVE来管理的,而外部表则不是。两者的区别主要体现在表的删除上,当删除一个表时,若为内部表,则表在HDFS上关联的数据也会被删除;删除外部表时其在HDFS上关联的数据不会被删除。

指定表的分隔格式

创建预设格式的表(field表示每行的字段,以制表符为分割):

create table if not exists test_formatted(name string comment 'name value',addr string comment 'addr value')row format delimitedfields terminated by '\t'lines terminated by '\n'stored as textfile;

以已有表的格式创建新表

只复制格式不复制内容

create table copy like test_formatted;desc formatted copy;

从本地加载数据到表中

local指定本地文件,本地文件会被上传到HDFS中;overwrite以覆盖方式写入,否则以追加方式写入:

load data local inpath '/home/daya/test/students.txt' overwrite into table test_formatted;select * from test_formatted;

查找条目

select * from test_formatted where name='wang';

删除表

drop table test_formatted;

表的存储位置

表数据默认存放在HDFS中:

复杂表的基本运用

array,map,struct都属于collection item,它们内部的条目设为以逗号分隔:

create external table if not exists employees(name string,salary float,subordinates array<string>,deductions map<string,float>,address struct<street:string,city:string,state:string,zip:int>)row format delimitedfields terminated by '\t'collection items terminated by ','map keys terminated by ':'lines terminated by '\n'stored as textfilelocation '/data/';load data local inpath '/home/daya/test/test.txt' overwrite into table employees;select * from employees;

本地文本内容:

输出信息:

查找:

select subordinates[0] from employees;select deductions["k4"] from employees;select address.city from employees;

官方HIVECLI命令指南

原创粉丝点击