-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands
230 lines (162 loc) · 6.95 KB
/
Commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
----------------------------------------------------------------------------------
Database commands
----------------------------------------------------------------------------------
Create a database:
CREATE DATABASE IF NOT EXISTS database_name;
List all databases:
SHOW DATABASES;
Use a specific database:
USE database_name;
Drop a database:
DROP DATABASE IF EXISTS database_name [CASCADE | RESTRICT];
CASCADE: Deletes all tables in the database.
RESTRICT: Prevents the database from being dropped if it contains tables.
Describe a database:
DESCRIBE DATABASE database_name;
----------------------------------------------------------------------------------
Table Commands
----------------------------------------------------------------------------------
Create a regular table:
----------------------------------------------------------------------------------
CREATE TABLE table_name (
column_name1 STRING,
column_name2 INT
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;
Create a partitioned table:
----------------------------------------------------------------------------------
Create a partitioned table
----------------------------------------------------------------------------------
CREATE TABLE table_name (
column_name1 STRING,
column_name2 INT
) PARTITIONED BY (partition_column STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;
Create an external table:
----------------------------------------------------------------------------------
Create an external table
----------------------------------------------------------------------------------
CREATE EXTERNAL TABLE table_name (
column_name1 STRING,
column_name2 INT
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE
LOCATION '/external/location/';
----------------------------------------------------------------------------------
Create a table like another table:
----------------------------------------------------------------------------------
CREATE TABLE new_table_name LIKE existing_table_name;
----------------------------------------------------------------------------------
List Tables
----------------------------------------------------------------------------------
List all tables in the current database:
SHOW TABLES;
Show tables matching a pattern:
SHOW TABLES 'pattern';
----------------------------------------------------------------------------------
Alter Table
----------------------------------------------------------------------------------
Add a column to a table:
ALTER TABLE table_name ADD COLUMNS (new_column_name STRING);
Change column name and type:
ALTER TABLE table_name CHANGE old_column_name new_column_name STRING;
Add a partition:
ALTER TABLE table_name ADD PARTITION (partition_column='partition_value') LOCATION '/path/';
Drop a partition:
ALTER TABLE table_name DROP PARTITION (partition_column='partition_value');
Rename a table:
ALTER TABLE old_table_name RENAME TO new_table_name;
----------------------------------------------------------------------------------
Drop Table
----------------------------------------------------------------------------------
Drop a table:
DROP TABLE IF EXISTS table_name;
Drop an external table but keep data:
DROP TABLE IF EXISTS external_table_name;
Truncate a table (delete all rows but keep schema):
TRUNCATE TABLE table_name;
Describe Table
DESCRIBE table structure:
DESCRIBE table_name;
Describe formatted details of a table (includes partitioning, storage):
DESCRIBE FORMATTED table_name;
----------------------------------------------------------------------------------
Data Manipulation Commands
----------------------------------------------------------------------------------
Inserting Data
Insert data into a table:
INSERT INTO TABLE table_name VALUES ('value1', 100);
Insert data into a table using SELECT:
INSERT INTO TABLE table_name SELECT * FROM another_table;
Insert overwrite (replace existing data):
INSERT OVERWRITE TABLE table_name SELECT * FROM another_table;
----------------------------------------------------------------------------------
Loading Data
----------------------------------------------------------------------------------
Load data from local file system into a table:
LOAD DATA LOCAL INPATH '/path/to/local/file' INTO TABLE table_name;
Load data from HDFS into a table:
LOAD DATA INPATH '/path/in/hdfs/file' INTO TABLE table_name;
Load data into a partitioned table:
LOAD DATA INPATH '/path/in/hdfs/file' INTO TABLE table_name PARTITION (partition_column='partition_value');
----------------------------------------------------------------------------------
Selecting Data
----------------------------------------------------------------------------------
Basic select query:
SELECT * FROM table_name;
Select with filtering:
SELECT * FROM table_name WHERE column_name='value';
Select specific columns:
SELECT column_name1, column_name2 FROM table_name;
Limit the number of results:
SELECT * FROM table_name LIMIT 10;
----------------------------------------------------------------------------------
Join tables:
----------------------------------------------------------------------------------
SELECT a.column_name, b.column_name
FROM table_a a
JOIN table_b b
ON a.id = b.id;
Group by and aggregate:
----------------------------------------------------------------------------------
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
----------------------------------------------------------------------------------
Partitions
----------------------------------------------------------------------------------
Show partitions in a table:
SHOW PARTITIONS table_name;
Add a partition:
ALTER TABLE table_name ADD PARTITION (partition_column='partition_value') LOCATION '/path/';
Drop a partition:
ALTER TABLE table_name DROP PARTITION (partition_column='partition_value');
----------------------------------------------------------------------------------
Views
----------------------------------------------------------------------------------
Create a view:
CREATE VIEW view_name AS SELECT column_name FROM table_name WHERE condition;
Drop a view:
DROP VIEW IF EXISTS view_name;
List all views:
SHOW VIEWS;
----------------------------------------------------------------------------------
Indexes
----------------------------------------------------------------------------------
Create an index on a table column
CREATE INDEX index_name ON TABLE table_name (column_name) AS 'COMPACT' WITH DEFERRED REBUILD;
Drop an index:
DROP INDEX index_name ON table_name;
----------------------------------------------------------------------------------
Miscellaneous Hive Commands
----------------------------------------------------------------------------------
Show current database:
SELECT current_database();
Show current user:
SELECT current_user();
Explain the execution plan of a query:
EXPLAIN SELECT * FROM table_name;
Set Hive parameters:
SET hive.execution.engine=mr;
List all functions:
SHOW FUNCTIONS;
Show specific function description:
DESCRIBE FUNCTION function_name;