Hive表的基本操作
1. 创建表
create table
语句遵从sql
语法习惯,只不过Hive
的语法更灵活。例如,可以定义表的数据文件存储位置,使用的存储格式等。
create table if not exists test.user1(
name string comment 'name',
salary float comment 'salary',
address struct<country:string, city:string> comment 'home address'
)
comment 'description of the table'
partitioned by (age int)
row format delimited fields terminated by '\t'
stored as orc;
没有指定external
关键字,则为管理表
,跟mysql
一样,if not exists
如果表存在则不做操作,否则则新建表。comment
可以为其做注释,分区为age
年龄,列之间分隔符是\t
,存储格式为列式存储orc
,存储位置为默认位置,即参数hive.metastore.warehouse.dir
(默认:/user/hive/warehouse)指定的hdfs
目录。
2. 拷贝表
使用like
可以拷贝一张跟原表结构一样的空表,里面是没有数据的。
create table if not exists test.user2 like test.user1;
3. 查看表结构
通过desc [可选参数] tableName
命令查看表结构,可以看出拷贝的表test.user1
与原表test.user1
的表结构是一样的。
hive> desc test.user2;
OK
name string name
salary float salary
address struct<country:string,city:string> home address
age int
# Partition Information
# col_name data_type comment
age int
也可以加formatted
,可以看到更加详细和冗长的输出信息。
hive> desc formatted test.user2;
OK
# col_name data_type comment
name string name
salary float salary
address struct<country:string,city:string> home address
# Partition Information
# col_name data_type comment
age int
# Detailed Table Information
Database: test
Owner: hdfs
CreateTime: Mon Dec 21 16:37:57 CST 2020
LastAccessTime: UNKNOWN
Retention: 0
Location: hdfs://nameservice2/user/hive/warehouse/test.db/user2
Table Type: MANAGED_TABLE
Table Parameters:
COLUMN_STATS_ACCURATE {\"BASIC_STATS\":\"true\"}
numFiles 0
numPartitions 0
numRows 0
rawDataSize 0
totalSize 0
transient_lastDdlTime 1608539877
# Storage Information
SerDe Library: org.apache.hadoop.hive.ql.io.orc.OrcSerde
InputFormat: org.apache.hadoop.hive.ql.io.orc.OrcInputFormat
OutputFormat: org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat
Compressed: No
Num Buckets: -1
Bucket Columns: []
Sort Columns: []
Storage Desc Params:
field.delim \t
serialization.format \t
4. 删除表
这跟sql
中删除命令drop table
是一样的:
drop table if exists table_name;
对于管理表(内部表),直接把表彻底删除了;对于外部表,还需要删除对应的hdfs
文件才会彻底将这张表删除掉,为了安全,通常hadoop
集群是开启回收站功能的,删除外表表的数据就在回收站,后面如果想恢复也是可以恢复的,直接从回收站mv
到hive
对应目录即可。
5. 修改表
大多数表属性可以通过alter table
来修改。
5.1 表重命名
alter table test.user1 rename to test.user3;
5.2 增、修、删分区
增加分区使用命令alter table table_name add partition(...) location hdfs_path
alter table test.user2 add if not exists
partition (age = 101) location '/user/hive/warehouse/test.db/user2/part-0000101'
partition (age = 102) location '/user/hive/warehouse/test.db/user2/part-0000102'
修改分区也是使用alter table ... set ...
命令
alter table test.user2 partition (age = 101) set location '/user/hive/warehouse/test.db/user2/part-0000110'
删除分区命令格式是alter table tableName drop if exists partition(...)
alter table test.user2 drop if exists partition(age = 101)
5.3 修改列信息
可以对某个字段进行重命名,并修改位置、类型或者注释:
修改前:
hive> desc user_log;
OK
userid string
time string
url string
修改列名time
为times
,并且使用after
把位置放到url
之后,本来是在之前的。
alter table test.user_log
change column time times string
comment 'salaries'
after url;
再来看表结构:
hive> desc user_log;
OK
userid string
url string
times string salaries
time -> times
,位置在url
之后。
5.4 增加列
hive
也是可以添加列的:
alter table test.user2 add columns (
birth date comment '生日',
hobby string comment '爱好'
);
5.5 删除列
删除列不是指定列删除,需要把原有所有列写一遍,要删除的列排除掉即可:
hive> desc test.user3;
OK
name string name
salary float salary
address struct<country:string,city:string> home address
age int
# Partition Information
# col_name data_type comment
age int
如果要删除列salary
,只需要这样写:
alter table test.user3 replace columns(
name string,
address struct<country:string,city:string>
);
这里会报错:
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Replacing columns cannot drop columns for table test.user3. SerDe may be incompatible
这张test.user3
表是orc
格式的,不支持删除,如果是textfile
格式,上面这种replace
写法是可以删除列的。通常情况下不会轻易去删除列的,增加列倒是常见。
5.6 修改表的属性
可以增加附加的表属性,或者修改属性,但是无法删除属性:
alter table tableName set tblproperties(
'key' = 'value'
);
举例:这里新建一张表:
create table t8(time string,country string,province string,city string)
row format delimited fields terminated by '#'
lines terminated by '\n'
stored as textfile;
这条语句将t8表中的字段分隔符'#'修改成'\t';
alter table t8 set serdepropertyes('field.delim'='\t');
关注公众号:Java大数据与数据仓库,领取资料,学习大数据技术。
Hive表的基本操作的更多相关文章
- 导hive表项目总结(未完待续)
shell里面对日期的操作 #!/bin/bash THIS_FROM=$(date +%Y%m%d -d "-7 day") THIS_TO=$(date +%Y-%m-%d - ...
- MySQL学习笔记02_数据库和表的基本操作
02_1 操作数据库 (1)创建数据库 CREATE DATABASE [IF NOT EXISTS] db_name [create_specification[, create_specifica ...
- hive 表分区操作
hive的数据查询一般会扫描整个表,当表数据太大时,就会消耗些时间,有时候我们只需要对部分数据感兴趣,所以hive引入了分区的概念 hive的表分区区别于一般的分布式分区(hash分区,范围分区 ...
- 如何快速把hdfs数据动态导入到hive表
1. hdfs 文件 {"retCode":1,"retMsg":"Success","data":[{" ...
- HDFS文件和HIVE表的一些操作
1. hadoop fs -ls 可以查看HDFS文件 后面不加目录参数的话,默认当前用户的目录./user/当前用户 $ hadoop fs -ls 16/05/19 10:40:10 WARN ...
- 用puthivestreaming把hdfs里的数据流到hive表
全景图: 1. 创建hive表 CREATE TABLE IF NOT EXISTS newsinfo.test( name STRING ) CLUSTERED BY (name)INTO 3 ...
- spark使用Hive表操作
spark Hive表操作 之前很长一段时间是通过hiveServer操作Hive表的,一旦hiveServer宕掉就无法进行操作. 比如说一个修改表分区的操作 一.使用HiveServer的方式 v ...
- spark+hcatalog操作hive表及其数据
package iie.hadoop.hcatalog.spark; import iie.udps.common.hcatalog.SerHCatInputFormat; import iie.ud ...
- 【原】创建Hive表,分号分隔符“;”引起的异常
[障碍再现] 在创建支持Map数据结构的Hive表时,抛出如下异常 hive> create table tab_map(name string,info map<string,strin ...
随机推荐
- CentOS下安装SublimeText
1.Install the GPG key: sudo rpm -v --import https://download.sublimetext.com/sublimehq-rpm-pub.gpg 2 ...
- XFF SSTI 模板注入 [BJDCTF2020]The mystery of ip
转自https://www.cnblogs.com/wangtanzhi/p/12328083.html SSTI模板注入:之前也写过:https://www.cnblogs.com/wangtanz ...
- FM解析(因子分解机,2010)
推荐参考:(知乎) https://zhuanlan.zhihu.com/p/37963267 要点理解: 1.fm应用场景,为什么提出了fm(和lr的不同点) ctr预测,特征组合,fm的隐向量分解 ...
- https中间人攻击
攻击过程: 服务器向客户端发送公钥. 攻击者截获公钥,保留在自己手上. 然后攻击者自己生成一个[伪造的]公钥,发给客户端. 客户端收到伪造的公钥后,生成加密hash值发给服务器. 攻击者获得加密has ...
- OI学习过程记录
这帖子本来是教练为了给低年级学生分享经验而让我写的学习经历,不过等我退役之后可能就变成回忆录了. 初三 WC 前:上了正睿的线上课程,练了一些模拟赛,同时也正在学文化课. 然后,莫名奇妙1膜考了全校前 ...
- js获取url参数、图片转本地base64跨域问题
获取url参数是经常需要用的一个方法,url上的参数可以让我们的程序执行更灵活. 图片转本地也是很实用的,因为海报合成通常只支持本地. 下面我们来看看这些功能的实现: 获取所有参数,采用split拆分 ...
- 容器编排系统之Kubernetes基础入门
一.kubernetes简介 1.什么是kubernetes?它是干什么用的? kubernetes是google公司用go语言开发的一套容器编排系统,简称k8s:它主要用于容器编排:所谓容器编排简单 ...
- 来感受Linux命令行的“真香定律”
Shell看起来只是一个黑黑的命令框,刚开始接触会觉得很丑,毕竟与Win/Mac的华丽界面比起来,命令行终端直接可以丑拒了.但是,实际上它的功能要强大得多,毕竟Linux一开始就是广泛应用于服务器,通 ...
- GitBook简单的使用
GitBook 是一个基于 Node.js 的命令行工具,支持 Markdown 和 AsciiDoc 两种语法格式,可以输出 HTML.PDF.eBook 等格式的电子书. 使用 GitBook 管 ...
- [实用指南]如何使您的旧代码库(遗留代码)符合MISRA C 2012编码规范?
重用旧代码是现实,但是在安全关键型软件项目中重用旧代码并实现MISRA C 2012的完全合规性是艰巨的任务. 最初的MISRA原则是为了在开发代码时应用而创建的,即使文档本身也有警告: " ...