Say Goodbye Before Goodbye Big Data Storage Review

Create a database. The default storage path of the database on HDFS is:
/user/hive/warehouse/*.db

.Create
database:
create database if not exists school;

.Filter
the databases that display the query:
show databases like 'db_hive*';

.Display
database information:
desc database school;

.Display
database details, extended
desc database extended school;

.Modify
database:
Users can use the ALTER DATABASE command to set key-value pair attribute values ​​for DBPROPERTIES of a certain database to describe the attribute information of this database. Many metadata information of the database cannot be changed, including the database name and the directory location where the database is located.

alter database school set dbproperties(‘createtime’=‘20200407’);

This is an SQL statement used to modify a database property, which sets the creation time property of the database named "school" to "20200407".

.If
the database is not empty, you can use the cascade command to forcefully delete
drop database school cascade;

.
EXTERNAL keyword allows users to create an external table

.
COMMENT: Add comments to tables and columns.

.
PARTITIONED BY creates a partitioned table

.
CLUSTERED BY creates bucket table

.
LOCATION: Specify the storage location of the table on HDFS.

.Generally
create a management table:

create table if not exists student2(
id int, name string
)
row format delimited fields terminated by '\t'
stored as textfile
location '/user/hive/warehouse/student2';


.Create a table based on the query results (the query results will be added to the newly created table): create
table if not exists student3 as select id, name from student2;

.Create
a table based on the existing table structure:
create table if not exists student4 like student2;

.Type
of query table:
desc formatted student2;

.Normally
create an external table:

create external table if not exists ext_stu(
id String,
name String,
age int
)
row format delimited fields terminated by ','
location '/usr/test_data/';

.Modify
the external table student2 to an internal table:

alter table ext_stu set tblproperties(‘EXTERNAL’=‘FALSE’);

('EXTERNAL'='TRUE') and ('EXTERNAL'='FALSE') are fixed writing methods and are case-sensitive!

.Create
partition table syntax:

create table stu_partition(
name String, Phone_NO String
)
partitioned by (class String)
row format delimited fields terminated by '\t';

.Load
data into the partitioned table:

load data local inpath '/usr/test_data/dsj-1.txt' into table school.stu_partition partition(class='dsj-1');

.Multi
-partition joint query:

select * from stu_partition where class='dsj-1'
union
select * from stu_partition where class='dsj-2';

.Create
a single partition:

alter table stu_partition add partition(class=‘dsj-1’);

Create multiple partitions at the same time:

alter table stu_partition add partition(class=‘dsj-2’) partition(class=‘dsj-3’);

.View
partitions:
show partitions stu_partition;

.
删除分区:
alter table stu_partition drop partition(class=‘dsj-1’);
alter table stu_partition drop partition(class=‘dsj-2’),partition(class=‘dsj-3’);

.View
how many partitions there are in the partition table:
show partitions stu_partition;

.Create
a secondary partition table:

create table stu_partition2(
name String, Phone_NO String
)
partitioned by (class String,year int)
row format delimited fields terminated by '\t';

.Example
of loading data from secondary partition:
Load data into secondary partition table:

load data local inpath '/usr/test_data/dsj-1.txt' into table 
school.stu_partition2 partition(class='dsj-1',year='2018'); 

Query partition data:

select * from school.stu_partition2 where class='dsj-1' 
and year='2018';

.Double
named table:
ALTER TABLE table_name RENAME TO new_table_name

.Modify
columns:

  • Add columns: alter table stu2 add columns (pid int);
  • Modify columns: alter table stu2 change pid tab_id int;
  • Replace columns: alter table stu2 replace columns (id String, pname
    String,tab_id String); will replace all columns, use with caution!

Guess you like

Origin blog.csdn.net/m0_64799907/article/details/131338457