hive表中导入数据 多种方法详细说明

文章中对hive表中导入数据 方法目录

方式一:通过load方式加载数据

方式二:直接向分区表中插入数据

[方式三:查询语句中创建表并加载数据(as select)](#方式三:查询语句中创建表并加载数据(as select))

方式四:创建表时通过location指定加载数据路径

[1. 创建表,并指定在hdfs上的位置](#1. 创建表,并指定在hdfs上的位置)

[2. 上传数据到hdfs上](#2. 上传数据到hdfs上)

[3. 查询数据](#3. 查询数据)

[方式五:export导出 与 import导入 hive表数据(内部非分区表操作)](#方式五:export导出 与 import导入 hive表数据(内部非分区表操作))


准备数据

|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| -- 创建 db_myhive_5 create database db_myhive_5; use db_myhive_5; -- 创建表 create table tb_score( s_id string, c_id string, score int ) partitioned by (month string) row format delimited fields terminated by '\t'; |

方式一:通过load方式加载数据

|------------------------------------------------------------------------------------------------------------------------------------------------------------|
| load data local inpath '/export/data/hive_data/score.txt' overwrite into table tb_score partition ( month = '202006'); |

方式二:直接向分区表中插入数据

通过insert into方式加载数据

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| create table score3 like tb_score**;** insert into table score3 partition ( month = '202007') values ( '001', '002', '100'); |

通过查询方式加载数据

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| create table score4 like score**;** insert overwrite table score4 partition ( month = '202006') select s_id**,** c_id**,** s_score from tb_score**;** |

例子

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 需求1: 创建新库 myhive6, 切换库 myhive6 需求2: 创建t_score_1(s_id, c_id, score) 按月指定分区 month, 指定字段分隔符为 '\t' 需求3: 通过 load data 方式加载文件中数据 需求4: 创建表 t_score_2 依据 表 t_score_1的结构 需求5: 通过insert into 添加一行数据 需求6: 创建表 t_score_3 依据 表 t_score_1的结构 需求7: 通过 select 添加n条记录 |

实现

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| -- 需求1: 创建新库 myhive6, 切换库 myhive6 create database myhive6; use myhive6; -- 需求2: 创建t_score_1(s_id, c_id, score) 按月指定分区 month, 指定字段分隔符为 '\t' create table t_score_1( s_id string, c_id string, score int ) partitioned by (month string) row format delimited fields terminated by '\t'; -- 需求3: 通过 load data 方式加载文件中数据 load data local inpath '/export/data/hive_data/f_score.csv' overwrite into table t_score_1 partition (month='202101'); -- 需求4: 创建表 t_score_2 依据 表 t_score_1的结构 create table t_score_2 like t_score_1; -- 需求5: 通过insert into 添加一行数据 insert into t_score_2 partition(month='202002') values('01', '02', 66); select * from t_score_2; -- 需求6: 创建表 t_score_3 依据 表 t_score_1的结构 create table t_score_3 like t_score_1; -- 需求7: 通过 select 添加n条记录 insert overwrite table t_score_3 partition(month='202003') select s_id, c_id, score from t_score_1; select * from t_score_3; |

方式三:查询语句中创建表并加载数据(as select)

将查询的结果保存到一张表当中去

|--------------------------------------------------------------------------|
| create table score5 as select * from score**;** |

方式四:创建表时通过location指定加载数据路径
1. 创建表,并指定在hdfs上的位置

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| create external table score6 ( s_id string**,** c_id string**,** s_score int ) row format delimited fields terminated by '\t' location '/myscore6'; |

2. 上传数据到hdfs上

|---------------------------------------------------------------------------------------------------|
| hdfs dfs - mkdir - p / myscore6 hdfs dfs - put score**.** txt / myscore6**;** |

3. 查询数据

|----------------------------------------|
| select * from score6**;** |

例子

|------------------------------------------------------------------------------------------------------------------------------------------|
| 1 创建表 t_score_6(s_id, c_id, score), 指定分隔符为'\t', 指定保存位置为 '/hivedatas/t_score_6'; 2 将分数信息文件 上传到 hdfs的目录下 '/hivedatas/t_score_6' 3 查看表中的数据 |

​​​​​​​实现

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| -- 1 创建表 t_score_6(s_id, c_id, score), 指定分隔符为'\t', 指定保存位置为 '/hivedatas/t_score_6'; create table t_score_6( s_id string, c_id string, score int ) row format delimited fields terminated by '\t' location '/hivedatas/t_score_6'; -- 2 将分数信息文件 上传到 hdfs的目录下 '/hivedatas/t_score_6' -- hdfs dfs -put 文件 /hivedatas/t_score_6 -- 3 查看表中的数据 select * from t_score_6; |

方式五:expo rt导出 import 导入 hive表数据 内部 非分区 操作

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| create table teacher2 like teacher**;** export table teacher to '/export/teacher'; import table teacher2 from '/export/teacher' |

注意: import 导入时结尾不要增加 分号;

相关推荐
weixin_307779136 小时前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
王小王-12311 小时前
基于Hadoop的公共自行车数据分布式存储和计算平台的设计与实现
大数据·hive·hadoop·分布式·hadoop公共自行车·共享单车大数据分析·hadoop共享单车
王小王-12314 小时前
基于Hadoop的大规模文本词频统计分析系统设计与实现
hadoop·mapreduce·hadoop词频统计·hadoop文本统计·mapreduce词频统计
陈敬雷-充电了么-CEO兼CTO16 小时前
推荐算法系统系列>推荐数据仓库集市的ETL数据处理
大数据·数据库·数据仓库·数据挖掘·数据分析·etl·推荐算法
桂成林20 小时前
Hive UDF 开发实战:MD5 哈希函数实现
hive·hadoop·哈希算法
isNotNullX20 小时前
什么是数据分析?常见方法全解析
大数据·数据库·数据仓库·人工智能·数据分析
王小王-1231 天前
基于Hadoop的京东厨具商品数据分析及商品价格预测系统的设计与实现
hadoop·数据分析·京东厨具·厨具分析·商品分析
谷新龙0012 天前
大数据环境搭建指南:基于 Docker 构建 Hadoop、Hive、HBase 等服务
大数据·hadoop·docker
百度Geek说2 天前
搜索数据建设系列之数据架构重构
数据仓库·重构·架构·spark·dubbo
爱吃面的猫2 天前
大数据Hadoop之——Hbase下载安装部署
大数据·hadoop·hbase