1. 创建表

create table语句遵从sql语法习惯,只不过Hive的语法更灵活。例如,可以定义表的数据文件存储位置,使用的存储格式等。

  1. create table if not exists test.user1(
  2. name string comment 'name',
  3. salary float comment 'salary',
  4. address struct<country:string, city:string> comment 'home address'
  5. )
  6. comment 'description of the table'
  7. partitioned by (age int)
  8. row format delimited fields terminated by '\t'
  9. stored as orc;

没有指定external关键字,则为管理表,跟mysql一样,if not exists如果表存在则不做操作,否则则新建表。comment可以为其做注释,分区为age年龄,列之间分隔符是\t,存储格式为列式存储orc,存储位置为默认位置,即参数hive.metastore.warehouse.dir(默认:/user/hive/warehouse)指定的hdfs目录。

2. 拷贝表

使用like可以拷贝一张跟原表结构一样的空表,里面是没有数据的。

  1. create table if not exists test.user2 like test.user1;

3. 查看表结构

通过desc [可选参数] tableName命令查看表结构,可以看出拷贝的表test.user1与原表test.user1的表结构是一样的。

  1. hive> desc test.user2;
  2. OK
  3. name string name
  4. salary float salary
  5. address struct<country:string,city:string> home address
  6. age int
  7. # Partition Information
  8. # col_name data_type comment
  9. age int

也可以加formatted,可以看到更加详细和冗长的输出信息。

  1. hive> desc formatted test.user2;
  2. OK
  3. # col_name data_type comment
  4. name string name
  5. salary float salary
  6. address struct<country:string,city:string> home address
  7. # Partition Information
  8. # col_name data_type comment
  9. age int
  10. # Detailed Table Information
  11. Database: test
  12. Owner: hdfs
  13. CreateTime: Mon Dec 21 16:37:57 CST 2020
  14. LastAccessTime: UNKNOWN
  15. Retention: 0
  16. Location: hdfs://nameservice2/user/hive/warehouse/test.db/user2
  17. Table Type: MANAGED_TABLE
  18. Table Parameters:
  19. COLUMN_STATS_ACCURATE {\"BASIC_STATS\":\"true\"}
  20. numFiles 0
  21. numPartitions 0
  22. numRows 0
  23. rawDataSize 0
  24. totalSize 0
  25. transient_lastDdlTime 1608539877
  26. # Storage Information
  27. SerDe Library: org.apache.hadoop.hive.ql.io.orc.OrcSerde
  28. InputFormat: org.apache.hadoop.hive.ql.io.orc.OrcInputFormat
  29. OutputFormat: org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat
  30. Compressed: No
  31. Num Buckets: -1
  32. Bucket Columns: []
  33. Sort Columns: []
  34. Storage Desc Params:
  35. field.delim \t
  36. serialization.format \t

4. 删除表

这跟sql中删除命令drop table是一样的:

  1. drop table if exists table_name;

对于管理表(内部表),直接把表彻底删除了;对于外部表,还需要删除对应的hdfs文件才会彻底将这张表删除掉,为了安全,通常hadoop集群是开启回收站功能的,删除外表表的数据就在回收站,后面如果想恢复也是可以恢复的,直接从回收站mvhive对应目录即可。

5. 修改表

大多数表属性可以通过alter table来修改。

5.1 表重命名

  1. alter table test.user1 rename to test.user3;

5.2 增、修、删分区

增加分区使用命令alter table table_name add partition(...) location hdfs_path

  1. alter table test.user2 add if not exists
  2. partition (age = 101) location '/user/hive/warehouse/test.db/user2/part-0000101'
  3. partition (age = 102) location '/user/hive/warehouse/test.db/user2/part-0000102'

修改分区也是使用alter table ... set ...命令

  1. alter table test.user2 partition (age = 101) set location '/user/hive/warehouse/test.db/user2/part-0000110'

删除分区命令格式是alter table tableName drop if exists partition(...)

  1. alter table test.user2 drop if exists partition(age = 101)

5.3 修改列信息

可以对某个字段进行重命名,并修改位置、类型或者注释:

修改前:

  1. hive> desc user_log;
  2. OK
  3. userid string
  4. time string
  5. url string

修改列名timetimes,并且使用after把位置放到url之后,本来是在之前的。

  1. alter table test.user_log
  2. change column time times string
  3. comment 'salaries'
  4. after url;

再来看表结构:

  1. hive> desc user_log;
  2. OK
  3. userid string
  4. url string
  5. times string salaries

time -> times,位置在url之后。

5.4 增加列

hive也是可以添加列的:

  1. alter table test.user2 add columns (
  2. birth date comment '生日',
  3. hobby string comment '爱好'
  4. );

5.5 删除列

删除列不是指定列删除,需要把原有所有列写一遍,要删除的列排除掉即可:

  1. hive> desc test.user3;
  2. OK
  3. name string name
  4. salary float salary
  5. address struct<country:string,city:string> home address
  6. age int
  7. # Partition Information
  8. # col_name data_type comment
  9. age int

如果要删除列salary,只需要这样写:

  1. alter table test.user3 replace columns(
  2. name string,
  3. address struct<country:string,city:string>
  4. );

这里会报错:

  1. 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 修改表的属性

可以增加附加的表属性,或者修改属性,但是无法删除属性:

  1. alter table tableName set tblproperties(
  2. 'key' = 'value'
  3. );

举例:这里新建一张表:

  1. create table t8(time string,country string,province string,city string)
  2. row format delimited fields terminated by '#'
  3. lines terminated by '\n'
  4. stored as textfile;

这条语句将t8表中的字段分隔符'#'修改成'\t';

  1. alter table t8 set serdepropertyes('field.delim'='\t');

关注公众号:Java大数据与数据仓库,领取资料,学习大数据技术。

Hive表的基本操作的更多相关文章

  1. 导hive表项目总结(未完待续)

    shell里面对日期的操作 #!/bin/bash THIS_FROM=$(date +%Y%m%d -d "-7 day") THIS_TO=$(date +%Y-%m-%d - ...

  2. MySQL学习笔记02_数据库和表的基本操作

    02_1 操作数据库 (1)创建数据库 CREATE DATABASE [IF NOT EXISTS] db_name [create_specification[, create_specifica ...

  3. hive 表分区操作

    hive的数据查询一般会扫描整个表,当表数据太大时,就会消耗些时间,有时候我们只需要对部分数据感兴趣,所以hive引入了分区的概念    hive的表分区区别于一般的分布式分区(hash分区,范围分区 ...

  4. 如何快速把hdfs数据动态导入到hive表

    1. hdfs 文件   {"retCode":1,"retMsg":"Success","data":[{" ...

  5. HDFS文件和HIVE表的一些操作

    1. hadoop fs -ls  可以查看HDFS文件 后面不加目录参数的话,默认当前用户的目录./user/当前用户 $ hadoop fs -ls 16/05/19 10:40:10 WARN ...

  6. 用puthivestreaming把hdfs里的数据流到hive表

    全景图:   1. 创建hive表 CREATE TABLE IF NOT EXISTS newsinfo.test( name STRING ) CLUSTERED BY (name)INTO 3 ...

  7. spark使用Hive表操作

    spark Hive表操作 之前很长一段时间是通过hiveServer操作Hive表的,一旦hiveServer宕掉就无法进行操作. 比如说一个修改表分区的操作 一.使用HiveServer的方式 v ...

  8. spark+hcatalog操作hive表及其数据

    package iie.hadoop.hcatalog.spark; import iie.udps.common.hcatalog.SerHCatInputFormat; import iie.ud ...

  9. 【原】创建Hive表,分号分隔符“;”引起的异常

    [障碍再现] 在创建支持Map数据结构的Hive表时,抛出如下异常 hive> create table tab_map(name string,info map<string,strin ...

随机推荐

  1. swpuCTF2019 web1 无列名注入

    上周参加的swpuctf比赛第一道web题做了好久,在最后一个小时用非预期的方法做出来了,看了官方题解之后记录一下wp里面的无列名注入. 关于无列名注入可以看一下这篇链接 https://www.ch ...

  2. Oracle批量新增数据最佳实践

    一.需求描述 现在的项目改造过程中,从国产的Gbase数据库改造为Oracle数据库,遇到一个问题有的业务操作需要批量新增数据. 这也是一个比较常规的操作,有很多地方确实需要一次性新增多条数据.Gba ...

  3. eclipse 搭建连接 activemq

    今天我特地写下笔记,希望可以完全掌握这个东西,也希望可以帮助到任何想对学习这个东西的同学. 1.下载activemq压缩包,并解压(如果需要下载请看文章尾部附录) 2.进入bin文件夹,(64位电脑就 ...

  4. MBR分区表为什么最大只能识别2TB硬盘容量

    1. 前言 最近公司的服务器硬盘坏了,需要换一个新的硬盘,容量是2TB,用的fdisk进行分区,期间搜索分区工具,看到了关于MBR(Master Boot Record)与GPT(GUID parti ...

  5. PHP文件上传、错误处理

    说明 这篇是针对之前php知识的补充内容 目录 说明 1. PHP目录处理函数 2. PHP文件权限设置 3. PHP文件路径函数 4. PHP实现文件留言本 5.PHP文件上传 1. php文件上传 ...

  6. DVWA各级文件包含漏洞

    File Inclusion文件包含漏洞 漏洞分析 程序开发人员通常会把可重复使用的函数写入到单个文件中,在使用某些函数时,直接调用此文件,而无需再次编写,这种调用文件的过程被称为包含. 有时候由于网 ...

  7. Docker(一):Docker安装

    简介   Docker是dotcloud公司开源的一款产品,主要基于PAAS平台为开发者提供服务.是解决运行环境和配置问题软件容器,方便做持续集成并有助于整体发布的容器虚拟化技术. Docker组件 ...

  8. css进阶 04-如何让一个元素水平垂直居中?

    04-如何让一个元素水平垂直居中? #前言 老板的手机收到一个红包,为什么红包没居中? 如何让一个子元素在父容器里水平垂直居中?这个问题必考,在实战开发中,也应用得非常多. 你也许能顺手写出好几种实现 ...

  9. PHP MySQL 快速导入10万条数据

    项目背景 数据来源:所有数据均为外部导入,最大数据量在10w+ 输出数据:导出经过业务处理之后的数据 使用框架:fastadmin 涉及的问题: 1.数据读取 2.数据保存 使用数据:10w+ 解决方 ...

  10. [日常摸鱼]bzoj1502[NOI2005]月下柠檬树-简单几何+Simpson法

    关于自适应Simpson法的介绍可以去看我的另一篇blog http://www.lydsy.com/JudgeOnline/problem.php?id=1502 题意:空间里圆心在同一直线上且底面 ...