Hive operation data table

First, some basic commands, then I will be the code of practice for reference, but also facilitate finishing my memory:


***不是代码,但是这样我认为有利于一眼看到重点!!!***

DDL操作(数据定义语言)包括:Create、Alter、Show、Drop等。

(1)create database- 创建新数据库

(2)alter database - 修改数据库

(3)drop database - 删除数据库

(4)create table - 创建新表

(5)alter table - 变更(改变)数据库表

(6)drop table - 删除表

(7)create index - 创建索引(搜索键)

(8)drop index - 删除索引

(9)show table - 查看表

DML操作(数据操作语言)包括:Load 、Insert、Update、Delete、Merge。

(1)load data - 加载数据

①insert into - 插入数据

②insert overwrite - 覆盖数据(insert … values从Hive 0.14开始可用。)

(2)update table - 更新表(update在Hive 0.14开始可用,并且只能在支持ACID的表上执行)

(3)delete from table where id = 1; - 删除表中ID等于1的数据(delete在Hive 0.14开始可用,并且只能在支持ACID的表上执行)

(4)merge - 合并(MERGE在Hive 2.2开始可用,并且只能在支持ACID的表上执行)

注意:频繁的update和delete操作已经违背了Hive的初衷。不到万不得已的情况,还是使用增量添加的方式最好。
--------------------- 
作者:Mr-Hunter 
来源:CSDN 
原文:https://blog.csdn.net/qq_41035588/article/details/90636494 
版权声明:本文为博主原创文章,转载请附上博文链接!

The actual operation begins below

1. Create a data warehouse DB:

hive> create database DB;
OK
Time taken: 0.178 seconds

2. When you create a library, you should avoid the new library name and library name has been repeated, if the library name duplication will report the following error (I am under the premise of the existing DB library, once again created a DB Library)

hive> create database if not exists DB;
OK
Time taken: 0.022 seconds

3. Check the database information and route

hive> describe database DB;
OK
db		hdfs://bin01:9000/user/hive/warehouse/db.db	root	USER	
Time taken: 0.034 seconds, Fetched: 1 row(s)

4. Delete data warehouse DB, and view the table

hive> drop database if exists DB;
OK
Time taken: 0.238 seconds
hive> show tables;
OK
aa
user1
Time taken: 0.033 seconds, Fetched: 2 row(s)

5. Review the existing tables

hive> show tables;
OK
aa
user1
Time taken: 0.033 seconds, Fetched: 2 row(s)

6. Create a table inside cat, and view

hive> create table cat(cat_id string ,cat_name string);
OK
Time taken: 0.209 seconds
hive> show tables;
OK
aa
cat
user1
Time taken: 0.029 seconds, Fetched: 3 row(s)

7. Create an error message will appear in the repeating table

hive> create table cat(cat_id string ,cat_name string);
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. AlreadyExistsException(message:Table cat already exists)

8 ... to create an external table, table named cat2, there are two fields cat_id and cat_name, character type to string.

hive> create external table if not exists cat2(cat_id string,cat_name string);
OK
Time taken: 0.075 seconds
hive> show table
table         tables        tablesample   
hive> show tables;
OK
aa
cat
cat2
user1
Time taken: 0.034 seconds, Fetched: 4 row(s)

9. Add fields in an internal table in the cat, and the table structure view

hive> alter table cat add columns (group_id string,cat_code string);
OK
Time taken: 0.211 seconds
hive> desc cat;
OK
cat_id              	string              	                    
cat_name            	string              	                    
group_id            	string              	                    
cat_code            	string              	                    
Time taken: 0.126 seconds, Fetched: 4 row(s)

Table 10. cat2 rename cat3, and Display

hive> alter table cat2 rename to cat3;
OK
Time taken: 0.177 seconds
hive> show tables;
OK
aa
cat
cat3
user1
Time taken: 0.016 seconds, Fetched: 4 row(s)

11. Delete table cat3, and view the table

hive> drop table cat3;
OK
Time taken: 0.886 seconds
hive> show tables;
OK
aa
cat
user1
Time taken: 0.031 seconds, Fetched: 3 row(s)

12. The same configuration and the new table cat4 cat table, and the table view

hive> create table cat4 like cat;
OK
Time taken: 0.135 seconds
hive> show tables;
OK
aa
cat
cat4
user1
Time taken: 0.023 seconds, Fetched: 4 row(s)

Guess you like

Origin blog.csdn.net/sincere_love/article/details/92981264