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;





猜你喜欢

转载自blog.csdn.net/tengxing007/article/details/80912837