hive(3)HiveQL数据定义

HiveQL与传统SQL区别

HiveQL是Hive的查询语言。与mysql的语言最接近,但还是存在于差异性,表现在:Hive不支持行级插入操作、更新操作和删除操作,不支持事物。

基本语法

数据库操作


1、创建数据库
hive> create database test; 或者 create database if not exists test;
2、查看数据库对应的目录文件
创建的数据库对应的数据目录或者存储在hdfs的目录为在hive配置文件里面定义的hive.metastore.warehouse.dir值
比如:我的是/hive/warehouse
查看刚刚创建的数据库对应的名称,如下所示:

# hadoop fs -ls /hive/warehouse
Found 3 items
drwxr-xr-x - root supergroup 0 2019-06-20 07:44 /hive/warehouse/myhive.db
drwxr-xr-x - root supergroup 0 2019-06-20 08:15 /hive/warehouse/test.db
drwxr-xr-x - root supergroup 0 2019-06-20 03:08 /hive/warehouse/user_info

  或者,使用desc或者describe database 数据库名字也可以查看具体的存储目录

hive (myhive)> desc database test;
OK
db_name comment location owner_name owner_type parameters
test hdfs://EDPI-HBASE/hive/warehouse/test.db root USER

  3、如果在创建数据库的时候,想要指定存储目录呢?

使用location关键字,如:
hive (myhive)> create database test1 location "/test1" ;

 hive (myhive)> desc database test1;
OK
db_name comment location owner_name owner_type parameters
test1 hdfs://EDPI-HBASE/test1 root USER

4、创建数据库时,指定描述信息
使用关键字:comment
hive (myhive)> create database test2 comment " create database" ;

5、给数据库增加一些键值对属性信息
使用关键字:with dbproperties
hive (myhive)> create database test with dbproperties ('name' = 'yjt', 'data' = '2019-06-20');
6、显示这些键值信息:
使用关键字:extended

hive (default)> desc database extended test;
OK
test hdfs://EDPI-HBASE/hive/warehouse/test.db root USER {data=2019-06-20, name=yjt}
Time taken: 0.228 seconds, Fetched: 1 row(s)



7、显示数据库
hive> show databases; 或者使用正则匹配:show databases like "my.*"; 表示匹配以my开头的数据库名称。

8、删除数据库
使用关键字:drop
hive> drop database if exists test;
默认情况下,Hive是不允许删除一个包含有表的数据库。如想要删除数据库,1、首先删除数据库中的表,然后在删除数据库;2、在删除命令最后面加上cascade关键字,这样Hive会自动先删除数据库当中的表。

9、修改数据库
使用关键字alter
hive (default)> alter database test set dbproperties ('name'= 'test');
10、查询当前的数据库
hive> select current_database();

表操作

一、内部表

1、创建表:

hive> create table if not exists myhive.employess(
name string comment "employess name",
salary float comment "employess salary",
subordinates array<string> comment 'name of subordinates',
deductions map<string,float> comment 'keys are deductions names,values are percentages',
address struct<street:string,city:string,state:string,zip:int> comment "home address")
comment 'Description of the table'
tblproperties ('creator'='me', 'created_at'='2019-06-20')

在创建表的时候,为每一个字段都指定了comment信息,也可以为这个表指定comment信息。

tblproperties关键字:按键值对的格式为这个表添加额外的文档说明。

Hive自懂增加的两个表属性:

last_modified_by:保存着最后修改这个表的用户名

last_modified_time:保存着最后修改一次的时间秒。

2、列举某个表的tblproperties属性

hive (myhive)> show tblproperties myhive.employess;
OK
COLUMN_STATS_ACCURATE    {"BASIC_STATS":"true"}
comment    Description of the table
created_at    2019-06-20
creator    me
numFiles    0
numRows    0
rawDataSize    0
totalSize    0
transient_lastDdlTime    1561023045
Time taken: 0.088 seconds, Fetched: 9 row(s)

3、拷贝表结构

使用关键字:like
hive (myhive)> create table if not exists t1 like employess;
这张t1表现在与employess表具有相同的表结构,只是没有数据。
可以使用desc关键字查看t1表结构。

4、拷贝表数据

使用关键字:as
hive> creat table t1 as select * from t2; //把t2表的数据插入到t1表

5、显示数据库下已经存在的表

hive<myhive>show tables;

如果想要在当前数据库显示其他数据库下的表,则需要使用in关键字
hive<default> show tables in myhive;

在显示表的时候,同样可以使用正则表达式
hive> show tables in myhive 'em.*';

6、查看表结构

1、直接使用desc或者describe关键字

hive (default)> desc user_info;
OK
user_id bigint
firstname string
lastname string
count string
Time taken: 1.336 seconds, Fetched: 4 row(s)

2、使用desc 配合extended关键字显示详细输出

hive (default)> desc extended user_info;
OK
user_id bigint
firstname string
lastname string
count string

Detailed Table Information Table(tableName:user_info, dbName:default, owner:root, createTime:1560823283, lastAccessTime:0, retention:0, sd:StorageDescriptor(cols:[FieldSchema(name:user_id, type:bigint, comment:null), FieldSchema(name:firstname, type:string, comment:null), FieldSchema(name:lastname, type:string, comment:null), FieldSchema(name:count, type:string, comment:null)], location:hdfs://EDPI-HBASE/hive/warehouse/user_info, inputFormat:org.apache.hadoop.mapred.TextInputFormat, outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat, compressed:false, numBuckets:-1, serdeInfo:SerDeInfo(name:null, serializationLib:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, parameters:{serialization.format=1}), bucketCols:[], sortCols:[], parameters:{}, skewedInfo:SkewedInfo(skewedColNames:[], skewedColValues:[], skewedColValueLocationMaps:{}), storedAsSubDirectories:false), partitionKeys:[], parameters:{totalSize=86, numRows=6, rawDataSize=80, COLUMN_STATS_ACCURATE={"BASIC_STATS":"true"}, numFiles=2, transient_lastDdlTime=1561000088}, viewOriginalText:null, viewExpandedText:null, tableType:MANAGED_TABLE)
Time taken: 0.068 seconds, Fetched: 6 row(s)

上面这一堆信息看的脑袋大,太混乱了

3、使用desc配合formatted关键字,也就是使用formatted替换extended,输出内容友好一些,这种方式更长用。

hive (default)> desc formatted user_info;

二、外部表

1、创建外部表

使用关键字:external
hive (myhive)> create external table if not exists stocke(id int,ymd string)row format delimited fields terminated by ',' location '/tmp/stocke';
字段分割使用','
使用location关键字指定对应的存储目录

三、分区表:

1、创建分区表

使用关键字:partitioned by
create table t4(id int,name string) partitioned by(country string, state string);

2、查看分区表

使用关键字:partitions 
hive (myhive)> show partitions t4;

显示具体某一个分区的数据
使用关键字 partition
hive (myhive)> show partitions t4 partition(state)

3、插入数据的方法

格式1:
INSERT INTO TABLE tablename [PARTITION (partcol1[=val1], partcol2[=val2] ...)] VALUES values_row [, values_row …];

格式2:(推荐使用)
load data local inpath '/home/had/data1.txt' into table employees partition (country =china,state=Asia)

四、修改表

1、表的重新命名

使用关键字:rename
hive>alter table t4 rename to t6;

2、增加表分区(通常是外部表)

使用关键字:add
hive> alter table t6 add partition(country='US',state='UA') location "/logs/country=US/state=UA";

3、修改分区路径

使用关键字:distcp
hadoop fs -distcp /logs/country=US/state=UA /logs_old/country=US/state=UA //这是在shell命令行执行的
使用关键字:set
Hive> alter table t6 partition(
country='US',state='UA') set location "/logs_old/country=UA/state=UA"

4、删除某个分区

使用关键字:drop
hive> alter table t6 drop if exists partition(country='US',state='UA'); //对于内部表,删除分区以后,元数据和数据都会被删除。对于外部表,分区内的数据不会被删除

5、修改列信息

使用关键字:change column、after、first    //对某个字段的重新命名,修改其位置、类型、和注释
假设t1 表存在两个字段 id、name、cj
hive> alter table t1 change column comment 'test alter change' name age int after cj; //修改字段name的名字、类型,并添加到cj之后,如果想要添加到第一列,可以使用first关键字代替after cj,里面的column 和comment可选

6、添加列

使用关键字:add
hive>alter table t1 add columns(city string,address string); //如果新增的字段中有某个或者多个字段位置是错误的,那么需要使用alter column 表名 change column 语句逐一调整。

7、修改表属性

可以增加附加的表属性或者修改已经存在的属性,但是无法删除属性

使用关键字:set
hive> alter table t1 set tblproperties('info' = 'this is a test');

8、修改表的存储属性

使用关键字:set
hive> alter table t1 partition(country='US',state='UA') set fileformat sequencefile; //修改表的存储属性为sequencefile

9、其他操作

1,把一个分区打包成一个har包,只适用于分区表中独立的分区
  alter table employees archive partition (country="china",state="Asia")
2, 把一个分区har包还原成原来的分区
  alter table employees unarchive partition (country="china",state="Asia")
3, 保护分区防止被删除
   alter table employees partition (country="china",state="Asia") enable no_drop
4,保护分区防止被查询
    alter table employees partition (country="china",state="Asia") enable offline
5,允许分区删除和查询
   alter table employees partition (country="china",state="Asia") disable no_drop
   alter table employees partition (country="china",state="Asia") disable offline
原文地址:https://www.cnblogs.com/yjt1993/p/11063666.html