1.建库

  • hive中有一个默认的库
    库名: default
    库目录:你的hdfs地址/user/hive/warehouse
  • 新建库create database db_order; 库建好后,在hdfs中会生成一个库目录:
    你的hdfs地址/user/hive/warehouse/db_order.db

2.建表

2.1基本建表语句

create table t_order(id string,create_time string,amount float,uid string);
  • 表建好后,会在所属的库目录中生成一个表目录
    /user/hive/warehouse/db_order.db/t_order
    只是,这样建表的话,hive会认为表数据文件中的字段分隔符为 ^A(\001)
  • 正确的建表语句为:
create table t_order(id string,create_time string,amount float,uid string)row format delimitedfields terminated by ',';
  • 这样就指定了,我们的表数据文件中的字段分隔符为 “,”

2.2内部表与外部表

  • 内部表(MANAGED_TABLE): 表目录按照hive的规范来部署,位于hive的仓库目录/user/hive/warehouse中
  • 外部表(EXTERNAL_TABLE): 表目录由建表用户自己指定external 外部表location ‘/access/log’ 指定外部表位置
• createexternaltable t_access(ip string,url string,access_time string)row format delimitedfields terminated by ','location'/access/log';
  • 外部表和内部表的特性差别:
    1.内部表的目录在hive的仓库目录中 VS 外部表的目录由用户指定
    2.drop一个内部表时:hive会清除相关元数据,并删除表数据目录
    3.drop一个外部表时:hive只会清除相关元数据;

2.3.分区表

  • 创建分区表
• create table t_access(ip string,url string,access_time string)partitioned by(dt string)
row format delimitedfields terminated by ',';
  • 向分区表导入数据
• load data local inpath '/root/access.log.2017-08-04.log' into table t_accesspartition(dt=‘20190527’);
load data local inpath '/root/access.log.2017-08-05.log' into table t_accesspartition(dt=‘20190528’);

3.删除表

  • drop table t_order; 删除表的效果是:
    hive会从元数据库中清除关于这个表的信息;
    hive还会从hdfs中删除这个表的表目录;