hive上层数据使用外部表操作说明

例如:使用ods.ods_o_test数据分区为dt=20170810/hour=00数据为例创建work.fz_ods_o_test

1.首先通过hadoop命令确定hdfs路径

hadoop fs -ls /user/hive/warehouse/ods.db/ods_o_test/dt=20170810/hour=00

ps:

a. /user/hive/warehouse/恒定

b. ods.db为库名+.db组成

c. ods_o_test直接为表名

d. 分区dt=20170810/hour=00

2.通过show create table命令查询出ods_o_test表表结构

hive> show create table ods.ods_o_test;
OK
CREATE TABLE `ods.ods_o_test`(
  `test` string COMMENT 'test'
)
COMMENT 'test'
PARTITIONED BY ( 
  `dt` string, 
  `hour` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
WITH SERDEPROPERTIES ( 
  'field.delim'='|', 
  'serialization.format'='|') 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://nameservice1/user/hive/warehouse/ods.db/ods_o_test'
TBLPROPERTIES (
  'transient_lastDdlTime'='1501132081')
Time taken: 0.335 seconds, Fetched: 100 row(s)
hive>

3.创建外部表,这里使用到刚刚查询到的表结构的字段信息

语法:

create external TABLE 表名(
字段 字段类型等
)
location '指定hdfs文件路径';

示例:

create external TABLE work.fz_ods_o_test(
`test` string COMMENT 'test')
row format delimited   
fields terminated by '|'
location '/user/hive/warehouse/ods.db/ods_o_test/dt=20170810/hour=00'
;

ps1: 建外部表如果使用到分区指定location必须到/user/hive/warehouse/ods.db/ods_o_test

ps2: 在使用的时候注意下需要指定数据的分割符,不然会出现无法查询到数据的情况

ps3: ods层的外部表请务必不要innsert 数据

ps4: 不能在ods层创建外部表