目录
汇总了8种,hive加载数据的方法
1,从linux通过load加载
准备数据
[root@node1 hive_data]# ll
总用量 20
-rw-r--r-- 1 root root 137 7月 27 21:10 employee.txt
drwxr-xr-x 4 root root 288 7月 27 10:16 honor_of_kings
-rw-r--r-- 1 root root 70 7月 28 16:30 order_cp.csv
-rw-r--r-- 1 root root 175 7月 27 14:26 orders_data.csv
-rw-r--r-- 1 root root 1167 7月 27 19:12 person.txt
-rw-r--r-- 1 root root 186 7月 27 19:12 story.txt
[root@node1 hive_data]# pwd
/root/hive_data
代码
-- 创建一个普通表
create table tb_order_data(
id int,
dates date,
g_id int,
product string,
sums int,
shuliang int
)
row format delimited fields terminated by ',';
-- 第一种方式 从linx上插入数据 用load方式
load data local inpath '/root/hive_data/orders_data.csv' into table tb_order_data;
2,从linux通过overwrite加载
-- 创建分区表 数据来源自
select * from tb_order_data;
drop table tb_order_data_par;
create table tb_order_data_par(
id int,
g_id int,
product string,
sums int,
shuliang int
)
partitioned by (dt string)
row format delimited fields terminated by ','
stored as textfile ;
-- 插入数据 采用load的方式 从linux上面 '/root/hive_data/orders_data.csv'
load data local inpath '/root/hive_data/order_cp.csv' into table tb_order_data_par partition (dt='2024-07-01');
3,从hdfs上面通过load加载
准备数据 上传到hdfs上面
-- 把本地文件传入到hdfs上面去 /* [root@node1 hive_data]# hdfs dfs -mkdir -p /mydata [root@node1 hive_data]# ll 总用量 16 -rw-r--r-- 1 root root 137 7月 27 21:10 employee.txt drwxr-xr-x 4 root root 288 7月 27 10:16 honor_of_kings -rw-r--r-- 1 root root 175 7月 27 14:26 orders_data.csv -rw-r--r-- 1 root root 1167 7月 27 19:12 person.txt -rw-r--r-- 1 root root 186 7月 27 19:12 story.txt [root@node1 hive_data]# hdfs dfs -put orders_data.csv /mydata/ */
代码:
create table tb_order_data2(
id int,
dates date,
g_id int,
product string,
sums int,
shuliang int
)
row format delimited fields terminated by ',';
-- 从hdfs上面加载
load data inpath '/mydata/orders_data.csv' into table tb_order_data2;
4,在hdfs上面通过overwrite加载
create table tb_order_data2(
id int,
dates date,
g_id int,
product string,
sums int,
shuliang int
)
row format delimited fields terminated by ',';
-- 把本地文件传入到hdfs上面去
/*
[root@node1 hive_data]# hdfs dfs -mkdir -p /mydata
[root@node1 hive_data]# ll
总用量 16
-rw-r--r-- 1 root root 137 7月 27 21:10 employee.txt
drwxr-xr-x 4 root root 288 7月 27 10:16 honor_of_kings
-rw-r--r-- 1 root root 175 7月 27 14:26 orders_data.csv
-rw-r--r-- 1 root root 1167 7月 27 19:12 person.txt
-rw-r--r-- 1 root root 186 7月 27 19:12 story.txt
[root@node1 hive_data]# hdfs dfs -put orders_data.csv /mydata/
*/
-- 从hdfs上面加载
load data inpath '/mydata/orders_data.csv' overwrite into table tb_order_data2;
5,从黑窗口上面加载
黑窗口直接添加数据
1,准备文件
2,把文件放到linux系统下某一个文件内 比如 /root/hive_data/honor_of_kings
3,把文件上传到HIVE默认的存储路径下
hdfs dfs -put 文件路径 /user/hive/warehouse/库名.表名
库名和表名需要与我们sql创建的库,表名一致
6,insert 从表里面加载
准备原始表
-- 创建普通分区表
drop database if exists db_2 cascade ;
create database db_2;
use db_2;
create table tb_order_data(
id int,
dates date,
g_id int,
product string,
sums int,
shuliang int
)
row format delimited fields terminated by ',';
--插入数据
load data local inpath '/root/hive_data/orders_data.csv' into table tb_order_data;
-- 查询数据
select * from tb_order_data;
-- 创建 静态的分区表
create table tb_order_data_paitition(
id int,
g_id int,
product string,
sums int,
shuliang int
)
partitioned by (dt string) -- 指定分区
row format delimited fields terminated by ','
stored as textfile ;
--插入数据 来源我们刚才建立的表
insert into tb_order_data_paitition partition (dt='2024-7-1')
select id,g_id,product,sums,shuliang from tb_order_data where dates='2024-7-1';
select * from tb_order_data_paitition;
7,insert over 从表里面覆盖
-- 创建普通分区表
drop database if exists db_2 cascade ;
create database db_2;
use db_2;
create table tb_order_data(
id int,
dates date,
g_id int,
product string,
sums int,
shuliang int
)
row format delimited fields terminated by ',';
--插入数据
load data local inpath '/root/hive_data/orders_data.csv' into table tb_order_data;
-- 查询数据
select * from tb_order_data;
-- 创建 静态的分区表
create table tb_order_data_paitition(
id int,
g_id int,
product string,
sums int,
shuliang int
)
partitioned by (dt string) -- 指定分区
row format delimited fields terminated by ','
stored as textfile ;
--插入数据 来源我们刚才建立的表
insert overwrite table tb_order_data_paitition partition (dt='2024-7-1')
select id,g_id,product,sums,shuliang from tb_order_data where dates='2024-7-1';
select * from tb_order_data_paitition;
8,建表的时候从hdfs路径里面加载
-- 创建普通表 创建表的时候指定数据
drop table tb_order_data3;
create table tb_order_data3(
id int,
dates date,
g_id int,
product string,
sums int,
shuliang int
)
row format delimited fields terminated by ','
location '/mydata/';
select * from tb_order_data3; --插入数据成功