-- 1.建表
-- 1.1 内部表
drop database Test;
create database Test location "/hive";
use test;
create table if not exists student(
id int,
name string
-- age int,
-- height double,
-- weight double
)-- 声明分隔符(默认符\n)
row format delimited fields terminated by "\t"
--指定路径
location "/hive/student";
-- 填充数据-hadoop fs student.txt /hive 会自动填充进student表
select * from student;
-- 删表
-- drop table default.student ;
-- 查看表结构
show create table student;
show tables ;
desc student;
-- 1.2 外部表
use test;
drop table student;
-- 外部表通常可用于处理其他工具上传的数据文件,对于外部表,Hive只负责管理元数据,不负责管理HDFS中的数据文件。
-- 删除hdfs文件时,外部表并不会将数据删除。而内部表会将数据删除。
create external table if not exists student(
id int,
name string
)
row format delimited fields terminated by '\t'
location '/hive/student';
-- 1.3 复杂数据类型处理
-- array 数组是一组相同类型的值的集合 array<string> arr[0]
-- map map是一组相同类型的键-值对集合 map<string, int> map['key']
-- struct 结构体由多个属性组成,每个属性都有自己的属性名和数据类型 struct<id:int, name:string> struct.id
/*
{
"name": "dasongsong", -- 使用String类型
"friends": [
"bingbing",
"lili"
], -- 使用array<String>
"students": {
"xiaohaihai": 18,
"xiaoyangyang": 16
}, -- 使用map<String:int>
"address": {
"street": "hui long guan",
"city": "beijing",
"postal_code": 10010
} -- 使用struct进行解析数据 struct<city:string,street:string,postal_code:int>
}
*/
use test;
create table teacher
(
name string,
friends array<string>,
students map<string,int>,
address struct<city:string,street:string,postal_code:int>
)
-- 行格式Json
row format serde 'org.apache.hadoop.hive.serde2.JsonSerDe'
location '/hive/teacher';
select * from teacher;
-- 查询复杂类型
-- select friends[0],name,students["xiaohaihai"],address.city from teacher;
-- 1.4 create table as select【中间表】和create table like
-- 使用中间表可以有效地存储过程中的数据
create table teacher1 as select * from teacher ;
select * from teacher;
-- 使用like复刻表结构
create table teacher2 like teacher;
select * from teacher2;
-- 修改表数据
-- 表的重命名
alter table teacher1 rename to teacher_copy;
-- 修改列的信息,注意表信息不能为空
use test;
alter table teacher_copy add columns(gender string);
08-03
5073
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交