1. 创建操作
1.1 创建表
CREATE TABLE pokes (foo INT, bar STRING);
1.2 基于现有的表结构创建一个新表
create table new_table like records;
1.3 创建视图:
CREATE VIEW valid_records AS SELECT * FROM records2 WHERE temperature !=9999;
1.4 创建外部表:
CREATE EXTERNAL TABLE page_view(viewTime INT, userid BIGINT,
page_url STRING, referrer_url STRING,
ip STRING COMMENT ‘IP Address of the User‘,
country STRING COMMENT ‘country of origination‘)
COMMENT ‘This is the staging page view table‘
ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\054‘
STORED AS TEXTFILE
LOCATION ‘<hdfs_location>‘;
1.5 建分区表:
CREATE TABLE par_table(viewTime INT, userid BIGINT,
page_url STRING, referrer_url STRING,
ip STRING COMMENT ‘IP Address of the User‘)
COMMENT ‘This is the page view table‘
PARTITIONED BY(date STRING, pos STRING)
ROW FORMAT DELIMITED fields terminated by ‘\t‘
lines terminated by ‘\n‘;
STORED AS SEQUENCEFILE;
2. 加载数据
hive不支持用insert语句一条一条的进行插入操作,也不支持update操作。数据是以load的方式加载到建立好的表中。数据一旦导入就不可以修改。
路径支持:
* 相对路径,例如:project/data1
* 绝对路径,例如: /user/hive/project/data1
* 包含模式的完整 URI,例如:hdfs://namenode:9000/user/hive/project/data1
2.1 从本地文件加载数据:
LOAD DATA LOCAL INPATH ‘/home/hadoop/input/ncdc/micro-tab/sample.txt‘ OVERWRITE INTO TABLE records;
2.2 加载分区表数据:
load data local inpath ‘/home/hadoop/input/hive/partitions/file1‘ into table logs partition (dt=‘2001-01-01‘,country=‘GB‘);
3. 查看表结构
3.1 展示所有表:
SHOW TABLES;
SHOW TABLES ‘.*s‘; //按正条件(正则表达式)显示表
3.2 显示表的结构信息
DESCRIBE invites;
3.3 展示表中有多少分区:
show partitions logs;
3.4 显示所有函数:
show functions;
3.5 查看函数用法:
describe function substr;
3.6 查看数组、map、结构
select col1[0],col2[‘b‘],col3.c from complex;
3.7 查看hive为某个查询使用多少个MapReduce作业
Explain SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);
3.8 查看视图详细信息:
DESCRIBE EXTENDED valid_records;
4. 更新操作
4.1 更新表的名称:
ALTER TABLE source RENAME TO target;
4.2 添加、更新一列
ALTER TABLE invites ADD|REPLACE COLUMNS (new_col2 INT COMMENT ‘a comment‘);
4.3 增加、删除分区
ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec [ LOCATION ‘location1‘ ] partition_spec [ LOCATION ‘location2‘ ] ...
partition_spec:
: PARTITION (partition_col = partition_col_value, partition_col = partiton_col_value, ...)
ALTER TABLE table_name DROP partition_spec, partition_spec,...
4.4 增加表的元数据信息
ALTER TABLE table_name SET TBLPROPERTIES table_properties table_properties:
:[property_name = property_value…..]
4.5 改变表文件格式与组织
ALTER TABLE table_name SET FILEFORMAT file_format
ALTER TABLE table_name CLUSTERED BY(userid) SORTED BY(viewTime) INTO num_buckets BUCKETS
5. 删除操作
5.1 删除表:
DROP TABLE records;
5.2 删除表中数据,但要保持表的结构定义
dfs -rmr /user/hive/warehouse/records;
5.3 删除视图
DROP VIEW view_name
6. 连接操作
6.1 内连接:
SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);
6.2 外连接:
SELECT sales.*, things.* FROM sales LEFT OUTER JOIN things ON (sales.id = things.id);
SELECT sales.*, things.* FROM sales RIGHT OUTER JOIN things ON (sales.id = things.id);
SELECT sales.*, things.* FROM sales FULL OUTER JOIN things ON (sales.id = things.id);
6.3 in查询:Hive不支持,但可以使用LEFT SEMI JOIN
SELECT * FROM things LEFT SEMI JOIN sales ON (sales.id = things.id);
6.4 Map连接:Hive可以把较小的表放入每个Mapper的内存来执行连接操作
SELECT /*+ MAPJOIN(things) */ sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);
INSERT OVERWRITE TABLE ..SELECT:新表预先存在
FROM records2
INSERT OVERWRITE TABLE stations_by_year SELECT year, COUNT(DISTINCT station) GROUP BY year
INSERT OVERWRITE TABLE records_by_year SELECT year, COUNT(1) GROUP BY year
INSERT OVERWRITE TABLE good_records_by_year SELECT year, COUNT(1) WHERE temperature != 9999 AND (quality = 0 OR quality = 1 OR quality = 4 OR quality = 5 OR quality = 9) GROUP BY year;
CREATE TABLE … AS SELECT:新表表预先不存在
CREATE TABLE target AS SELECT col1,col2 FROM source;
7. 插入数据
7.1 基本模式
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1 FROM from_statement
7.2 多插入模式
FROM from_statement
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...)] select_statement1
[INSERT OVERWRITE TABLE tablename2 [PARTITION ...] select_statement2] ...
7.3 自动分区模式
INSERT OVERWRITE TABLE tablename PARTITION (partcol1[=val1], partcol2[=val2] ...) select_statement FROM from_statement
8. 导出数据到HDFS
数据写入文件系统时进行文本序列化,且每列用^A 来区分,\n
换行
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 SELECT ... FROM ...
FROM from_statement
INSERT OVERWRITE [LOCAL] DIRECTORY directory1 select_statement1
[INSERT OVERWRITE [LOCAL] DIRECTORY directory2 select_statement2]
时间: 2024-10-21 01:25:00