Hive创建表以及导入数据

## 建表
### 内表
```
create table test_user_base(
name string comment 'name value',
workid string comment 'workid value',
age string comment 'age value',
sex string comment 'sex value',
phone string comment 'phone value',
addr string comment 'addr value',
flag string comment 'flag value',
)row format delimited fields terminated by ',';
```


### 外表
create EXTERNAL table test_user_base(
name string comment 'name value',
workid string comment 'workid value',
age string comment 'age value',
sex string comment 'sex value',
phone string comment 'phone value',
addr string comment 'addr value',
flag string comment 'flag value',
)row format delimited fields terminated by ',';
```


> 内部表和外部表不一样,内部表保存的是真实的数据,外部表的数据保存在文件系统上,hive内部仅仅保存数据元路径;删除的时候,内部表会删除元数据和真实数据全部删除,且不可恢复;内部表仅仅删除元数据。


## 导数
### 导入
1. 从本地导入数据
```
load data local inpath '/opt/custorm' into table customer;
```


    2. 从HDFS导入数据(会将HDFS上的文件mv到建表时指定的目录下,而不是copy)
    ```
    load data inpath '/user/root/employees.txt' overwrite into table employees;
    ```
    > into是加上,而overwrite是覆盖。


### 导出
1. 导出到本地
```
insert overwrite local directory '/opt/customer_exp' 
row format delimited fields terminated by ','
select * from employees limit 5;
```
2. 导出到HDFS
```
insert overwrite directory '/user/root/customer_exp' 
row format delimited fields terminated by ','
select * from employees limit 5;
```


tips:
- 建表和加载分区时加上 if not exists 
- 删除表时加上   drop table if exists
- 强制删除数据库:drop database if exists test cascate;





  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值