HIVE-DDL

  • conf/hive-site。xml 元数据相关配置 hive的配置文件
  • more +文件名 查看
  • cp a b 拷贝
  • hive.log.dir = 临时目录
  • tail -100f 文件名 查看文件最后一百行
  • DDL:创建、删除、修改、msck、show 、describe
  • DML:
  • hive中:DB/TABLE/PARTITION 都是目录、文件夹
  • 在这里插入图片描述
  • create database hive; 创建数据库hive
  • 创建数据库可以指定存储目录 create database hive2 location ‘db_hive/directory’;
  • desc hive; 查看数据库 可以看到hive存放在hdfs上的路径,默认目录是:/user/hive/warehouse/hive.db
  • 修改hive参数的几种方式:
    • 1)set key 取值 set key=value 设置值 局部,只对当前窗口有效
    • 2)设置hive-site.xml 全局
  • 创建表 1. create table emp(
    • empid int,
    • ename string,
    • job string,
    • mgr int,
    • hiredata string,
    • sal double,
    • comm double,
    • deptno int
    • ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’
  • 设置可以看到数据库 set hive.cli.print.current.db=true;
  • 加载数据到表:LOAD DATA LOCAL INPATH '/home/hadoop/data/tmp.txt ’ OVERWRITE INTO TABLE emp
  • 2.复制表结构:== CREATE TABLE emp2 LIKE emp;==
  • 3.拷贝表结构和表数据: CREATE table emp3 as select * from emp;\
  • 查看表结构的详细信息:desc extended emp3; 或者 desc formatted emp3
  • 修改表名: alter table emp3 rename to new_emp3;
  • 数值类型:int \bigint \float\double\decimal
  • 时间类型 : string
  • 布尔类型:生产环境通常会使用0/1
  • row format 两大分隔符:行与行 字段与字段之间的分隔符
  • 列分隔符:默认的是 \001
  • 文件格式:行式 列式
  • Table Type:MANAGED_TABLE 内部表
  • 内部表和外部表的区别:hive默认是内部表,如果想使用外部表 create external table tablename
    • 删除内部表的时候 数据+元数据都会被删除
    • 删除外部表的数据:数据不删,元数据删
  • 分区表:create table order_partition(oreder_no string,event_time string) partitioned by(event_month string) row format delimited fields terminated by ‘\t’
  • 加载数据 load data local inpath ’ ’ overwrite into table order_partition PARTITON (event_month=‘2014-05’);
  • 新增分区:==alter table order_partition add if not exists partiton (event_month=‘2014-06’);
  • 以上属于静态分区,create table emp_static_partiton(‘empno’ int,‘ename’ string,‘job’ string,‘mgr’ int,hiredata string,sal double,comm double) partitoned by(deptno int) row format delimited fields terminated by ‘\t’
  • 加载数据到静态分区表:insert into table emp_static_partiton PARTITION (deptno=10) select empno, ename, job, mgr , hiredata, sal, comm from emp where deptno=10;
    -=======================================以下是动态分区
  • 创建动态分区表:create table emp_dynamic_partiton(empno int,ename string,job string,mgr int,hiredata string,sal double,comm double) partitoned by(deptno int) row format delimited fields terminated by ‘\t’
  • 插入数据之前要将hive的模式改为非严格模式:set hive.exec.dynamic.partiton.mode=nonstrict
  • emp表按照部门编号写到制定分区:insert into table emp_dynamic_partition PARTITION (deptno) select empno, ename, job, mgr , hiredata, sal, comm,deptno from emp;
  • 复杂的数据类型掌握:如何存、如何取
  • 1.array类型
    creat table hive_array(name string,work_location array<'string>)
    row format delimited fields terminated by ‘\t’------------->row format是字段和字段的分隔符
    COLLECTION ITEMS TERMINATED BY ‘,’-------------->ARRAY集合里面的分割符
  • 问:求上班地点在天津的人员:select * from hive_array where array_contains(work_location,‘tianjin’);
  • 2.map类型 例如father:xiaoming#mother:xiaohuang#brother:xiaoxu
  • 创建表
    creat table hive_map(id int,name string,members map<'string,string>,age int)
    row format delimited fields terminated by ‘\t’------------->row format是字段和字段的分隔符
    COLLECTION ITEMS TERMINATED BY ‘#’-------------->每组键值对之间的分隔符是#
    MAP KEYS TERMINATED BY ‘:’--------------------->key和value之间的分隔符
  • 查询数据:select id,name,age,members[‘father’] from hive_map;
  • 求map中的key:select map_keys(members) from hive_map;
  • 求map中的value:select map_values(members) from hive_map;
  • 求map中的键值对个数:select size(members) from hive_map;
  • 3.struct类型 192.168.1.1#zhangsan:40
    创建表:create table hive_struct(ip string,userinfo struct<'name:string,age:int>)
    row format delimited fields terminated by ‘#’------------->row format是字段和字段的分隔符
    COLLECTION ITEMS TERMINATED BY ‘:’-------------->每组键值对之间的分隔符是#
    查询表:select userinfo.name,userinfo.age from hive_struct;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值