Hive小白学习记录 ——(一)基本操作

基本命令

  • Hive 数据类型

    类型大小
    TINYINT1 byte
    SMALLINT2 bytes
    INT4 bytes
    BIGINT8 bytes
    FLOAT8 bytes
    DOUBLE38 digits
    DECIMAL
    BINARY
    BOOLEAN
    STRING
    CHAR
    VARCHAR
    DATE
    TIMESTAMP
  • 常用命令

    在 Hive 中可以使用hdfs命令、即dfs命令

增 (创建+添加数据)

1、创建一个数据库,mysql记录数据库得描述信息,对应HDFS 上的数据库文件夹

create database <basename>;
e.g.
	create DATABASE IF NOT EXISTS myhive
	COMMENT 'hive database demo'
	LOCATION '/hdfs/directory'    //指定数据库在HDFS上的存放位置
	WITH DEPROPERTIES ('creator'='penny', 'date'='2020-7-14');

hive> create database myhive;  //创建一个数据库 myhive

2、创建表 mysql记录表的描述信息,对应HDFS的表文件夹,不加修饰则是内部表【托管表managed_table 即由Hive管理】

create table <tablename> (id int, name string, age int,...)
comment 'table description'
row format delimited
fields terminated by ','
lines terminated by '\n'
stored as textfile;



e.g.
### 1. 在数据库myhive 创建一张student表
hive> create table student( id int,
                          	name string,
                          	sex string,
                          	age int,
                            department string
                          ) comment 'table description' row format delimited fields 								terminated by ','
                            lines terminated by '\n'
							stored as textfile;
							
2. create table if not exists student (
    								   name string, 
                                       str_arr ARRAY<string>,
                                       t_struct<sex:string, age:int>,
                                       t_map MAP<string, int>,
                                       t_map_arr<string, ARRAY<string>>
                                       )
                                       row format delimited fields by ','
                                       map keys terminated by ':';

## 注 ##: 分隔符不能为 分号 ';'

3、从本地加载数据,即添加数据,——> 数据put 到HDFS上对应的文件夹中

load data local inpath '...[本地路径]' overwrite into table <tablename> 
// 省去 local 则是加载集群上的数据,该加载时移动
//overwrite 会覆盖原来的数据

e.g.
//向表里添加数据
hive> load data local inpath "/home/hadoop/student.txt" into table student;

查(数据库、表)

  • 所有数据库

    show databases;
    
  • 当前使用的数据库

    select current_database();
    
  • 查看数据库结构

    desc database <databasename>;
    
  • 查看数据库结构及其扩展信息

    desc database extended <databasename>;
    
  • 显示所有表

    show tables;
    
  • 查看表结构

    desc <tablename>;
    
  • 查看表结构及其扩展信息

    desc extended <tablename>;
    desc formatted <tablename>;   //友好显示
    
  • 查询表中数据

    select * from student;
    

3、使用新的数据库myhive

hive> use myhive;

改(数据库、表 结构、数据)

  • 修改数据库结构,增加数据库属性

    alter database <databasename> set dbproperties('created='penny');
    //属性时键值对,可以自行添加
    
  • 重命名表名

    alter table <tablename> rename to <tablename>;
    
  • 添加列

    alter table <tablename> add columns (...)
    
  • 复制表

    //复制mybase数据库中的test2 到 default中的test1中,只复制表结构,不复制数据
    create table default.test1 like mybase.test2; //复制
    
  • 创建表的时候复制数据,改功能不能用于外部表

    create table <tablename> as select ... from <tablename> where ...
    
  • 复制表数据,批量插入

    insert into <tablename> select ... from <tablename> where ...
    
  • 导出数据,即将 hive[hdfs]中的数据导出至本地

    insert overwrite local directory '...[local path]' 
    	   select * from test where
    
  • 导出表到hdfs

    export table employee to '/home/penny/tmp';
    从hdfs上导入数据到一张新表中
    import table employee_imported from 'home/penny/tmp';
    
  • 数据类型转换

    cast (value as type);
    select cast('100' as int) from xxx;   ??? 不能转换整个列吗,那意思只能修改表的结构
    
  • 修改分隔符

    alter table <tablename> set serdproperties('field.delim' = ',');
    
  • 修改表的位置

    alter table <tablename> set location '...[path]';
    
  • 保护表【不能被删除】

    alter table <tablename> enable no_drop;
    alter table <tablename> disable no_drop;  //取消保护
    
  • 离线表【不能查询】

    alter table <tablename> enable offline;
    alter tabel <tablename> disable offline; //取消离线
    
  • 连接 join

    HIVE 默认时mapjoin
    属性设置为hive.auto.convert.join, 默认时true
    动态设置mapjoin
    	select /*MAPJOIN(table_name) */...
    mapjoin 不支持以下操作
    	在UNION ALL, LATERAL VIEW, GROUP BY/JOIN/SORT BY/ CLUSTER BY/
    	DISTRIBUTE BY 之后使用
    	在UNION,JOIN,and 另一个MAPJOIN之前使用
    
  • 排序 order by

    1. order by 全局排序
    2. sort by 每一个reducer排序,并不整体排序
    3. distribute by 类似于分组
    4. cluster by 先distribute by 后 sort by, 即先分组【reducer】,后组内排序
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值