Hive 数据仓库

1. 配置

  1. 初始化默认的 derby 数据库,作为 Hive 的元数据库(MetaStore)

    bin/schematool -dbType derby -initSchema
    

注意:初始化 Derby 作为元数据库时,会在当前 shell 路径下生成 metastore_db/ 目录。如在别处启动 Hive,而此处无 metastore_db/ 目录时,将无法启动 Hive,需要再在此路径下,重新初始化,生成该目录。

  1. 配置 MySQL 作为 Hive 的元数据库

    1. 在 conf 目录下创建 hive-site.xml,配置如下

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
      
      <configuration>
        <property>
            <name>javax.jdo.option.ConnectionURL</name>
            <value>jdbc:mysql://hm02:3306/hive?createDatabaseIfNotExist=true</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionDriverName</name>
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionUserName</name>
            <value>root</value>
        </property>
        <property>
            <name>javax.jdo.option.ConnectionPassword</name>
            <value>123</value>
        </property>
      </configuration>
      
    2. com.mysql.jdbc.Driver 复制到 lib/ 目录下

    3. 初始化 MySQL 的 MetaStore

      bin/schematool -dbType mysql -initSchema
      

注意 MySQL 需要授权远程登录

2. 使用

  • 启动 Hive

    bin/hive
    
  • DQL

    --查看数据库
    show databases;
    
    --查看当前数据库的表信息 (Derby 默认使用 default 作为数据库)
    show tables;
    
    --查看表结构
    show create table student;
    
    --启用 MapReduce 进行查询
    select * from student order by id desc;
    select count(1) from student;
    
  • DDL

    --创建表
    create table student(id int, name string);
    
    --创建表 (指定分隔符)
    create table student(id int, name string) row format delimited fields terminated by '\t';
    
  • DML

    --Hive 独有语法,将指定路径的文件 导入至数据仓库
    ----如未指定分隔符,则导入的数据无法被解析,全部为 NULL
    load data local inpath '/opt/stu.txt' into table student;
    

3. 说明

Derby 数据库 为只能允许一个会话连接的文件数据库,因此需要修改为 MySQL 数据库,从而支持多用户会话。

MySQL 作为元数据库时的表信息:

  • TBLS
    • 表信息
  • COLUMNS_V2
    • 列信息
  • SDS
    • 存放在 HDFS 中的位置信息、输入输出格式等
  • PARTITION_*
    • 分区表

4. 进阶

1. 内部表、外部表

  • 内部表
    • 由 Hive 管理,文件保存在 HDFS 的 Hive 目录内
    • 先有表,后有数据
    • 删除表后,删除元数据和存储的数据
  • 外部表(加 external 修饰,指定 location
    • 由用户自行管理,文件需要指定保存的 HDFS 路径
    • 先有数据,后建表
    • 删除表后,仅删除元数据,存储数据不会被删除
    • 建表后,自动从指定 HDFS 导入元数据
  • 准备数据,保存到 /opt/person.txt

    1,xiaoming,book-TV-code,beijing:chaoyang
    2,lilei,book-code,nanjing:yuhua
    3,lihua,music-book,heilongjiang:haerbin
    
  • 创建内部表

    --单元格分隔:row format delimited fields terminated by ','
    --集合分隔:collection items terminated by '-'
    --Map分隔:map keys terminated by ':'
    create table person1 (
        id int,
        name string,
        hobby array<string>,
        addr map<string,string>
    )
    row format
    delimited fields terminated by ','
    collection items terminated by '-'
    map keys terminated by ':';
    
    --导入
    load data local inpath '/opt/person.txt' into table person1;
    
  • 创建外部表

    --外部表:多一个 external 修饰;需要指定 location
    create external table person2 (
        id int,
        name string,
        hobby array<string>,
        addr map<string,string>
    )
    row format
    delimited fields terminated by ','
    collection items terminated by '-'
    map keys terminated by ':'
    location '/user/person2';
    
    --导入
    load data local inpath '/opt/person.txt' into table person2;
    
  • 此时查看 Hive 元数据库的 TBLS

    • TBL_TYPE 列中,内部表为 MANAGED_TABLE ,外部表为EXTERNAL_TABLE
  • 查看详细的表信息(可以看到 HDFS 保存路径的变化)

    desc formatted person1
    

2. 分区

  • 创建内部表时,定义分区

    create table person3 (
        id int,
        name string,
        hobby array<string>,
        addr map<string,string>
    )
    partitioned by (p_dt string)
    row format
    delimited fields terminated by ','
    collection items terminated by '-'
    map keys terminated by ':';
    
  • 导入时,指定分区

    load data local inpath '/opt/person.txt' into table person3 partition(p_dt='201907');
    
  • 查看指定表的所有分区

    show partitions person3;
    
  • 添加一个分区

    alter table person3 add partition(p_dt='201908')
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容