文章目录

  • hive上传加载数据
  • 一、创建数据文件
  • 1,在linux本地写入employess 员工信息表的数据
  • 2,在linux本地写入 student_exam 学生成绩表的数据
  • 3,在linux本地写入 sales 商品销售表的数据
  • 4,在linux本地写入 students 学生数据表的数据
  • 5,在linux本地写入 teacher 教师数据表的数据
  • 二,创建目录和上传数据文件
  • 1,在hdfs上创建文件 /hive_data/employess
  • 2,在hdfs上创建目录 /hive_data/student_exam 然后将上面创建的student_exam.txt 文件数据上传到这个目录下
  • 3,在hdfs上创建目录 /hive_data/sales 然后将上面创建的sales.txt 文件数据上传到这个目录下
  • 4,在hdfs上创建目录 /hive_data/teacher 将上面创建的 teacher.txt 文件数据上传到这个目录下
  • 5,在hdfs上创建目录 /hive_data/students 将![在这里插入图片描述](https://s2.51cto.com/images/blog/202311/23123001_655ed549659a156385.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
  • 三、创建信息表
  • 1,创建员工信息表
  • 2,创建学生成绩表
  • 3,创建商品销售表
  • 4,创建学生名单表
  • 5,创建教师名单表


hive上传加载数据

1,使用load data来加载文本、csv、josn等格式数据

load data: 表示加载数据

local:表示从本地加载,否则从hdfs加载,local一般是在服务器命令行模式使用,其他地方比如dbeaver不能用

inpath:加载文件的路径

overwrite:表示覆盖原表已有数据,否则表示追加

into table:加载到哪个表

partition:加载到指定分区

例如:

load data inpath ‘/student.txt’ overwrite into table student; 将student.txt里面的数据插入到 表studnt学生表里面,overwrite表示覆盖。

2,通过查询方式加载数据

insert overwrite table student3 select * from student;

查询student学生表的数据然后覆盖到student3数据表上,overwrite表示是覆盖,如果使用into就是追加。

上传本地服务到hive命令 hive上传数据_hive

一、创建数据文件

1,在linux本地写入employess 员工信息表的数据

上传本地服务到hive命令 hive上传数据_上传本地服务到hive命令_02

Lilith Hardy,30,6000,50,Finance Department
Byron Green,36,5000,25,Personnel Department
Yvette Ward,21,4500,15.5, Arlen Esther,28,8000,20,Finance Department
Rupert Gold,39,10000,66,R&D Department
Deborah Madge,41,6500,0,R&D Department
Tim Springhall,22,6000,36.5,R&D Department
Olga Belloc,36,5600,10,Sales Department
Bruno Wallis,43,6700,0,Personnel Department
Flora Dan,27,4000,35,Sales Department

2,在linux本地写入 student_exam 学生成绩表的数据

上传本地服务到hive命令 hive上传数据_数据_03

Mandy,Peking University-Wuhan University-Nankai University,Chemistry:90-Physics:98- Biology:83,126-135-140
Jerome,Tsinghua University-Fudan University-Nanjing University,History:89-Politics:92- Geography:87,130-116-128
Delia,Nanjing University-Wuhan University-Nankai University,Chemistry:87-Physics:95- Biology:73,102-123-112
Ben,Tianjin Universit-Peking University-Fudan University,Chemistry:92-Physics:88- Biology:79,98-142-106
Carter,Tsinghua University-Fudan University-Tianjin Universit,History:90-Politics:91- Geography:80,109-111-140
Vivian,Fudan University-Nanjing University-Nankai University,Chemistry:83-Physics:86- Biology:90,120-140-132

3,在linux本地写入 sales 商品销售表的数据

上传本地服务到hive命令 hive上传数据_hadoop_04

SiChuan,ChengDu,34631
SiChuan,MianYang,54516
SiChuan,LeShan,41288
HuBei,WuHan,16564
HuBei,YiBin,87007
HeBei,ShiJiaZhuang,73612
HeBei,BaoDing,68192
HeBei,TangShan,29889

4,在linux本地写入 students 学生数据表的数据

上传本地服务到hive命令 hive上传数据_数据_05

301,student1
302,student2
305,student3
303,student4
302,student5
303,student6
301,student7
303,student8
302,student9
301,student10

5,在linux本地写入 teacher 教师数据表的数据

上传本地服务到hive命令 hive上传数据_hive_06

301,teacher01
302,teacher02
303,teacher03
304,teacher04

二,创建目录和上传数据文件

1,在hdfs上创建文件 /hive_data/employess

输入命令: hdfs dfs -mkdir -p /hive_data/employess

上传本地服务到hive命令 hive上传数据_上传本地服务到hive命令_07


然后再webUI界面上查看,有了这个目录

上传本地服务到hive命令 hive上传数据_数据_08


然后将上面上面写入的员工数据表的文件上传到 /hive_data/employess 目录下

输入命令: hdfs dfs -put employess.txt /hive_data/employess

上传本地服务到hive命令 hive上传数据_hadoop_09


webUI界面查看,已经有了这个文件

上传本地服务到hive命令 hive上传数据_hadoop_10

2,在hdfs上创建目录 /hive_data/student_exam 然后将上面创建的student_exam.txt 文件数据上传到这个目录下

上传本地服务到hive命令 hive上传数据_hive_11


webUI界面下查看

上传本地服务到hive命令 hive上传数据_hive_12

3,在hdfs上创建目录 /hive_data/sales 然后将上面创建的sales.txt 文件数据上传到这个目录下

上传本地服务到hive命令 hive上传数据_hive_13


在webUI界面下查看

上传本地服务到hive命令 hive上传数据_hadoop_14

4,在hdfs上创建目录 /hive_data/teacher 将上面创建的 teacher.txt 文件数据上传到这个目录下

上传本地服务到hive命令 hive上传数据_数据_15


webUI界面下查看

上传本地服务到hive命令 hive上传数据_上传本地服务到hive命令_16

5,在hdfs上创建目录 /hive_data/students 将

上传本地服务到hive命令 hive上传数据_数据_17

上面创建的students.txt 文件数据上传到这个目录下

上传本地服务到hive命令 hive上传数据_数据_18


webUI界面下查看

三、创建信息表

1,创建员工信息表

在dbaver上创建员工信息表,上面是创建表的基础命令,下面那个 localtion “/hive_data/employess” 这个是上面员工信息表数据存放目录的位置,这个路径不写对,那表里就没有数据

上传本地服务到hive命令 hive上传数据_hadoop_19


创建员工信息表的sql语句如下:

CREATE EXTERNAL TABLE IF NOT EXISTS  --上面是创建表的基本的格式
hive_database.employess_table(
staff_name STRING, staff_age INT, staff_salary FLOAT, late_deduction FLOAT, staff_dept STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS textfile
LOCATION '/hive_data/employess'  --lacation 这个是表的数据的位置,这个位置是我门创建的/hive_data/emplyess目录下的emplyess.txt 员工信息表数据

2,创建学生成绩表

在dbaver上创建学生成绩表 student_exam_table,学生成绩表的数据文件路径是"/hive_data/student_exam"

上传本地服务到hive命令 hive上传数据_上传本地服务到hive命令_20


创建学生成绩表的sql语句如下:

CREATE EXTERNAL TABLE IF NOT EXISTS
hive_database.student_exam_table(
student_name STRING, intent_university ARRAY<STRING>, humanities_or_sciences MAP<STRING, FLOAT>, comprehensive STRUCT<chinese:FLOAT,maths:FLOAT,english:FLOAT>)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '-' MAP KEYS TERMINATED BY ':' LINES TERMINATED BY '\n' STORED AS textfile
LOCATION '/hive_data/student_exam';

3,创建商品销售表

在dbaver上创建商品销售表 sales_table,学生成绩表的数据文件路径是"/hive_data/sales"

上传本地服务到hive命令 hive上传数据_上传本地服务到hive命令_21


创建商品销售表sql语句如下:

CREATE EXTERNAL TABLE IF NOT EXISTS
hive_database.sales_table(
province STRING, city STRING, sales_amount FLOAT
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS textfile
LOCATION '/hive_data/sales';

4,创建学生名单表

在dbaver上创建学生名单表表 students_table,学生名单表的数据文件路径是"/hive_data/students"

上传本地服务到hive命令 hive上传数据_hive_22


创建学生名单表sql语句如下:

CREATE EXTERNAL TABLE IF NOT EXISTS
hive_database.students_table(
class STRING, student_name STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS textfile
location '/hive_data/students'

5,创建教师名单表

在dbaver上创建教师名单表表 teacher_table,教师名单表的数据文件路径是"/hive_data/teacher"

上传本地服务到hive命令 hive上传数据_上传本地服务到hive命令_23


创建教师名单表sql语句如下:

CREATE EXTERNAL TABLE IF NOT EXISTS
hive_database.teacher_table(
class STRING, teacher_name STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS textfile
LOCATION '/hive_data/teacher';