• Hive内部表,外部表,分区表的创建


    --------创建内部表------------
    默认存储在/user/hive/warehouse下 也可以通过location指定
    删除表时,会删除表数据及元数据

    create table if not exists db_study.student(
    id String ,
    name String
    )
    ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘ ’ #文件内容格式与表的格式相同,否则导入后表的数据是null
    location ‘/user/wei/hive-table/student’;
    #加载本地数据到表中
    load data local inpath ‘/opt/data/student.table’ into table db_study.student ;
    #删除表,会删除创建表时指定的目录,以及目录下的数据文件
    drop table if exists student ;

    ---------创建外部表-------------
    在创建表时必须指定目录位置(LOCATION)
    删除表时,只删除元数据不会删除表数据

    create external table if not exists db_study.teacher(
    id String,
    name String
    )
    row format delimited fields terminated by ‘ ’
    location ‘/user/wei/hive-table/teacher’
    #上传数据文件到localtion目录下,hive会把所有的文件映射到表中
    dfs -put /opt/data/teacher.table /user/wei/hive-table/teacher
    dfs -put /opt/data/teacher2.table /user/wei/hive-table/teacher

    ----------创建分区表-------------
    实际上就是对应一个HDFS文件系统上的独立文件夹,该文件夹下是该分区的所有数据文件
    Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成更多的数据集
    查询时通过where子句中的表达式来选择查询所需要的指定分区,这样的查询效率会高很多

    create table db_study.class_partition(
    id int,
    name string
    )
    partitioned by(month string)
    row format delimited fields terminated by ‘ ’
    location ‘/user/wei/hive-table/class_partition’
    加载表数据
    方法一 load 加载
    load data local inpath ‘/opt/data/class1.table’ into table db_study.class_partition partition(month=‘201809’)
    select * from class_partition where month=201809 ;
    方法二 insert 加载
    insert overwrite table NAME partition(month=‘201707’) select id, name from NAME;
    方法三 可通过手动上传文件到分区目录,进行加载
    hdfs dfs -mkdir /user/hive/warehouse/tb_partition/month=201710
    hdfs dfs -put nameinfo.txt /user/hive/warehouse/tb_partition/month=201710
    虽然方法三手动上传文件到分区目录,但是查询表的时候是查询不到数据的,需要更新元数据信息。
    更新源数据的两种方法
    方法一:msck repair table 表名
    方法二:alter table tb_partition add partition(month=‘201708’);
    查询表数据
    select * from default.emp_partition where month = ‘201807’ ;
    show partitions tb_partition;

    -------从已经存在的表选出字段内容组成新的表,分表抽取-------
    create table IF NOT EXISTS test_db.user_tmp
    ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘ ’
    location ‘/user/wei/oozie/datas/table/user_tmp’
    as select username from test_db.hive_user

    -------复制表结构-------
    create table IF NOT EXITSTS default.teacher03
    like default.teacher

    -------导出Hive 表的数据---------------
    1)导出查询数据到本地目录下
    insert overwrite local directory ‘/opt/datas/user2.txt’
    row format delimited fields terminated by ‘/t’ collection items terminated by ‘ ’
    select * from db_wei.user_pt ;
    2)导出查询数据到本地目录下
    bin/hive -e “select * from db_wei.user;” > /opt/datas/user
    3)导出查询数据到HDFS
    insert overwrite directory ‘/user/root’
    row format delimited fields terminated by ‘/t’ collection items terminated by ‘ ’
    select * from db_wei.user_pt ;

    -------数据库的操作-------
    create database if not exists
    show databases;
    show databases like ‘db_hive’;
    use db_hive;
    desc database db_hive ;
    desc database extended db_hive ;
    drop database db_hive;
    drop database db_hive cascade ; #级联删除,删除数据库,删除下面的表
    drop database if exists db_hive;

    -------表的操作-------
    #清除一个表的数据
    truncate table tablename ;
    #对表进行改名
    alter table tablename rename to tablename ;
    #删除表
    drop table if exists tablename;
    #表详情
    desc FORMATTED tablename

  • 相关阅读:
    Delphi编译器属性(特别修饰符Ref,Unsafe,Volatile,Weak)
    .netcore dapr微服务入门
    Net WebApi一个简单的Token验证
    发布订阅和观察者模式
    NET Core创建Windows服务
    jquery.barrager.js弹幕实现
    跨平台中的RN、Flutter,服务端GraphQL、Serverless,Node和Electron
    分布式系统与高并发高可用
    11 个 Linux 命令
    接口幂等性
  • 原文地址:https://www.cnblogs.com/Thomas-blog/p/9728176.html
Copyright © 2020-2023  润新知