HiveQL:数据定义(三)

本文介绍了使用Hive SQL进行数据库及表的操作方法,包括创建、删除数据库与表,以及如何加载数据、修改表结构等。此外,还详细讲解了内部表、外部表和分区表的区别与使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

drop database if exists 数据库名 cascade ;//数据库里有表,删不掉,需用强制删除

create table test(id string, name string, age int);

CREATE TABLE table(

id STRING,

name STRING,

age INT)

ROW  FORMAT  DELIMITED

FIELDS  TERMINATED BY '\t'

LOCATION '/test0923';

vi t2.tsv

hadoop fs -put t2.tsv /test0923/

hadoop fs -ls /test0923

select * from t2;

 

CREATE TABLE t3(

id STRING,

name STRING,

age INT)

ROW  FORMAT  DELIMITED

FIELDS  TERMINATED BY '\t'

e1:

sogou 500w数据,创建一张表sogou_20111230,这张表在sogou这个数据库中,注意,6个字段的数据类型.

e2:

把sogou 500w的数据放到表中,并执行下面的查询,

查询前100行数据

select * from sogou.sogou_20111230 limit 100;

SELECT uid FROM sogou_20111230 limit 10;

2011-12-30 00:00:08  timestamp

20111230000008  string

select `order` from sogou_20111230 order by `order` limit 10;   //order是关键字,不能直接用作列名

e3:

sogou_20111230_2

\t -> ,

hive> CREATE TABLE sogou_20111230(

    > ts string,

    > uid string,

    > keyword string,

    > rank int,

    > order int,

    > url string)

    > ROW FORMAT DELIMITED

    > FIELDS TERMINATED BY '\t'

    > LOCATION '/sogoudata';

1. 内部表

   CREATE TABLE t1(id int);  

   LOAD DATA LOCAL INPATH '/root/id' INTO TABLE t1;

   

   CREATE TABLE t2(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

2. 外部表MBNM

   hadoop fs -put id /external/id 

   create external table t5(id int) location '/external';  

关键字external 告诉Hive这个表是外部表,删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉,元数据信息存储在mysql中

describe extended s1;//tableType:MANAGED_TABLE)

describe extended s2;// tableType:EXTERNAL_TABLE) 

 

3. 分区表

   CREATE TABLE t3(id int) PARTITIONED BY (day int);                --partitioned

   LOAD DATA LOCAL INPATH '/root/id' INTO TABLE t1 PARTITION (day=20);     --partition

 

 

修改列名

IUYTREWQ   :

hive> alter table test

    > change column stuname name string;

describe test;

增加列:

hive> alter table test add columns(

    > height int);

describe test;

替换列:

hive> alter table test replace columns(

    > id int,

    > name string,

    > age int);

为表添加属性:

hive> alter table test set tblproperties (

    > 'note'='hello welcome');

show create table test;

========================================

创建带有分区的内部表:

hive> create table testpar(

    > id int,

    > name string,age int) PARTITIONED BY (day string)

    > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'

    > location '/testpar';

为带有分区的内部表加载数据:

hive> load data local inpath '/home/zkpk/test' into table testpar

    > partition (day='0925');

添加防止删除的保护:

hive> alter table testpar

    > partition (day='0925') enable no_drop;

测试:删除分区

hive> alter table testpar drop if exists partition (day='0925');

删除添加的"删除"保护:

hive> alter table testpar

    > partition (day='0925') disable no_drop;

添加防止查询的保护:

hive> alter table testpar

    > partition (day='0925') enable offline;

删除防止查询的保护:

hive> alter table testpar

    > partition (day='0925') disable offline;

select * from testpar;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值