hive学习总结—数据库创建、表创建、加载数据

原文链接: http://gaoxianwei.iteye.com/blog/2160558

--创建数据库

create database if not exists sopdm

comment ‘this is test database’

with dbproperties(‘creator’=’gxw’,’date’=’2014-11-12’)     --数据库键值对属性信息

location ‘/my/preferred/directory’;
-- 查看数据库的描述信息和文件目录位置路径信息
describe database sopdm;

--查看数据库的描述信息和文件目录位置路径信息(加上数据库键值对的属性信息)

describe database extended sopdm;
-- 删除数据库
drop database if exists sopdm;

--级联删除数据库(当数据库还有表时,级联删除表后在删除数据库),默认是restrict

drop database if exists sopdm cascade;

--修改数据库


--只能修改数据库的键值对属性值。数据库名和数据库所在的目录位置不能修改

alter database sopdm set dmproperties(‘edited-by’=’gaoxianwei’);


-- 创建表
--其中tblproperties作用:按照键值对的格式为表增加额外的文档说明,也可用来表示数据库连接的必要的元数据信息

--hive会自动增加二个表属性:last_modified_by(最后修改表的用户名),last_modified_time(最后一次修改的时间)

create table if not exists sopdm.test1(name string comment ‘姓名’,salary float comment ‘薪水’)

comment ‘这是一个测试的表’

tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’)

location ‘/user/hive/warehouse/sopdm.db/test1’

-- 查看和列举表的 tblproperties 属性信息
show tblproperties table_name;


--使用like在创建表的时候,拷贝表模式(而无需拷贝数据)

create table if not exists sopdm.test2 like sopdm.test1;

-- 查看表的详细结构信息(也可以显示表是管理表,还是外部表。还有分区信息)
describe extended sopdm.test1;

--使用formatted信息更多些,可读性更强

describe formatted sopdm.test1;


--创建外部表

--删除表时,表的元数据会被删除掉,但是数据不会被删除

--如果数据被多个工具(如pig等)共享,可以创建外部表


create external table if not exists sopdm.test1(

name string comment ‘姓名’,

salary float comment ‘薪水’)

comment ‘这是一个测试的表’

tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’)

location ‘/user/hive/warehouse/sopdm.db/test1’

-- 创建外部表

--删除表时,表的元数据会被删除掉,但是数据不会被删除

--如果数据被多个工具(如pig等)共享,可以创建外部表

create external table if not exists sopdm.test1(

name string comment ‘姓名’,

salary float comment ‘薪水’)

comment ‘这是一个测试的表’

tblproperties(‘creator’=’me’,’created_at’=’2014-11-13 09:50:33’)

location ‘/user/hive/warehouse/sopdm.db/test1’
-- 删除表
drop table if exists table_name;

-- 修改表 - 表重命名
alter table old_table_name rename to new_table_name;

-- 修改列信息
alter table table_name

change column old_name new_name int

comment ‘this is comment’

after severity;         --字段移到severity字段之后(移动到第一个位置,使用first关键字)

-- 增加列
alter table table_name add columns(app_name string comment ‘application name’);

-- 删除或者替换列
alter table table_name replace columns(hms int comment ‘hhh’);

-- 通过查询语句向表中插入数据
--overwrite是覆盖,into是追加

insert overwrite table table_name

partition(country=’US’)

select * from table_name2 tn where tn.cnty=’US’


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值