hive oracle truncate table,hive基本操作之一

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

689f16ecc315

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。但内部表删除表时,会删除改路径的下的文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值