hive的表的基本操作
环境简介
实验环境使用的是cloudera-quickstart-vm-5.0环境。
内容摘要
- 创建表
- 修改表名
- 修改表中的列名
- 添加列
- 删除列
- 替换列
正文
Alter Table 语句
上面所述的6种针对hive的操作都是使用Alter Table来完成的。
Alter Table的语法如下:
ALTER TABLE name RENAME TO new_name
ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...])
ALTER TABLE name DROP [COLUMN] column_name
ALTER TABLE name CHANGE column_name new_name new_type
ALTER TABLE name REPLACE COLUMNS (col_spec[, col_spec ...])
重命名表名
- 首先创建目标数据库和目标表
对于的hive sql语句如下:
create database testdb;-- 创建实验需要的数据库
-- 开始 创建表
create table testdb.student(
id int comment 'student id',
name string comment 'student name',
gender string comment 'student gender'
);
-- 结束 创建表
- 查看创建后的表的信息
查看表的信息,使用describe table来实现。对应的hive sql 语句如下:
-- 开始 查看表
describe testdb.student;
-- 结束 查看表
可以看到,该表已经成功创建,如果想要查看该表的详细信息,使用formatted关键字。代码如下:
-- 开始 查看表
describe formatted testdb.student;
-- 结束 查看表
- 修改表名
为了简单起见,我们这里将testdb.student修改为testdb.student2。对应的代码如下:
use testdb;-- 这里最好先切换数据库为目标数据库
alter table student rename to student2; -- tablea rename to tableb
describe testdb.student2; -- 用新表名查看表信息
修改字段信息
下表包含testdb.employees表的字段,它显示的字段要被更改(粗体)。
| 字段名 | 从数据类型转换 | 更改字段名称 | 转换为数据类型 |
|---|---|---|---|
| eid | int | eid | int |
| name | String | ename | String |
| salary | Float | salary | Double |
| designation | String | designation | String |
1. 准备工作
为了上面的实验,我们需要先创建testdb.employees表。对应的hive sql语句如下:
create table testdb.employees(
eid int comment 'this is employees id',
name string comment 'this is employee name',
salary float comment 'this is the salary of employee',
desination string );
-- 查看创建后的表
describe testdb.employees;
2. 修改表中的列信息 从1中的表我们知道我们需要将employees.name修改成employees.ename,还是就是将employees.salary的类型由float换成double类型。实现本案例的hive sql语句如下:
use testdb; -- 切换到目标数据库
-- 本条语句是将name 字段更改为ename string
alter table employees CHANGE name ename string;
-- 本条语句是将salary更改为salary double
alter table employees CHANGE salary salary double;
describe employees;
相信大家可以看出,需要修改字段的信息我们使用change 命令。命令格式为:
alter table chane old_column new_column new_clumn_type
添加字段
- 准备工作
我们仍然使用上例中的testdb.employees表来进行操作。这里就不再重复贴出代码。 - 新加的列信息
我们希望给员工添加上一个入职时间:joinDate date。为了实现该目标,我么需要输入的hive sql 如下所示:
use testdb;
alter table employees add COLUMNS (
joinDate date comment 'date type is supported by Hive0.12.0'
);
describe testdb.employees;
替换列
这里的替换和之前的修改有点类似,这里的替换应该更强调的是位置。
我们这里将employees中的eid和ename替换掉。
1. 动手之前先看一下当前的testdb.employees中有哪些字段吧
describe testdb.employees;
- 完成替换
为了完成替换的任务,我们编写如下的hive sql语句
use testdb;
alter table employees replace columns(
eid int epmid int,
ename string empname string
);
describe testdb.employees;
- 这里要说一声抱歉,如下两张图所示,我的语句并没有执行成功。
- 解决方法
use testdb;
alter table employees replace columns(
epmid int,
empname string
);
describe testdb.employees;
根据上面的语句和图片所示,我们可以看到REPLACE COLUMNS是将原来的表中的字段删除,然后使用新的字段来填充该表。
更多内容,请移步阅读:传送门
hive的表的基本操作的更多相关文章
- MySQL学习笔记02_数据库和表的基本操作
02_1 操作数据库 (1)创建数据库 CREATE DATABASE [IF NOT EXISTS] db_name [create_specification[, create_specifica ...
- 【转】Hive内部表、外部表
hive内部表.外部表区别自不用说,可实际用的时候还是要小心. 1. 内部表: create table tt (name string , age string) location '/input/ ...
- hive内部表、外部表
hive内部表.外部表区别自不用说,可实际用的时候还是要小心. Hive的数据分为表数据和元数据,表数据是Hive中表格(table)具有的数据:而元数据是用来存储表的名字,表的列和分区及其属性,表的 ...
- Hive内表和外表的区别
本文以例子的形式介绍一下Hive内表和外表的区别.例子共有4个:不带分区的内表.带分区的内表.不带分区的外表.带分区的外表. 1 不带分区的内表 #创建表 create table innerTabl ...
- hive内部表与外部表区别
1.在Hive里面创建一个表: hive> create table wyp(id int, > name string, > age int, > tele ...
- Hive学习之三 《Hive的表的详解和应用案例详解》
一.Hive的表 Hive的表分为内部表.外部表和分区表. 1.内部表,为托管表. 2.外部表,external. 3.分区表. 详解: 内部表,删除表的时候,数据会跟着删除. 外部表,在删除表的时候 ...
- hive 创建表和导入数据实例
//创建数据库create datebase hive;//创建表create table t_emp(id int,name string,age int,dept_name string,like ...
- S3c2440A WINCE平台HIVE注册表+binfs的实现
今天最大的收获莫过于把binfs和hive注册表同时在三星的平台上实现了,这可是前无古人啊(只是看到好多哥们说找不到三星的HIVE资料),哈哈哈.怕今天的成果日后成炮灰,还是写下来比较好,要养成这样的 ...
- hive删除表和表中的数据
hive删除表和表中的数据,以及按分区删除数据 hive删除表: drop table table_name; hive删除表中数据: truncate table table_name; hive按 ...
随机推荐
- P2物理引擎中文文档
P2物理引擎中文文档地址:https://github.com/schteppe/p2.js/wiki/Chinese-wiki-%E4%B8%AD%E6%96%87%E7%BB%B4%E5%9F%B ...
- 老李分享:webservice是什么?2
web service 组件 基本的 web service 平台是 XML + HTTP.所有标准的 web service 使用以下组件: SOAP(简单对象访问协议) UDDI(通用描述.发现与 ...
- 利用<meta http-equiv="refresh" content="0;URL=?id='.$id.'" />一条一条的更新数据
<meta http-equiv="refresh" content="0;URL=?id='.$id.'" /> 解释:页面定时刷新,后面加url ...
- json解包与json封包
首先,对两个名词进行简单的说明: 1.NSData 用来存储二进制的数据类型.NSData类提供了一种简单的方式,它用来设置缓冲区.将文件的内容读入缓冲区,或将缓冲区的内容写到一个文件.不变缓冲区(N ...
- c++中的namespace(附程序运行图)
实验于华中农业大学逸夫楼2017.3.10 namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的namespace,但是由于现在的程序的规模越来越大,程序的分工越 来越细,全 ...
- 跟着刚哥梳理java知识点——泛型(十三)
一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: public class GenericTest { public static void main(String[] a ...
- Timer,TimerTask通过程序计数器实现的定时任务
1.程序计数器 程序计数器(Program Counter Register)是一块较小的内存空间,它的作用可以看 做是当前线程所执行的字节码的行号指示器.在虚拟机的概念模型里(仅是概念模型, 各种虚 ...
- tmux鼠标配置出现错误unknown option: mode-mouse
setw -g mode-mouse on set -g mouse-select-pane on set -g mouse-resize-pane on set -g mouse-select-wi ...
- 初学VUE2.0
初学VUE2.0 (个人笔记,写完后发现好乱....下期使用markdown书写.) 概述 webstorm添加对VUE的支持 http://www.jianshu.com/p/142dae4f8b5 ...
- 学习css之文本属性
css3之文本属性: 1.缩进和水平对齐:text-indent, 通过使用 text-indent 属性,所有元素的第一行都可以缩进一个给定的长度,甚至该长度可以是负值. 这个属性最常见的用途是将段 ...