hive(数据仓库)常用命令

hive的常见操作命令

-- 开启hive
hive 
-- 查看当前数据库
select current_database();
-- 强制删除数据库(hdfs上的也删了)
drop database demo cascade;
-- 创建数据库
drop database if exists mydemo;
create database mydemo;
-- 选择数据库
use mydemo;
-- 创建一个内部表
drop table if exists userinfos;
create table userinfos(
userid int ,
username string,
birthday DATE
);
-- 添加往userinfos内部表中插入数据
insert into userinfos values(1,'zhangsan','1996-09-24'),(2,'lisi','1996-08-24'),(3,'wangwu','1995-12-29');
-- 查看userinfos表
select * from userinfos;
在hdfs分布式文件系统中(192.168.56.101:50070)查看
/opt/soft/hive110/warehouse/mydemo.db中有没有多一个userinfos文件
-- 删除userinfos内部表
drop table userinfos;
-- 再次查看
select * from userinfos; -- 没有这个表了也就等于
/opt/soft/hive110/warehouse/mydemo.db -- 这里也没有
								-- 而且数据的存取路径中的表(hdfs上的)也被删除了
-- 创建一个外部表
-- 先在hdfs中put一个userinfos的txt文件
hdfs dfs -mkdir /userinfo
hdfs dfs -put /opt/soft/data/user.txt /userinfo

create external table userinfos(
userid int,
username string,        -- 最前面千万不要按tab键
age int
)
row format delimited
fields terminated by ' '     -- 何如分隔列(字段)
stored as textfile          --文件存储格式
location '/userinfo'		--hdfs中数据存储的路径
-- 这里如果只用/userinfo 且userinfo文件夹中有两个及以上个文件 ,就删掉多余的文件 location这里只可以写到文件夹,或者用load data inpath 的方法导入数据。

-- 查看表结构和表数据
desc userinfos;
select * from userinfos;
-- 查看完全的表结构 可以查看此表是内部还是外部表
describe formatted userinfos;

-- 删除外部表
drop table userinfos;
-- 再次查看
select * from userinfos;  -- 没有这个表了  hdfs上的 warehouse 里的 mydemo.db中的userinfos表也没有了
						-- 但是 /useinfo/user.txt 还在 如果是内部表这个数据就不在了

-- 创建一个带有集合字段的外部表
create external table myuser(
userid string ,
username string,
like array<string>,
age int
)
row format delimited fields terminated by ' '
collection items terminated by ','
location 'user';
-- 查看表中字段含有xxx的数据
select * from myuser where array_contains(like,'football');
-- 修改数据分隔方式
alter table myuser set serdeproperties('fields.delim'=',');

-- 建表高阶语句
create table employees as
with
r1 as (select name from r2 where name='mike'),
r2 as (select name form employees where sex = 1 and age = 20),
r3 as (select name from employees where sex =0)
select * from r1 union all select * from r3;

-- 修改表名
alter table employees rename to new_employees;
-- 修改表的字段
alter table employees change name empname string;
-- 删除表的字段
alter table employees drop  empname ;
-- 修改表字段的类型
alter table employees modify empname varchar;
-- 都可以通过下面的代码看到修改成功是否
describe formatted employees;

-- 删除表数据
truncate table employees; -- select * employees 不会保没有表这个错 但是也没有数据

-- 使用load将数据导入表中
load data inpath '/user/a.txt' into table userinfos;

-- 静态分区和动态分区
create external table origninfos(
id string,
name string,
sex string,
age int
)
row format delimited fields terminated by ' '
location '/orgin'

create external table partinfos(
id string ,
name string,
age int
)
partitioned by (sex string)
row format delimited fields terminated by ' ';
-- 开启动态分区
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;

insert into partinfos partition(sex) select id,name,age,sex from orgininfos; 

-- 修改数据分隔方式
alter table tablename set serdeproperties('field.delim'=' ');
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值