大数据从入门到实战——Hive表DDL操作(一)

Hive表DDL操作

  • Create/Alter/Drop 数据库
  • Create/Drop/Truncate 表
  • Alter 表/列
  • 表分区

Create/Alter/Drop 数据库

#********* 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 *********#

Create/Drop/Truncate 表

#********* Begin *********#
echo "
	CREATE DATABASE IF NOT EXISTS test2
	LOCATION '/hive/test2'
	WITH DBPROPERTIES ('creator'='Floret','date'='2020-11-16');
	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 COMMENT 'student score')
	COMMENT 'students information table'
	TBLPROPERTIES ('creator'='Floret','date'='2020-11-16');
	CREATE TABLE IF NOT EXISTS student_info
	LIKE student;
	DROP TABLE IF EXISTS student;
"
#********* End *********#

Alter 表/列

#********* Begin *********#
echo "
	CREATE DATABASE IF NOT EXISTS test3
	LOCATION '/hive/test3'
	WITH DBPROPERTIES ('creator'='Floret','date'='2020-11-16');
	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 COMMENT 'student score')
	COMMENT 'students information table'
	TBLPROPERTIES ('creator'='Floret','date'='2020-11-16');
	ALTER TABLE student RENAME TO student_info;
	ALTER TABLE student_info CHANGE age student_age INT COMMENT 'student age';
	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'='Floret','date'='2020-11-16');
	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 COMMENT 'student score')
	COMMENT 'students information table'
	PARTITIONED BY (stu_year STRING,subject STRING)
	ROW FORMAT DELIMITED
	FIELDS TERMINATED BY '\t'
	COLLECTION ITEMS TERMINATED BY ','
	TBLPROPERTIES('creator'='Floret','date'='2020-11-16');
	ALTER TABLE student ADD PARTITION (stu_year='2018',subject='Chinese') LOCATION '/hive/test4/student/2018/Chinese'
	PARTITION (stu_year='2018',subject='Math') LOCATION '/hive/test4/student/2018/Math';
	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 *********#

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