当前位置:   article > 正文

hive创建分区表,并且加载hdfs的数据到表中_如果hadoop上面已经又=有一张表数据,想创建hive分区表加载该数据可以吗

如果hadoop上面已经又=有一张表数据,想创建hive分区表加载该数据可以吗

创建表

drop table if exists ods_order_info;
create external table ods_order_info (
    `id` string COMMENT '订单号',
    `consignee` string COMMENT '收货人',
    `consignee_tel` string COMMENT '收件人电话',`final_total_amount` decimal(16,2) COMMENT '订单金额',`order_status` string COMMENT '订单状态',
    `user_id` string COMMENT '用户id',`delivery_address` string COMMENT '送货地址',`order_comment` string COMMENT '订单备注',`out_trade_no` string COMMENT '支付流水号',`trade_body` string COMMENT '订单描述(第三方支付用)',`create_time` string COMMENT '创建时间',
`operate_time` string COMMENT '操作时间',
`expire_time` string COMMENT '失效时间',
`tracking_no` string COMMENT '物流单编号',
`parent_order_id` string COMMENT '父订单编号',
`img_url` string COMMENT '图片路径',
`province_id` string COMMENT '省份ID',
`benefit_reduce_amount` decimal(16,2) COMMENT '优惠金额',
`original_total_amount` decimal(16,2)  COMMENT '原价金额',
`feight_fee` decimal(16,2)  COMMENT '运费'
) COMMENT '订单表'
PARTITIONED BY (`dt` string) --创建分区
row format delimited fields terminated by '\t' -- 指定分割符为\t 
location '/data/hive/gmall/order_info/' -- 指定数据在hdfs上的存储位置,此路径会在hdfs中生成
;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

创建分区

alter table ods_order_info add if not exists partition(dt='2021-04-17') location '2021-04-17';
  • 1

加载hdfs的数据到hive表中 (方法一)

这里的数据是我之前就存在的了,没有数据的话可以自己造,这个不难,但是用这种方法的话,原来hdfs的文件,会被移动到/data/hive/gmall/order_info/这个目录下面,源文件消失。

load data INPATH '/data/sqoop/gmall/db/order_info/2021-04-17/part-m-00000.gz' overwrite into table ods_order_info PARTITION (dt='2021-04-17');
  • 1

hive表的数据路径直接就指定hdfs路径 (方法二)

在原来创建表的语句中的location 的路径,直接改成hdfs源文件所在路径。

location '/data/sqoop/gmall/db/order_info'
  • 1

因为我们建的是分区表,所以要创建一个分区

alter table ods_order_info add if not exists partition(dt='2021-04-17') location '2021-04-17';
  • 1

查询

select * from ods_order_info where dt = '2021-04-17';
  • 1

自学大数据的路中,建了一个交流群,678513209。有转大数据的小伙伴,加群一起交流

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/504879
推荐阅读
相关标签
  

闽ICP备14008679号