1)Hive基本数据类型

首先,我们简单叙述一下HiveQL的基本数据类型。

Hive支持基本数据类型和复杂类型, 基本数据类型主要有数值类型(INT、FLOAT、DOUBLE ) 、布尔型和字符串, 复杂类型有三种:ARRAY、MAP 和 STRUCT。

a.基本数据类型

  • TINYINT: 1个字节
  • SMALLINT: 2个字节
  • INT: 4个字节
  • BIGINT: 8个字节
  • BOOLEAN: TRUE/FALSE
  • FLOAT: 4个字节,单精度浮点型
  • DOUBLE: 8个字节,双精度浮点型STRING 字符串

b.复杂数据类型

  • ARRAY: 有序字段
  • MAP: 无序字段
  • STRUCT: 一组命名的字段

2)常用的HiveQL操作命令

Hive常用的HiveQL操作命令主要包括:数据定义、数据操作。接下来详细介绍一下这些命令即用法(想要了解更多请参照《Hive编程指南》一书)。

a.数据定义:主要用于创建修改和删除数据库、表、视图、函数和索引。

创建、修改和删除数据库

  1.  
    create database if not exists hive; #创建数据库
  2.  
    show databases; #查看Hive中包含数据库
  3.  
    show databases like 'h.*'; #查看Hive中以h开头数据库
  4.  
    describe databases; #查看hive数据库位置等信息
  5.  
    alter database hive set dbproperties; #为hive设置键值对属性
  6.  
    use hive; #切换到hive数据库下
  7.  
    drop database if exists hive; #删除不含表的数据库
  8.  
    drop database if exists hive cascade; #删除数据库和它中的表

 

注意,除 dbproperties属性外,数据库的元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置,没有办法删除或重置数据库属性。

创建、修改和删除表

  1.  
    #创建内部表(管理表)
  2.  
    create table if not exists hive.usr(
  3.  
    name string comment 'username',
  4.  
    pwd string comment 'password',
  5.  
    address struct<street:string,city:string,state:string,zip:int>,
  6.  
    comment 'home address',
  7.  
    identify map<int,tinyint> comment 'number,sex')
  8.  
    comment 'description of the table'
  9.  
    tblproperties('creator'='me','time'='2016.1.1');
  10.  
    #创建外部表
  11.  
    create external table if not exists usr2(
  12.  
    name string,
  13.  
    pwd string,
  14.  
    address struct<street:string,city:string,state:string,zip:int>,
  15.  
    identify map<int,tinyint>)
  16.  
    row format delimited fields terminated by ','
  17.  
    location '/usr/local/hive/warehouse/hive.db/usr';
  18.  
    #创建分区表
  19.  
    create table if not exists usr3(
  20.  
    name string,
  21.  
    pwd string,
  22.  
    address struct<street:string,city:string,state:string,zip:int>,
  23.  
    identify map<int,tinyint>)
  24.  
    partitioned by(city string,state string);
  25.  
    #复制usr表的表模式
  26.  
    create table if not exists hive.usr1 like hive.usr;
  27.  
     
  28.  
    show tables in hive;
  29.  
    show tables 'u.*'; #查看hive中以u开头的表
  30.  
    describe hive.usr; #查看usr表相关信息
  31.  
    alter table usr rename to custom; #重命名表
  32.  
     
  33.  
    #为表增加一个分区
  34.  
    alter table usr2 add if not exists
  35.  
    partition(city=”beijing”,state=”China”)
  36.  
    location '/usr/local/hive/warehouse/usr2/China/beijing';
  37.  
    #修改分区路径
  38.  
    alter table usr2 partition(city=”beijing”,state=”China”)
  39.  
    set location '/usr/local/hive/warehouse/usr2/CH/beijing';
  40.  
    #删除分区
  41.  
    alter table usr2 drop if exists partition(city=”beijing”,state=”China”)
  42.  
    #修改列信息
  43.  
    alter table usr change column pwd password string after address;
  44.  
     
  45.  
    alter table usr add columns(hobby string); #增加列
  46.  
    alter table usr replace columns(uname string); #删除替换列
  47.  
    alter table usr set tblproperties('creator'='liming'); #修改表属性
  48.  
    alter table usr2 partition(city=”beijing”,state=”China”) #修改存储属性
  49.  
    set fileformat sequencefile;
  50.  
    use hive; #切换到hive数据库下
  51.  
    drop table if exists usr1; #删除表
  52.  
    drop database if exists hive cascade; #删除数据库和它中的表
  53.  

posted on 2021-10-21 15:45  sean1246  阅读(35)  评论(0编辑  收藏  举报