当前位置: 首页 > news >正文

【大数据】Hive 中的批量数据导入

Hive 中的批量数据导入

在博客【大数据】Hive 表中插入多条数据 中,我简单介绍了几种向 Hive 表中插入数据的方法。然而更多的时候,我们并不是一条数据一条数据的插入,而是以批量导入的方式。在本文中,我将较为全面地介绍几种向 Hive 中批量导入数据的方法。

1.从本地文件系统加载(load)数据

load data [local] inpath '路径' [overwrite] into table 表名 [partition (分区字段=,)];
  • overwrite:表示覆盖表中已有数据,否则表示追加。
  • 此种加载方式是数据的复制。

(1)创建一张表。

hive (default)> create table student(id string, name string) row format delimited fields terminated by '\t';

(2)加载本地文件到 Hive。

hive (default)> load data local inpath '/opt/module/datas/student.txt' into table default.student;

2.从 HDFS 文件系统加载(load)数据

从 HDFS 文件系统向表中加载数据,其实就是一个移动文件的操作,需要提前将数据上传到 HDFS 文件系统。

(1)上传文件到 HDFS(Linux 本地 /opt/module/datas/student.txt 文件传到 /user/victor/hive 目录)。

hive (default)> dfs -put /opt/module/datas/student.txt /user/victor/hive;

(2)从 HDFS 文件系统向表中加载数据。

hive (default)> load data inpath '/user/victor/hive/student.txt' into table default.student;

3.通过 as select 向表中插入数据

hive (default)> create table if not exists student3 as select id, name from student;

4.通过 insert into 向表中插入数据

insert into table test [partition(partcol1=val1, partcol2=val2 ...)] select id,name from student;
  • insert into:以追加数据的方式插入到表或分区,原有数据不会删除。
insert overwrite table test [partition(partcol1=val1, partcol2=val2 ...)] select id,name from student;
  • insert overwrite:覆盖表中已存在的数据。

(1)创建一张分区表。

hive (default)> create table student(id string, name string) partitioned by (month string) row format delimited fields terminated by '\t';

(2)基本插入数据。

hive (default)> insert into table student partition(month='201801') values('1004','wangwu');

(3)基本模式插入(根据单张表查询结果)。

hive (default)> insert overwrite table student partition(month='201802') select id, name from student where month='201801';

(4)多插入模式(只需要扫描一遍源表就可以生成多个不相交的输出)。

hive (default)> from studentinsert overwrite table student partition(month='201803')select id, name where month='201801'insert overwrite table student partition(month='201804')select id, name where month='201801';

5.通过 location 的方式

直接将数据文件上传到 location 指定的 HDFS 的目录下;

(1)创建表,并指定在 HDFS 上的位置。

hive (default)> create external table student(id int, name string)row format delimited fields terminated by '\t'location '/user/hive/warehouse/student';

(2)上传数据到 HDFS 上。

hive (default)> dfs -mkdir -p /user/hive/warehouse/student;
hive (default)> dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student;

(3)查询数据。

select * from student;
http://www.lryc.cn/news/136723.html

相关文章:

  • 【Modbus通信实验三】数据切片问题
  • 记录《现有docker中安装spark3.4.1》
  • 【3ds Max】练习——制作衣柜
  • Spring-MVC的数据响应-19
  • (三)行为模式:5、中介者模式(Mediator Pattern)(C++示例)
  • 期权是什么?期权的优缺点是什么?
  • 目标检测任务数据集的数据增强中,图像垂直翻转和xml标注文件坐标调整
  • html5提供的FileReader是一种异步文件读取文件中的数据
  • Linux学习记录——이십오 多线程(2)
  • 单片机(二)使用位移 让灯亮
  • 探究代理服务器在网络安全与爬虫中的双重作用
  • JavaWeb-学习目录
  • C语言题目 - 调用qsort函数对数组进行排序
  • Matplotlib学习笔记
  • 对比flink cdc和canal获取mysql binlog优缺点
  • SpringCloud学习笔记(三)_服务提供者集群与服务发现Discovery
  • .NET 8 Preview 7 中的 ASP.NET Core 更新
  • Ajax+Vue+ElementUI
  • python读取pdf、doc、docx、ppt、pptx文件内容
  • 鸿鹄工程项目管理系统 Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统 em
  • maven无法从阿里云中央仓库下载jar包的解决方法
  • Nevron Open Vision for .NET Crack
  • 玩转单元测试之cppmockfree
  • Open3D点云数据处理(二十一):最小二乘多项式拟合
  • 智能手表:华米稳、华为猛
  • 【日常积累】Linux中vi/vim的使用
  • 基于React实现日历组件详细教程
  • Web安全测试(二):HTTP状态码、响应和url详解
  • 什么是算法评价指标
  • 什么是软件压力测试?软件压力测试工具和流程有哪些?