hive 表操作的基本sql

  1. 创建一个新表
create table temp.course(
    cno string,
    cname string,
    tno string
)row format delimited fields terminated by '\t' lines terminated by '\n'

  1. 表中添加数据
insert into temp.course values ('3-105', '计算机导论', 825),
    ('3-245', '操作系统', 804),
    ('6-177', '高等数学', 100);

  1. 利用select 查询结果来创建新表
create table temp.course_2 as select cno, cname from temp.course

注意:这种方式建的的表是非分区表,是内部表

  1. 查看表结构
desc temp.course_2
  1. 创建分区表
create table order_basic_info (order_id string comment '',
                buyer_id string comment '',
                seller_id string comment ''
                
                ) 
partition by(day string) 
locattion 'hdfs://user/hive/warehouse/temp.db/order_basic_info'
  1. 向已有表中导数
insert into table temp.ad select * from vc.ad_rule_result where day>='2020-11-10'  # 向已有的表中导数
insert overwrite  table temp.ad select * from vc.ad_rule_result where day>='2020-11-10' # 向已有的表中导数,覆盖原数据
  1. 删除某个表
drop table temp.course_new2  # 删除整个表
  1. 复制一个表结构
create table temp.course_new2 like temp.course 
  1. 清空表数据
truncate table temp.course_new # 使用truncate仅可删除内部表数据,不可删除表结构
  1. 添加一个字段
alter table temp.course add columns (id String)
  1. 删除一个分区
alter table temp.course drop partition (day='')
  1. 删除一个字段
alter table temp.course replace columns(
cno string,
cname string,
tno string
)

注意:不能用spark sql 来跑,否则回报Operation not allowed: alter table replace columns(line 1, pos 0)的错误
原因是:replace columns spark并没有实现这个功能
但是 hive sql是可以跑成功的,另外hive sql没有删除字段的语法,可以用replace来替换

  1. 如何创建含named_struct结构的表
named_struct类型,主要用这个函数做字段拼接,并且每个字段都可以取别名;

首先,创建一个中间表:
spark.read.csv("/user/vc/users/test/test.csv").toDF("risk_id", "shop_id", "user_id")
.write.mode("overwrite").saveAsTable("temp.ugc_user_info_old")

然后,创建一个含struct数据结构的表
CREATE TABLE temp.ugc_user_info(
  risk_id string COMMENT '',
user_info struct<shop_id:string,user_id:string> COMMENT '') COMMENT ''
 LOCATION 'hdfs://user/vc/users/test/temp.db/ugc_user_info'
 
最后,从中间表把数据导入到正式表
INSERT INTO temp.ugc_user_info SELECT risk_id, named_struct('shop_id',shop_id,'user_id',user_id) as user_info from temp.ugc_user_info_old

## 或者省去了创建表的麻烦
create table temp.ugc_user_info as 
SELECT risk_id, named_struct('shop_id',shop_id,'user_id',user_id) as user_info 
from temp.ugc_user_info_old

注意:您不能直接在Hive中插入复杂数据类型.对于插入结构,您有函数named_struct.您需要创建一个虚拟表,其中包含要插入所需表的"结构"列中的数据.

  1. map 类型,可以通过str_to_map()来创建:
insert into test3(field2)
values(
    str_to_map("name:zhangsan,age:25")),
    (str_to_map("name:lisi,age:23")
    );

select 
str_to_map(concat_ws(',',collect_set(concat(seller_id,'=',buyer_id))),',','=')  as 
 from vc.dwd_order_basic_info_p where day='2022-01-10' limit 10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值