//建库
create database if NOT EXISTS test03
//建库,并指定位置
create database if NOT EXISTS test04 location '/apps/hive/warehouse/cheshi_4'
//查询所有的数据库
show databases
//使用库
use test03
//展示所有表
show tables
//显示表结构
desc employee
//显示创建表的语句【详细】
show create table employee
//显示表的拓展描述信息
desc formatted employee
//建表
create table IF NOT EXISTS employee
(eud int,name String,salary String,destination String)
COMMENT 'Employee table'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE
//建表,字段类型多样化
create table IF NOT EXISTS t1(
id int
,name string
,hobby array<string>
,add map<String,string>
)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
//建 外部表 ,注意external和location 字段类型多样化
create external table IF NOT EXISTS t2(
id int
,name string
,hobby array<string>
,add map<String,string>
)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
location '/user/oozie/oozie_test/t2'
//要导入的数据如下
1,xiaoming,book-TV-code,beijing:chaoyang-shagnhai:pudong
2,lilei,book-code,nanjing:jiangning-taiwan:taibei
3,lihua,music-book,heilongjiang:haerbin
//创建表,携带表结构
create table IF NOT EXISTS emp_2 like employee
//创建外部表,携带表结构,并指定数据的位置
create external table IF NOT EXISTS emp_wb_3 like employee
location '/user/hive/warehouse/ods/of_out_stock_old'
//创建表,携带数据
create table emp_4 as select * from employee
//内部表转外部表,只是改了external的属性,table type属性 没有变化,删除表的时候,hdfs文件不会删除
alter table emp_2 set TBLPROPERTIES('EXTERNAL'='true')
//外部表转内部表,只是改了external的属性,table type属性 有变化
alter table emp_2 set TBLPROPERTIES('EXTERNAL'='false')
//表重名
alter table emp_2 rename to emp_lei_2
//增加字段
alter table emp_lei_2 add columns (age int comment '年龄')
//修改字段
alter table emp_lei_2 change age age_1 string
//删除字段(COLUMNS中只放保留的字段)
alter table emp_lei_2 replace columns (eud string,name String,salary String,destination String)
//删除数据库
drop database db1
//删除数据库和表
drop database db1 cascade
//删除表
drop table emp_lei_2
//删除表数据(删除所有数据,保留表结构)
truncate table my_partitioner1
//从本地加载数据到表,linux上的文件不会丢失,相当于是复制 ,这是 追加的模式
load data local inpath '/data/log/1.txt' into table employee;
//从本地加载数据到表,linux上的文件不会丢失,相当于是复制 ,overwrite 是 覆盖的模式
load data local inpath '/data/log/2.txt' overwrite into table t1
//从hdfs将文件加载到hive表,同时hdfs上的文件会丢失,相当于是移动文件
load data inpath '/user/oozie/oozie_test/dir8/1.txt' into table employee;