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 ...
随机推荐
- 云服务器AWD平台搭建
开学后实验室来了几个新同学,在线上CTF方面大家一直在持续学习,但AWD模式的CTF我们练习并不多,所以准备搭建一个AWD平台用于实验室成员的线下赛攻防练习. 最开始的是防灾科技大学的线下AWD靶场: ...
- centos7 yum搭建lamp
环境 系统:centos7 安装apache #yum 安装apache [root@localhost ~]# yum install httpd httpd-devel #启动httpd服务 [r ...
- jmeter使用中的问题
1.响应乱码 step1:指定请求节点下,新建后置控制器"BeanShell PostProcessor" step2:其脚本框中输入以下代码,保存 //获取响应代码Unicode ...
- Scrum 冲刺第二天
一.每日站立式会议 1.会议内容 1)进行每日工作汇报 张博愉: 昨天已完成的工作:制定测试计划.博客编写 今日工作计划:测试mappe里的接口 工作中遇到的困难:对测试接触得较少 张润柏: 昨天已完 ...
- vue Export2Excel 导出文件
使用需要引入这些js 在src目录下创建一个文件(vendor)进入Blob.js和Export2Excel.js npm install -S file-saver 用来生成文件的web应用程序 n ...
- P6100 [USACO19FEB]Painting the Barn G
本题解提供的做法思路应该是比较清晰的,可惜代码实现比较繁琐,仅供大家参考. 题解 不难发现 \(x\) ,\(y\) 的取值范围只有 \(200\) ,所以我们可以考虑从这里入手.我们可以先通过二维前 ...
- Power BI八年回望记
本人从事BI,数据仓库领域相关工作15个年头,这15年目睹了这个方向从火爆到逐渐被大数据领域不断吞食.中间零散关注Power BI好长时间,也算目睹了它的成长. 那天在网络上搜索power bi,无意 ...
- 【JAVA基础&Python】静态/非静态代码块
/* * * static静态代码块: * 调用静态属性的时候 对应类里面的静态代码块就会被直接执行 * 注意: 只会执行一次,只能调用类内静态结构的(方法/属性) * 作用: 初始化类的属性 * * ...
- Sql Server 数据把列根据指定的内容拆分数据
今天由于工作需要,需要把数据把列根据指定的内容拆分数据 其中一条数据实例 select id , XXXX FROM BIZ_PAPER where id ='4af210ec675927fa016 ...
- jmeter的一些知识目录
1.JDK安装及环境变量配置2.Jmeter安装及环境变量配置3.如何启动 jmeter 4.下载并安装mysql驱动5.创建JDBC连接池及配置6 .新建线程组及参数配置7.http默认请求及参数配 ...