Hive 建表

-- 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值