ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Hive基本使用

2021-08-10 13:35:33  阅读:174  来源: 互联网

标签:基本 comment string -- ann Hive tm 使用 data


一、Hive基本使用

(一) DDL操作

1. 数据库相关

(1) 创建数据库

create database tm;
create database if not exists tm;

(2) 切换数据库

use tm;

(3) 查看数据库信息

-- 查看数据库信息
desc database tm;

-- 显示数据库详细信息(可以查看数据库属性信息)
desc database extended tm; 

(4) 修改数据库信息

-- 修改数据库(属性信息)
alter database tm set dbproperties('create_time'='20210506');

(5) 修改数据库信息

-- 删除空数据库
drop database tm;
drop database if exists tm;

-- 删除非空数据库
drop database tm cascade;

2. 数据表基本操作

前提:解决“中文注释”乱码


-- ########### 解决 “建表时” 中文注释是乱码 ################

-- 在mysql中 hive元数据库中执行 以下 SQL,
-- 只有新建表的注释才会是中文,之前表的注释无法变为中文

-- 修改表字段注解和表注解
alter table COLUMNS_V2 modify column COMMENT varchar(256) character set utf8;
alter table TABLE_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;

-- 修改分区字段注解
alter table PARTITION_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8 ;
alter table PARTITION_KEYS modify column PKEY_COMMENT varchar(4000) character set utf8;

-- 修改索引注解
alter table INDEX_PARAMS modify column PARAM_VALUE varchar(4000) character set utf8;

(1) 创建内部表

-- 创建“内部表” (管理表)
    -- 指定 字段分隔符
    -- 指定 行分隔符
    -- 指定 表属性信息
    
create table if not exists tm_data(
    id bigint comment '自增ID',
    ann_num int comment '公告期号',
    ann_date date comment '公告日期',
    ann_id int comment '公告类型id',
    ann_type_code string comment '公告类型代码',
    ann_type string comment '公告类型',
    ncl_code smallint comment '国际分类号',
    reg_number string comment '注册号',
    reg_name string comment '申请人',
    tm_name string comment '商标名称',
    pic_url string comment '图片链接',
    page_no int comment '页码',
    down_path string comment '下载地址',
    data_index string comment '数据库位置索引',
    add_time date comment '新增时间',
    mod_time date comment '修改时间',
    info_id string comment '公告id'
)
comment '公告数据列表'
row format delimited fields terminated by '\t'
lines terminated by '\n';
TBLPROPERTIES ('creator'='yzy', 'crate_time'='2021-05-08');

(2) 创建外部表

-- 创建“外部表”

create external table if not exists tm_data(
    id bigint comment '自增ID',
    ann_num int comment '公告期号',
    ann_date date comment '公告日期',
    ann_id int comment '公告类型id',
    ann_type_code string comment '公告类型代码',
    ann_type string comment '公告类型',
    ncl_code smallint comment '国际分类号',
    reg_number string comment '注册号',
    reg_name string comment '申请人',
    tm_name string comment '商标名称',
    pic_url string comment '图片链接',
    page_no int comment '页码',
    down_path string comment '下载地址',
    data_index string comment '数据库位置索引',
    add_time date comment '新增时间',
    mod_time date comment '修改时间',
    info_id string comment '公告id'
)
comment '公告数据列表'
row format delimited fields terminated by '\t'
lines terminated by '\n';
TBLPROPERTIES ('creator'='yzy', 'crate_time'='2021-05-08');

(3) 更改内【外】部表

-- 将 内部表 改为外部表
alter table tm_data set tblproperties('EXTERNAL'='TURE');

-- 将 外部表 改为内部表
alter table tm_data set tblproperties('EXTERNAL'='FALSE');

(4) 创建分区表

-- 创建 “分区表”

-- 注意:分区字段不能存在于表字段中

create table if not exists tm_data_partition(
    id bigint comment '自增ID',
    -- ann_num int comment '公告期号',
    ann_date date comment '公告日期',
    ann_id int comment '公告类型id',
    ann_type_code string comment '公告类型代码',
    ann_type string comment '公告类型',
    ncl_code smallint comment '国际分类号',
    reg_number string comment '注册号',
    reg_name string comment '申请人',
    tm_name string comment '商标名称',
    pic_url string comment '图片链接',
    page_no int comment '页码',
    down_path string comment '下载地址',
    data_index string comment '数据库位置索引',
    add_time date comment '新增时间',
    mod_time date comment '修改时间',
    info_id string comment '公告id'
)
comment '公告数据列表'
partitioned by (ann_num int comment '公告期号')
row format delimited fields terminated by '\t'
TBLPROPERTIES ('creator'='yzy', 'crate_time'='2021-05-07');

(5) 操作分区

-- 分区字段可以作为表的普通字段进行查询
select * from tm_data where ann_num=1234;

-- 查看分区信息
show partitions tm_data;

-- 增加分区
aler table tm_data add [if not exist] partition(ann_num=1234,ann_type_code='TMZCSQ')

-- 删除分区
aler table tm_data drop [if exist] partition(ann_num=1234,ann_type_code='TMZCSQ')

(6) 查看表信息

-- 查看表字段信息
desc tm_data;

-- 查看详细的表字段信息;
desc formatted tm_data;

-- 查看表建表语句;
show create table tm_data;

(7) 根据已有数据表创建数据表

-- 复制表结构
create table if not exists tm_data2 like tm_data;

-- 复制表结构及数据[取部分字段]

-- 1. 未指定分隔符
create table if not exists tm_data3 as select id, ann_num, ann_date from tm_data;

-- 2. 指定分隔符
create table if not exists tm_data4 
row format delimited fields terminated by '\t' 
lines terminated by '\n' 
as select id, ann_num, ann_date from tm_data;

-- 3. 指定表备注信息,不能指定要创建表的字段及备注信息
create table if not exists tm_data5 
	comment '表备注' 
	row format delimited fields terminated by '\t' 
	lines terminated by '\n'
as select id, ann_num, ann_date from tm_data;

(8) 创建复合类型字段的数据表

-- 数据准备,两条数据
-- 新建test.txt文件,内容如下:导入hive表中
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing

-- cmd: load data local inpath ‘/root/test.txt’into table student


-- 指定 array,map,struct 等复合类型的 分隔符
create table if not exists student(
	name string,
	friend array<string>,
	children map<string,int>,
	address struct<street:string,city:string>
)
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';

-- 查询语句
select name,friend[1],children["xiaoxiao"],address.street,address.city from student;

标签:基本,comment,string,--,ann,Hive,tm,使用,data
来源: https://www.cnblogs.com/yzyang/p/15123279.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有