当前位置:   article > 正文

大数据从入门到实战——Hive表DML操作_nav 第1关:将文件中的数据导入(load)到 hive 表中

nav 第1关:将文件中的数据导入(load)到 hive 表中

将文件中的数据导入(Load)到 Hive 表中

/********* Begin *********/
CREATE DATABASE IF NOT EXISTS test1
LOCATION '/hive/test1';
USE test1;
CREATE TABLE IF NOT EXISTS test1.student(
    Sno INT,
    name STRING,
    age INT,
    sex STRING,
    score STRUCT <Chinese:FLOAT, Math:FLOAT, English:FLOAT>
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '-';
load data local inpath '/home/student.txt' overwrite into table student;
/********* End *********/
select * from student;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Select 操作

--Begin
USE test2;
select * from student;
select * from student where age > 17 and sex = "female";
select * from student where score.Chinese > 90;
select * from student limit 3;
select * from student sort by age desc limit 2;

--End
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

将 select 查询结果插入 hive 表中

--Begin
--使用test3数据库
use test3;
--复制student表两份,分别名为:student2、student3
CREATE TABLE IF NOT EXISTS student2
LIKE student;

CREATE TABLE IF NOT EXISTS student3
LIKE student;

--以覆盖插入的方式把student表中前两条数据插入到student2中
insert overwrite table student2
select * from student limit 2;

--评测代码,勿删
select * from student2;

--以追加插入的方式把student表中前两条数据插入到student2中
insert into table student2
select * from student limit 2;

--评测代码,勿删
select * from student2;

--以覆盖插入的方式把student表中年龄大于17岁的数据插入到student2、student3中
from student ii
insert overwrite table student2
select * where ii.age > 17
insert overwrite table student3
select * where ii.age > 17;


--评测代码,勿删
select * from student2;
select * from student3;

--以追加插入的方式把student表中的男生数据插入到student2,以覆盖插入的方式把女生数据插入到student3中
from student ii
insert into table student2
select * where ii.sex = 'male'
insert overwrite table student3
select * where ii.sex = 'female';

--评测代码,勿删
select * from student2;
select * from student3;
--End
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

将 select 查询结果写入文件

--使用test4数据库
use test4;
--Begin
insert overwrite local directory '/home/test4'
select * from student limit 2;

FROM student
insert overwrite local directory '/home/test4_1'
select * where sex = 'male'
insert overwrite local directory '/home/test4_2'
select * where sex='female'
--End
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/436929
推荐阅读
相关标签
  

闽ICP备14008679号