赞
踩
目录
目录
(2)Create/Drop/Truncate 表
#********* Begin *********#
echo "CREATE DATABASE IF NOT EXISTS test1
LOCATION '/hive/test1'
WITH DBPROPERTIES('creator'='John','date'='2019-02-25');
ALTER DATABASE test1 SET DBPROPERTIES('creator'='Marry');
DROP DATABASE test1;
"
#********* End *********#
#********* Begin *********#
echo "
CREATE DATABASE IF NOT EXISTS test2
LOCATION '/hive/test2'
WITH DBPROPERTIES('creator'='John','date'='2022-04-20');
CREATE TABLE IF NOT EXISTS test2.student(
Sno INT COMMENT 'student sno',
name STRING COMMENT 'student name',
age INT COMMENT 'student age',
sex STRING COMMENT 'student sex',
score STRUCT <Chinese:FLOAT,Math:FLOAT,English:FLOAT> COMMENT 'student score');
CREATE TABLE IF NOT EXISTS student_info
LIKE student;
DROP TABLE IF EXISTS student;
"
#********* End *********#
#********* Begin *********#
echo "
CREATE DATABASE IF NOT EXISTS test3
LOCATION '/hive/test3'
WITH DBPROPERTIES('creator'='John','date'='2022-04-20');
CREATE TABLE IF NOT EXISTS test3.student(
Sno INT COMMENT 'student sno',
name STRING COMMENT 'student name',
age INT COMMENT 'student age',
sex STRING COMMENT 'student sex',
score STRUCT <Chinese:FLOAT,Math:FLOAT,English:FLOAT> COMMENT 'student score');
ALTER TABLE student RENAME TO student_info;
ALTER TABLE student_info CHANGE age student_age int;
ALTER TABLE student_info ADD COLUMNS (birthday STRING COMMENT 'student birthday');
"
#********* End *********#
#********* Begin *********#
echo "
CREATE DATABASE IF NOT EXISTS test4
LOCATION '/hive/test4'
WITH DBPROPERTIES('creator'='John','date'='2022-04-20');
CREATE TABLE IF NOT EXISTS test4.student(
Sno INT COMMENT 'student sno',
name STRING COMMENT 'student name',
age INT COMMENT 'student age',
sex STRING COMMENT 'student sex',
score STRUCT <Chinese:FLOAT,Math:FLOAT,English:FLOAT> COMMENT 'student score')
PARTITIONED BY (stu_year STRING,subject STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ',' ;
ALTER TABLE student ADD PARTITION (stu_year='2018',subject='Chinese') LOCATION '/hive/test4/student/Chinese/2018'
PARTITION (stu_year='2018',subject='Math') LOCATION '/hive/test4/student/Math/2018';
ALTER TABLE student PARTITION (stu_year='2018',subject='Math') RENAME TO PARTITION (stu_year='2018',subject='English');
ALTER TABLE student DROP IF EXISTS PARTITION (stu_year='2018',subject='Chinese');
"
#********* End *********#
#********* Begin *********#
echo "
CREATE DATABASE IF NOT EXISTS test1;
CREATE TABLE IF NOT EXISTS test1.student(
Sno INT ,
name STRING ,
age INT ,
sex STRING ,
score STRUCT <Chinese:FLOAT,Math:FLOAT,English:FLOAT>
) ;
CREATE VIEW student_view(
Sno,
name_length,
age,
sex
)
AS SELECT Sno,length(name),age,sex FROM student;
alter view student_view rename to student_info_views;
drop view if exists student_info_views;
"
#********* End *********#
#********* Begin *********#
echo "
CREATE DATABASE IF NOT EXISTS test2;
CREATE TABLE IF NOT EXISTS test2.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;
create index student_index on table student(Sno)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with deferred rebuild
IN TABLE student_index_table;
drop index if exists student_index on student;
"
#********* End *********#
/********* 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;
--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
--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
--使用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
看下面这位博主的很详细(按照博主的照片一步步来)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。