hive

一、建库语句

--建库
create database db_test;

二、建表语句

--建表语句和普通sql略有不同

create table db_tabel(id int,name string...)
row format delimited
fields terminated by ',';  --指定分隔符

--如果不加后面两行,这样建表会默认分隔符为:^A

三、外部表(EXTERNAL_TABLE)和内部表(MANAGED_TABLE)

  •         内部表和外部表的区别:

1)创建表时:创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径, 不对数据的位置做任何改变。(内部表数据移动到:/apps/hive/warehouse/db_order.db/t_order,但是外部表数据还是在hdfs中的某个位置上)

2)删除表时:在删除表的时候,内部表的元数据和数据会被一起删除, 而外部表只删除元数据,不删除数据。这样外部表相对来说更加安全些,数据组织也更加灵活,方便共享源数据。 

  • 内部表的创建
create table inner_table(id int,name string...)
row format delimited
fields terminated by ','
  • 外部表的创建
create external tabl exter_table(id int,name string...)
row format delimited 
fields terminated by ','  --指定分隔符
location '/hive/db/'

四、分区表

  • 创建分区表
--创建分区表
create table partion_table(id int,name string)
partitioned by(day string,country string)
row format delimited
fields terminated by ','

--注意:分区字段不能是表定义中的已存在字段

五、CTAS建表语句

通过已经存在的表来创建新的表

--通过已经存在的表来创建新的表(没有数据,只有表结构)

    create table t_user_2 like t_user;

    --新建的t_user_2表结构定义与源表t_user一致,但是没有数据


--通过已经存在的表来创建新的表(不光有表结构,还有数据)
     
    create table t_access_user 
    as
    select ip,url from t_access;

    --t_access_user会根据select查询的字段来建表,同时将查询的结果插入新表中

六、数据导入和导出

 数据文件导入到hive表中

三种方式:

一、通过hdfs命令直接将文件移动到hdfs中hive表所在的目录
二、将本地文件导入到表中(复制)
    load data local inpath '/所在目录' into table 表名[(分区字段)] ;
三、将hdfs目录下的文件导入到表中(移动)
    load data inpath '/目录' into table 表名;
  • hive表中数据导入到指定路径
    1、将hive表中的数据导入HDFS的文件
    insert overwrite directory '/root/access-data'
    row format delimited fields terminated by ','
    select * from t_access;
    
    
    2、将hive表中的数据导入本地磁盘文件(在linux上面,而不是自己的操作系统上)
    insert overwrite local directory '/root/access-data'
    row format delimited fields terminated by ','
    select * from t_access limit 100000;
    

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值