美文网首页
hive基本操作之一

hive基本操作之一

作者: 点典 | 来源:发表于2018-01-05 17:28 被阅读0次

1.hive表中的基本数据类型

2,hive创建分区表

      2.1内部表

                建表语句:

                create table table_name(

                    Field1 string,

                    Field2 int,

                   Field3 bigint,

                )partitioned by (Field4 string,Field5 string)        --用来指定分区

               row format delimited fields terminated by ',';      --用来指定分隔符,本例程分隔符为“,”

      2.2外部表

           2.2.1如何建立外部表

                           关键字是:external

                           基本语句是:create external table table_name (...) ...

           2.2.2外部表的指定位置

                           1.不指定外部表的存放路径,默认Hive将在HDFS路径下的/user/hive/warehouse/文件夹下以外部表的表名创建一个文件夹。建表语句:

                           create external table table_name(

                               Field1 string,

                               Field2 int,

                               Field3 bigint,

                           )partitioned by (Field4 string,Field5 string)        --用来指定分区

                           row format delimited fields terminated by ',';      --用来指定分隔符,本例程分隔符为“,”

                           2.指定外部表存放路径,Hive不会在/user/hive/warehouse/文件夹下建立文件,hive会直接把指定路径存储在元数据中。建表语句:

                          create external table table_name(

                               Field1 string,

                               Field2 int,

                               Field3 bigint,

                          )partitioned by (Field4 string,Field5 string)        --用来指定分区

                          row format delimited fields terminated by ','       --用来指定分隔符,本例程分隔符为“,”

                          location '/fdir/cdir/sdir';                                       --指定路径

            2.2.3外部表内部表转换

                          1.内部表转换为外部表:alter table tablename set tblproperties ('EXTERNAL' = 'TRUE');

                          2.外部表转换为内部表:alter table tablename set tblproperties ('EXTERNAL' = 'FALSE');

3.表操作

      3.1字段的怎删改

          3.1.1.添加字段

                  alter table table_name add columns(aField1 int, aField1 int);

          3.1.2.删除字段

                  alter table name drop [column] column_name

           3.1.3.修改字段:改变列名/类型/位置/注释

                  alter table table_name change column_name new_name new_type [conmment col_conmment] [first|after column_name];

     3.2表的查删改

            3.2.1.查找数据

                                select* from table_name;

            3.2.2.删除数据

                                delete from table_name where Field1='xxx;

            3.2.3.修改数据

                                 update table_name set Field1='xxx'  where Field2='xxx';

             注意:若删除,修改不能操作,或操作报错,则需要在hive-site.xml中配置相关属性

            3.2.4.删除整个表

                     1.truncate:

                           truncate table table_name;

                        truncate用于删除所有的行,这个行为在hive元存储删除数据是不可逆的。保留表结构

                     2.drop:

                          drop table table_name;

                         drop用来删除hive中的表。(完全删除)

            3.2.5获得表的建表语句

                         show  create  table table_name;

            3.2.6重命名表

                        alter table table_name rename to new_table_name;

4.修改表属性

     4.1增加表的属性

                 alter table table_name  set  tblproperties  table_properties;

            用户可以使用这个语句增加表属性,其中table_properties的结构为       (property_name=property_value,property_name=property_value, ...),目前last_modified_time(最后修改时 间),last_modified_user(做最后修改的用户)是由Hive自动管理的。用户可以向列中添加自己的属性

    4.2查看表属性

                discribe  extebded  table_name;

                desc formatted tablename; --查看比较详细的表结构信息

                desc tablename;      --查看表结构信息比较简单

   4.3增加SerDE属性

          a:    alter table table_name set serde serde_class_name

                  [whit  serdeproperties serde_properties];

          b:    alter table table_name set serdeproperties serde_properties;

        上面两个命令都允许用户想SerDE对象增加用户定义的元数据。Hive为了序列化和反序列化数据,将会初始化SerDE属性,并将属性传给表的SerDE。这样用户可以为自定义的SerDe存储属性。上面serde_properties的结构为(property_name=property_value,property_name=property_value, ...)

   4.4修改表文件格式和组织

          a:        alter table table_name set fileformat file_format;

          b:        alter table table_name clustered by(col_name, col_name, ...)

                     [sort by(col_name, ...)] into num_buckets buckets;

    4.5 修改hive表中的分隔符

         alter table ods_wls_login_bak set SERDEPROPERTIES('field.delim'='|');

         alter table ods_wls_login_bak set SERDEPROPERTIES('serialization.format'='|');

5.表的分区操作

     5.1 修改分区名字

          alter table table_name partition (property_name=property_value1,...) rename to partition (property_name=new_property_value1,...);

        例如:

          alter table tmp_student partition (dt='2017_12_18',hour='12') rename to partition (dt='20171218',hour='12');

        把分区(dt=2017_12_18,hour=12)改为(dt=20171218,hour=12)

    5.2 添加分区

        alter table table_name add partition(property_name=property_value1,...) location 'path';

        添加指定分区下的具体分区数值,并把path路径下的数据关联到该分区下

6.Hive数据操作

   6.1数据导入

       6.1.1 从HDFS上导入数据

           load data inpath 'patht' into table table_name [partition(...)];

      6.1.2关联数据到指定分区(添加分区)

           alter table table_name add partition (...) location 'path';   --推荐使用

          注意:load加载数据到指定分区,会改变源路径的目录结构

                    alter关联数据到指定分区,且不移动源数据和改变源路径结构(推荐使用)

     6.1.3 从别的表中查询出相应的数据并导入到Hive表中

          insert  into  table  table_name2 [ partition (...)]  select *  from table_name1;

          insert  overwrite tabletable_name2 [partition (...)]  select *  from table_name1;

         into:是插入数据,overwrite:是覆盖数据,即先删除表中原有的数据,然后插入

      6.1.4 在创建表的时候通过从别的表中查询出相应的记录并插入到所创建的表中

          create table table_name1 as  select * from table_name2;

   6.2修改location

        alter table table_name [partition(...)] set location 'path';         --推荐使用

          注意:不只是外部表可以指定location,内部表也可以指定location。但内部表删除表时,会删除改路径的下的文件

相关文章

  • hive基本操作之一

    1.hive表中的基本数据类型 2,hive创建分区表 2.1内部表 建表语句: create table...

  • HIVE

    基本操作 hive进入hive模式 exit; 退出hive模式 展示正则匹配表名hive> use ad_s...

  • Hive之同比环比的计算

    Hive系列文章 Hive表的基本操作[http://www.ikeguang.com/?p=1657] Hive...

  • Hive sql常见操作

    基本sql操作 hive表操作 分区操作 Hive内置函数 (1)数学函数 常用的数学函数都有:round、flo...

  • Hive 基本操作

    准备工作 1.检查各项服务是否已正常启动 [x] hdfs启动【start-dfs.sh】 [x] yarn启动【...

  • Hive 基本操作

    数据库基本操作 ( 和MySQL脚本相似 ): 创建删除库操作 创建删除表操作 hive 特点 Hive不支持修改...

  • Hive基本操作

    在所有操作前先启动hdfs、yarn、historyserver。 hive操作 启动hivebin/hive 创...

  • Hive基本操作

    参考:hive 表的常用操作Hive基本操作 1 Thrift Server Thrift 支持多种语言之间的RP...

  • Hive基本操作

    1.使用SQL文件创建一张表: hive -f create_table 2.将外部数据加载到一张表里面: LOA...

  • hive 基本操作

    hive添加表 hive的命令就是mysql的命令. 创建数据库 创建表 把数据文件put到表在hdfs中的目录下...

网友评论

      本文标题:hive基本操作之一

      本文链接:https://www.haomeiwen.com/subject/ohnawxtx.html