Hadoop入门之Hive的DDL和DML
2017-09-12 17:56
507 查看
1.Hive的表创建(分区,分桶)
创建表SQL
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
1.1创建一个普通的表每个字段以 , 为分割方式
drop table my_one_table;
create table my_one_table
(id int,name string)
row format delimited fields terminated by ',' stored as textfile;
1.2 创建一个外部表
create external table my_two_table
(id int,name string)
row format delimited fields terminated by ','
location 'hdfs://hadoop5:9000/test/hive/onedb/';
(就是将文件放在你指定的目录位置)
1.3创建一个分区表
create table my_three_table
(id int,name string)
partitioned by (part string)
row format delimited fields terminated by ','
stored as textfile;
(分区字段要未被定义过,形式上来说,就是创建了一个子目录而已,加载时加载到指定目录)
load data local inpath '/home/hadoop/local_data/china' overwrite
into table my_three_table partition (part='China');
load data local inpath '/home/hadoop/local_data/usa' overwrite
into table my_three_table partition (part='usa');
show partitions my_three_table;
1.4 带桶的表的创建语句
create table my_four_table
(id int,age int,name string)
partitioned by (stat_data string)
clustered by(id) sorted by (age) into 2 buckets
row format delimited fields terminated by ','
stored as textfile;
这个一般是作为MR结果输出表来创建的.
2.Hive的DML操作
1.Load操作
LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO
TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]
说明:
1、 Load 操作只是单纯的复制/移动操作,将数据文件移动到 Hive 表对应的位置。
2、 filepath:
相对路径,例如:project/data1
绝对路径,例如:/user/hive/project/data1
包含模式的完整 URI,列如:
hdfs://namenode:9000/user/hive/project/data1
3、 LOCAL关键字
如果指定了 LOCAL, load 命令会去查找本地文件系统中的 filepath。
如果没有指定 LOCAL 关键字,则根据inpath中的uri 查找文件
4、 OVERWRITE 关键字
如果使用了 OVERWRITE 关键字,则目标表(或者分区)中的内容会被删除,然后再将 filepath 指向的文件/目录中的内容添加到表/分区中。
如果目标表(分区)已经有一个文件,并且文件名和 filepath 中的文件名冲突,那么现有的文件会被新文件所替代。
2.Insert 操作
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement
Multiple inserts:
FROM from_statement
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1
[INSERT OVERWRITE TABLE tablename2 [PARTITION ...] select_statement2] ...
Dynamic partition inserts:
INSERT OVERWRITE TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement
3.Select基本操作
SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list [HAVING condition]]
[CLUSTER BY col_list
| [DISTRIBUTE BY col_list] [SORT BY| ORDER BY col_list]
]
[LIMIT number]
注:1、order by 会对输入做全局排序,因此只有一个reducer,会导致当输入规模较大时,需要较长的计算时间。
2、sort by不是全局排序,其在数据进入reducer前完成排序。因此,如果用sort by进行排序,并且设置mapred.reduce.tasks>1,则sort by只保证每个reducer的输出有序,不保证全局有序。
3、distribute by根据distribute by指定的内容将数据分到同一个reducer。
4、Cluster by 除了具有Distribute by的功能外,还会对该字段进行排序。因此,常常认为cluster by = distribute by + sort by
创建表SQL
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
[CLUSTERED BY (col_name, col_name, ...)
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
1.1创建一个普通的表每个字段以 , 为分割方式
drop table my_one_table;
create table my_one_table
(id int,name string)
row format delimited fields terminated by ',' stored as textfile;
1.2 创建一个外部表
create external table my_two_table
(id int,name string)
row format delimited fields terminated by ','
location 'hdfs://hadoop5:9000/test/hive/onedb/';
(就是将文件放在你指定的目录位置)
1.3创建一个分区表
create table my_three_table
(id int,name string)
partitioned by (part string)
row format delimited fields terminated by ','
stored as textfile;
(分区字段要未被定义过,形式上来说,就是创建了一个子目录而已,加载时加载到指定目录)
load data local inpath '/home/hadoop/local_data/china' overwrite
into table my_three_table partition (part='China');
load data local inpath '/home/hadoop/local_data/usa' overwrite
into table my_three_table partition (part='usa');
show partitions my_three_table;
1.4 带桶的表的创建语句
create table my_four_table
(id int,age int,name string)
partitioned by (stat_data string)
clustered by(id) sorted by (age) into 2 buckets
row format delimited fields terminated by ','
stored as textfile;
这个一般是作为MR结果输出表来创建的.
2.Hive的DML操作
1.Load操作
LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO
TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]
说明:
1、 Load 操作只是单纯的复制/移动操作,将数据文件移动到 Hive 表对应的位置。
2、 filepath:
相对路径,例如:project/data1
绝对路径,例如:/user/hive/project/data1
包含模式的完整 URI,列如:
hdfs://namenode:9000/user/hive/project/data1
3、 LOCAL关键字
如果指定了 LOCAL, load 命令会去查找本地文件系统中的 filepath。
如果没有指定 LOCAL 关键字,则根据inpath中的uri 查找文件
4、 OVERWRITE 关键字
如果使用了 OVERWRITE 关键字,则目标表(或者分区)中的内容会被删除,然后再将 filepath 指向的文件/目录中的内容添加到表/分区中。
如果目标表(分区)已经有一个文件,并且文件名和 filepath 中的文件名冲突,那么现有的文件会被新文件所替代。
2.Insert 操作
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement
Multiple inserts:
FROM from_statement
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1
[INSERT OVERWRITE TABLE tablename2 [PARTITION ...] select_statement2] ...
Dynamic partition inserts:
INSERT OVERWRITE TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement
3.Select基本操作
SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list [HAVING condition]]
[CLUSTER BY col_list
| [DISTRIBUTE BY col_list] [SORT BY| ORDER BY col_list]
]
[LIMIT number]
注:1、order by 会对输入做全局排序,因此只有一个reducer,会导致当输入规模较大时,需要较长的计算时间。
2、sort by不是全局排序,其在数据进入reducer前完成排序。因此,如果用sort by进行排序,并且设置mapred.reduce.tasks>1,则sort by只保证每个reducer的输出有序,不保证全局有序。
3、distribute by根据distribute by指定的内容将数据分到同一个reducer。
4、Cluster by 除了具有Distribute by的功能外,还会对该字段进行排序。因此,常常认为cluster by = distribute by + sort by
相关文章推荐
- 23-hadoop-hive的DDL和DML操作
- 数据库入门速成---ORACLE (03) 子查询 / DML / TCL / DDL
- 北风网Hadoop套餐教程 Hive 入门实战:Mysql+Hive
- Hive 5、Hive 的数据类型 和 DDL Data Definition Language) Hive DML(Data Manipulation Language)
- Hive创建表格报【Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException】引发的血案
- 008-Hadoop Hive sql语法详解3-DML 操作:元数据存储
- MySQL入门基本命令 SQL分类,DDL,DML
- org.apache.hadoop.hive.ql.exec.DDLTask. MetaException
- (防坑笔记)hadoop3.0 (五) Hive的入门与数据类型
- Hadoop入门进阶课程8--Hive介绍和安装部署
- Hadoop入门之Hive自定义函数和Transform
- Hadoop Hive概念学习系列之hive里的JDBC编程入门(二十二)
- Hive 编程之DDL、DML、UDF、Select总结
- Hadoop入门(三):Sqoop和Hive的使用
- Hadoop Hive sql语法详解1-认识hive及DDL操作
- Hive 编程之DDL、DML、UDF、Select总结
- Hadoop Hive基础SQL语法(DDL 操作)
- hadoop入门学习系列之四hadoop的hive安装和配置
- hive中删除表的错误Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException
- hadoop入门第七步---hive部署安装(apache-hive-1.1.0)