hive学习--基本使用和创建内外部表

29 篇文章 0 订阅
15 篇文章 0 订阅

数据库

查看数据库: hive> show databases;

                        hive>show databases like 'h.*';---展示以h开头的所有数据库,注意:like后字符串匹配使用正则

创建数据库: hive> create database [if not exitsts] 数据库名

                                   [location 'HDFS的目录下'];-------不使用设置的warehouse目录

查看数据库属性命令:hive> desc database 数据库名;

使用数据库: hive> use 数据库名;

删除数据库:drop database 数据库名 [cascade(联级删除---连表一起删除)]

修改数据库:

临时显示当前正在使用的数据库名:set hive.cli.print.current.db=true;

临时显示字段名称:set hive.cli.print.header=true;

如果想永久显示:可在当前用户下→vi .hiverc。填入上两条sql语句。或者在hive-site.xml配置文件中修改该属性的值

创建内部表:当前使用drop table语句删除表时,存在hdfs上表的目录会被删除
   create table [if not exitsts] 表名
    (列名 数据类型,
     ....)
     [comment '表的描述']
    [row format delimited
       fields terminated by '\001'--字段之间的分割符\001八进制对应^A
       collection items terminated by '\002'--数组元素和结构元素的的分割符  ^B
       map keys terminated by '\003'--对应map类型的key-value之间分隔符 ^C
       lines terminated by '\n'--行之间的分割符换行
    ]--指定表行的格式
    [stored as textfile]--指定表存储的数据格式
     [location '可以指定表存储的路径']    

创建外部表:

create external table [if not exitsts] 表名
    (列名 数据类型,
     ....)
     [comment '表的描述']
    [row format delimited
       fields terminated by '\001'--字段之间的分割符\001八进制对应^A
       collection items terminated by '\002'--数组元素和结构元素的的分割符  ^B
       map keys terminated by '\003'--对应map类型的key-value之间分隔符 ^C
       lines terminated by '\n'--行之间的分割符换行
    ]--指定表行的格式
    [stored as textfile]--指定表存储的数据格式
     [location '可以指定表存储的路径']    
     
    例如:
    指定hdfs上的外部存储目录,当使用drop table时,只会删除存储在元数据中的表的映射信息;
    而hdfs上的表对应的目录不会被删除;

查看表:

hive> show tables;

查看表信息:desc 表名;

查看表中第二个字段的内容个数:select count(2) from 表名;

修改表


修改表名:alter table 旧表名 rename to 新表名
追加分区
     alter table 表名 add [if not exitsts]
     partition(year=2014,month=1) location 'hdfs://master:9000/hive/2014/1'
     partition(year=2014,month=2) location 'hdfs://master:9000/hive/2014/2'
     partition(year=2014,month=3) location 'hdfs://master:9000/hive/2014/3'

修改外部表location

     alter table ability2_20181010 set location '/test_log/hemap_ability2_log'
删除分区
     alter table 表名 drop [if exitsts] partition(year=2014,month=1)
  修改列信息
     alter table 表名 change [column] 旧列名 新列名 修改的类型 [comment '列说明'] after 修改列的前一个列名;
     注意
     如果修改的是第一个列,使用first关键字替换after子句;
替换列
     例如:创建原始表,create table x1(a1 string);
          替换并追加列: alter table x1 replace columns(b1 string,b2 int);
          注意:对于以存在列可以修改列名,但是不能修改列的类型;可以添加新的列;          
添加列
      alter table x1 add columns(w4 double,w5 string);

退出hive:quit;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值