大数据从入门到实战——Hive表DML操作

Hive表DML操作

  • 将文件中的数据导入(Load)到 Hive 表中
  • Select 操作
  • 将 select 查询结果插入 hive 表中
  • 将 select 查询结果写入文件

将文件中的数据导入(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;

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

将 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

将 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

你可能感兴趣的:(大数据从入门到实战,hive,big,data,hadoop)