您的位置:首页 > 其它

Hive 元数据和QL基本操作学习整理

2017-04-11 11:49 295 查看
Hive元数据库

Hive将元数据存储在RDBMS 中,一般常用的有MySQL和DERBY。

hive元数据对应的表约有20个,其中和表结构信息有关的有9张,其余的10多张或为空,或只有简单的几条记录,以下是部分主要表的简要说明。



从上面表的内容来看,hive整个创建表的过程已经比较清楚了。

1. 解析用户提交hive语句,对其进行解析,分解为表、字段、分区等hive对象

2. 根据解析到的信息构建对应的表、字段、分区等对象,从 SEQUENCE_TABLE中获取构建对象的最新ID,与构建对象信息(名称,类型等)一同通过DAO方法写入到元数据表中去,成功后将SEQUENCE_TABLE中对应的最新ID+5。

实际上我们常见的RDBMS都是通过这种方法进行组织的,典型的如postgresql,其系统表中和hive元数据一样裸露了这些id信息(oid,cid等),而Oracle等商业化的系统则隐藏了这些具体的ID。通过这些元数据我们可以很容易的读到数据诸如创建一个表的数据字典信息,比如导出建表语名等。

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);


LEFT SEMI JOIN的限制是, JOIN子句中右边的表只能在ON子句中设置过滤条件,在WHERE子句、SELECT子句或其他地方过滤都不行。

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]


转自:http://blog.csdn.net/lnho2015/article/details/51381198





大小: 44.1 KB

查看图片附件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: