100% found this document useful (2 votes)
554 views4 pages

Apache Hive DDL DML, Queries

This document discusses various Hive DDL commands including: creating and dropping databases; showing databases; creating, altering, and dropping tables; selecting data from tables; and loading data into tables. Some key commands are CREATE DATABASE, SHOW DATABASES, DROP DATABASE, CREATE TABLE, ALTER TABLE, DROP TABLE, SELECT, and LOAD DATA.

Uploaded by

Rahul Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
554 views4 pages

Apache Hive DDL DML, Queries

This document discusses various Hive DDL commands including: creating and dropping databases; showing databases; creating, altering, and dropping tables; selecting data from tables; and loading data into tables. Some key commands are CREATE DATABASE, SHOW DATABASES, DROP DATABASE, CREATE TABLE, ALTER TABLE, DROP TABLE, SELECT, and LOAD DATA.

Uploaded by

Rahul Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to Hive DDL Commands

There are several types of Hive DDL commands, we commonly use. such as:
1. Create Database Statement
2. Hive Show Database
3. Drop database
4. Creating Hive Tables
5. Browse the table
6. Altering and Dropping Tables
7. Hive Select Data from Table
8. Hive Load Data
Let’s discuss each Hive DDL commands in detail:
a. Create Database Statement
Basically, in Apache Hive, the database is a namespace or a collection of tables.
 Syntax-

CREATE DATABASE [IF NOT EXISTS] db_name;


 For Example-
hive> CREATE DATABASE IF NOT EXISTS TEST;
OK
Time taken: 9.253 seconds
hive>

b. Hive Show Database


However, with this Hive DDL commands, we generally display the databases
present in Hive. Moreover, to see all available databases in Hive below is the
syntax:
 Syntax-
hive> DROP DATABASE IF EXISTS db1;
Or,
SHOW DATABASES;
 For Example-
hive> SHOW DATABASES;
OK
default
test
Time taken: 2.346 seconds, Fetched: 2 row(s)
hive>
c. Drop database
The Syntax & Example of Drop database – Hive DDL commands are:
 Syntax-
hive> DROP DATABASE db1;
Or,
DROP DATABASE IF EXISTS db2;
 For Example-
hive> DROP DATABASE IF EXISTS TEST;
OK
Time taken: 1.165 seconds
hive>
Read about Apache Hive Data Types in detail
d. Creating Hive Tables
However, with two columns “Create a table” called Sonoo. Although, the first
being an integer and the other a string.
1. hive> CREATE TABLE T1(id INT, name STRING);
2. hive> CREATE TABLE HIVE_TABLE (id INT, name STRING) PARTITIONED BY (ds STRING);
3. Or,
4. CREATE [TEMPORARY ] [EXTERNAL] TABLE [IF NOT EXISTS] db_name table_name;

For Example-

1. hive> CREATE TABLE IF NOT EXISTS test(col1 char(10),col2 char(20));

OK Time taken: 1.1 seconds hive>

e. Browse the table


The Syntax of Browse the table – Hive DDL commands are:
 Syntax-
hive> Show tables;
f. Altering and Dropping Tables
The Syntax of Altering and Dropping Tables – Hive DDL commands are:
 Syntax-
1. hive> ALTER TABLE T1 RENAME TO TAB1;
2. hive> ALTER TABLE T2 ADD COLUMNS (id INT);
3. hive> ALTER TABLE HIVE_TABLE ADD COLUMNS (eid INT COMMENT 'a comment');
i. Hive Drop Table
Generally, with these Hive DDL commands, we remove the table data and their
metadata. Moreover, to drop tables in Hive below is the syntax:
 Syntax-
DROP TABLE [IF EXISTS] table_name;
 For Example-
hive> DROP TABLE test;
OK
Time taken: 1.165 seconds
hive>
ii. Hive Alter Table
Basically, with these Hive DDL commands, we can alter table to modify attributes
of Hive table.
 Syntax-
ALTER TABLE table_name ADD COLUMNS (column1, column2) ;
ALTER TABLE table_name RENAME TO table_new_name;
 For Example-
1. hive> ALTER TABLE test1 ADD COLUMNS(col3 char(5),col4 char(10));
2. OK
3. Time taken: 0.56 seconds
4. hive>
5. hive> ALTER TABLE test11 RENAME TO test12;
6. OK
7. Time taken: 0.343 seconds
8. hive>

g. Hive Select Data from Table


However, to select the columns from a table we use Hive select Data from Table
command. Moreover, Syntax for it is:
 Syntax-
SELECT [ALL | DISTINCT ] select_col, select_col FROM table WHERE
where_condition [GROUP BY col_list] [HAVING having_con] [ORDER BY
col_list][LIMIT number];
 For Example-
1. hive> SELECT * FROM t1;
2. OK
3. a1
4. b2
5. c3
6. def 104
7. Time taken: 2.036 seconds, Fetched: 4 row(s)
8. hive>

h. Hive Load Data


To load the data into the Hive table, we use Hive Load Data command. Moreover,
to load data to hive table from external file, below is the syntax.
 Syntax-
LOAD DATA [LOCAL] INPATH ‘filepath’ [OVERWRITE] INTO TABLE
table_name;
 For Example-
1. hive> LOAD DATA LOCAL INPATH 'sample.txt' INTO TABLE test;

You might also like