四、大数据组件之Hive--创建表
Create Table
官网说明
Hive建表方式共有三种:
- 直接建表法
- 查询建表法
- like建表法
首先看官网介绍
’[]’ 表示可选,’|’ 表示二选一
1 CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name -- (Note: TEMPORARY available in Hive 0.14.0 and later) 2 [(col_name data_type [COMMENT col_comment], ... [constraint_specification])] 3 [COMMENT table_comment] 4 [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] 5 [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] 6 [SKEWED BY (col_name, col_name, ...) -- (Note: Available in Hive 0.10.0 and later)] 7 ON ((col_value, col_value, ...), (col_value, col_value, ...), ...) 8 [STORED AS DIRECTORIES] 9 [ 10 [ROW FORMAT row_format] 11 [STORED AS file_format] 12 | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)] -- (Note: Available in Hive 0.6.0 and later) 13 ] 14 [LOCATION hdfs_path] 15 [TBLPROPERTIES (property_name=property_value, ...)] -- (Note: Available in Hive 0.6.0 and later) 16 17 18 19 [AS select_statement]; -- (Note: Available in Hive 0.5.0 and later; not supported for external tables) 20 21 22 23 CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name 24 LIKE existing_table_or_view_name 25 [LOCATION hdfs_path]; 26 27 data_type 28 : primitive_type 29 | array_type 30 | map_type 31 | struct_type 32 | union_type -- (Note: Available in Hive 0.7.0 and later) 33 34 primitive_type 35 : TINYINT 36 | SMALLINT 37 | INT 38 | BIGINT 39 | BOOLEAN 40 | FLOAT 41 | DOUBLE 42 | DOUBLE PRECISION -- (Note: Available in Hive 2.2.0 and later) 43 | STRING 44 | BINARY -- (Note: Available in Hive 0.8.0 and later) 45 | TIMESTAMP -- (Note: Available in Hive 0.8.0 and later) 46 | DECIMAL -- (Note: Available in Hive 0.11.0 and later) 47 | DECIMAL(precision, scale) -- (Note: Available in Hive 0.13.0 and later) 48 | DATE -- (Note: Available in Hive 0.12.0 and later) 49 | VARCHAR -- (Note: Available in Hive 0.12.0 and later) 50 | CHAR -- (Note: Available in Hive 0.13.0 and later) 51 52 array_type 53 : ARRAY < data_type > 54 55 map_type 56 : MAP < primitive_type, data_type > 57 58 struct_type 59 : STRUCT < col_name : data_type [COMMENT col_comment], ...> 60 61 union_type 62 : UNIONTYPE < data_type, data_type, ... > -- (Note: Available in Hive 0.7.0 and later) 63 64 row_format 65 : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char] 66 [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] 67 [NULL DEFINED AS char] -- (Note: Available in Hive 0.13 and later) 68 | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)] 69 70 file_format: 71 : SEQUENCEFILE 72 | TEXTFILE -- (Default, depending on hive.default.fileformat configuration) 73 | RCFILE -- (Note: Available in Hive 0.6.0 and later) 74 | ORC -- (Note: Available in Hive 0.11.0 and later) 75 | PARQUET -- (Note: Available in Hive 0.13.0 and later) 76 | AVRO -- (Note: Available in Hive 0.14.0 and later) 77 | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname 78 79 constraint_specification: 80 : [, PRIMARY KEY (col_name, ...) DISABLE NOVALIDATE ] 81 [, CONSTRAINT constraint_name FOREIGN KEY (col_name, ...) REFERENCES table_name(col_name, ...) DISABLE NOVALIDATE
观察可发现一共有三种建表方式,接下来我们将一一讲解。
1.直接建表法:
1 create table table_name(col_name data_type);
这里我们针对里面的一些不同于关系型数据库的地方进行说明。
row format
1 row_format 2 : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char] 3 [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char] 4 [NULL DEFINED AS char] -- (Note: Available in Hive 0.13 and later) 5 | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
Hive将HDFS上的文件映射成表结构,通过分隔符来区分列(比如’,’ ‘;’ or ‘^’ 等),row format就是用于指定序列化和反序列化的规则。
比如对于以下记录:
1 1,xiaoming,book-TV-code,beijing:chaoyang-shagnhai:pudong 2 2,lilei,book-code,nanjing:jiangning-taiwan:taibei 3 3,lihua,music-book,heilongjiang:haerbin
逗号用于分割列,即FIELDS TERMINATED BY char,分割为如下列 ID、name、hobby(该字段是数组形式,通过 ‘-’ 进行分割,即COLLECTION ITEMS TERMINATED BY ‘-’)、address(该字段是键值对形式map,通过 ‘:’ 分割键值,即 MAP KEYS TERMINATED BY ‘:’);
而FIELDS TERMINATED BY char用于区分不同条的数据,默认是换行符;
file format(HDFS文件存放的格式)
默认TEXTFILE,即文本格式,可以直接打开。
- 是
如下:根据上述文件内容,创建一个表t1
1 create table t1( 2 id int 3 ,name string 4 ,hobby array<string> 5 ,add map<String,string> 6 ) 7 row format delimited 8 fields terminated by ',' 9 collection items terminated by '-' 10 map keys terminated by ':' 11 ;
2. 查看表的描述:desc t1;
- 下面插入数据
注:一般很少用insert (不是insert overwrite)语句,因为就算就算插入一条数据,也会调用MapReduce,这里我们选择Load Data的方式。
1 LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]
创建一个文件粘贴上述记录,并上载即可,如下图:
然后上载
1 load data local inpath '/home/hadoop/Desktop/data' overwrite into table t1;
别忘记写文件名/data,笔者第一次忘记写,把整个Desktop上传了,一查全是null和乱码。。。。
查看表内容:
1 select * from t1;
external
未被external修饰的是内部表(managed table),被external修饰的为外部表(external table);
区别:
内部表数据由Hive自身管理,外部表数据由HDFS管理;
内部表数据存储的位置是hive.metastore.warehouse.dir(默认:/user/hive/warehouse),外部表数据的存储位置由自己制定;
删除内部表会直接删除元数据(metadata)及存储数据;删除外部表仅仅会删除元数据,HDFS上的文件并不会被删除;
对内部表的修改会将修改直接同步给元数据,而对外部表的表结构和分区进行修改,则需要修复(MSCK REPAIR TABLE table_name;)
创建一个外部表t2
1 create external table t2( 2 id int 3 ,name string 4 ,hobby array<string> 5 ,add map<String,string> 6 ) 7 row format delimited 8 fields terminated by ',' 9 collection items terminated by '-' 10 map keys terminated by ':' 11 location '/user/t2' 12 ;
装载数据
1 load data local inpath '/home/hadoop/Desktop/data' overwrite into table t2;
查看文件位置
如下图,我们在NameNode:50070/explorer.html#/user/目录下,可以看到t2文件
t1在哪呢?在我们之前配置的默认路径里
同样我们可以通过命令行获得两者的位置信息:
1 desc formatted table_name;
注:图中managed table就是内部表,而external table就是外部表。
分别删除内部表和外部表
下面分别删除内部表和外部表,查看区别
观察HDFS上的文件
发现t1已经不存在了
但是t2仍然存在
因而外部表仅仅删除元数据
重新创建外部表t2
1 create external table t2( 2 id int 3 ,name string 4 ,hobby array<string> 5 ,add map<String,string> 6 ) 7 row format delimited 8 fields terminated by ',' 9 collection items terminated by '-' 10 map keys terminated by ':' 11 location '/user/t2' 12 ;
不往里面插入数据,我们select * 看看结果
可见数据仍然在!!!
官网说明
1 A table created without the EXTERNAL clause is called a managed table because Hive manages its data. 2 Managed and External Tables 3 By default Hive creates managed tables, where files, metadata and statistics are managed by internal Hive processes. A managed table is stored under the hive.metastore.warehouse.dir path property, by default in a folder path similar to /apps/hive/warehouse/databasename.db/tablename/. The default location can be overridden by the location property during table creation. If a managed table or partition is dropped, the data and metadata associated with that table or partition are deleted. If the PURGE option is not specified, the data is moved to a trash folder for a defined duration. 4 Use managed tables when Hive should manage the lifecycle of the table, or when generating temporary tables. 5 An external table describes the metadata / schema on external files. External table files can be accessed and managed by processes outside of Hive. External tables can access data stored in sources such as Azure Storage Volumes (ASV) or remote HDFS locations. If the structure or partitioning of an external table is changed, an MSCK REPAIR TABLE table_name statement can be used to refresh metadata information. 6 Use external tables when files are already present or in remote locations, and the files should remain even if the table is dropped. 7 Managed or external tables can be identified using the DESCRIBE FORMATTED table_name command, which will display either MANAGED_TABLE or EXTERNAL_TABLE depending on table type. 8 Statistics can be managed on internal and external tables and partitions for query optimization.
2.查询建表法
通过AS 查询语句完成建表:将子查询的结果存在新表里,有数据
一般用于中间表
1 CREATE TABLE new_key_value_store 2 ROW FORMAT SERDE "org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe" 3 STORED AS RCFile 4 AS 5 SELECT (key % 1024) new_key, concat(key, value) key_value_pair 6 FROM key_value_store 7 SORT BY new_key, key_value_pair;
根据例子我们建一张表:t3
1 create table t3 as 2 select 3 id 4 ,name 5 from t2 6 ;
会执行MapReduce过程。
查看表结构及内容,发现是有数据的,并且由于没有指定外部表和location,该表在默认位置,即是内部表。
3.like建表法
会创建结构完全相同的表,但是没有数据。
常用语中间表
1 CREATE TABLE empty_key_value_store 2 LIKE key_value_store;
例子
1 create table t4 like t2;
可以发现,不会执行MapReduce,且表结构和t2完全一样,但是没有数据。
文章转载于:https://blog.csdn.net/qq_36743482/article/details/78383964