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、创建数据库时,指定描述信息使用关键字:commenthive (myhive)> create database test2 comment " create database" ;

5、给数据库增加一些键值对属性信息使用关键字:with dbpropertieshive (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、删除数据库使用关键字:drophive> drop database if exists test;默认情况下,Hive是不允许删除一个包含有表的数据库。如想要删除数据库,1、首先删除数据库中的表,然后在删除数据库;2、在删除命令最后面加上cascade关键字,这样Hive会自动先删除数据库当中的表。

9、修改数据库使用关键字alterhive (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、拷贝表结构

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

4、拷贝表数据

使用关键字:ashive> 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 bycreate table t4(id int,name string) partitioned by(country string, state string);

2、查看分区表

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

显示具体某一个分区的数据使用关键字 partitionhive (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、表的重新命名

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

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

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

3、修改分区路径

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

4、删除某个分区

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

5、修改列信息

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

6、添加列

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

7、修改表属性

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

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

8、修改表的存储属性

使用关键字:sethive> 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

时间: 2024-07-30 21:46:38

hive(3)HiveQL数据定义的相关文章

Hive[5] HiveQL 数据操作

5.1 向管理表中装载数据  Hive 没有行级别的数据插入更新和删除操作,那么往表中装载数据的唯一途径就是使用一种“大量”的数据装载操作,或者通过其他方式仅仅将文件写入到正确的目录下: LOAD DATA LOCAL INPATH '${env:HOME}/califonia-employees' OVERWRITE INOT TABLE employees PARTITON (country=''US, state='CA') ; 向管理表中装载数据,如果目录不存在的话, overwrite

Hive的DDL数据定义语言

1.创建数据库 hive>create database myhive; hive>create database if not exists myhive; hive>show databases; hive>show databases like '*t*'; 说明:hive为创建的数据库生成了相对应的目录(*.db),目录在{hive.metastore.warehouse.dir}属性下,同时,数据库中的表将以目录中的子目录进行存储:default默认数据库除外. a.自定

Hive 官方手册翻译 -- Hive DDL(数据定义语言)

Hive DDL(数据定义语言) Confluence Administrator创建, Janaki Lahorani修改于 2018年9月19日 原文链接 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL 翻译:Google Google翻译,金山软件 金山词霸 校对:南大通用 范振勇 (2018.9.26) 一.概述 这里是HiveQL DDL语句的文档,其中包括: CREATE 数据库/SCHEMA,表

hive编程指南--employees表数据定义

hive编程指南中有个employees表,默认的分隔符比较繁杂,编辑起来不太方便(普通编辑器编辑的控制字符^A等被当成字符串处理了,没有起到分隔符的作用).收集的解决方案如下: http://www.myexception.cn/software-architecture-design/1351552.html http://blog.csdn.net/lichangzai/article/details/18703971 切记,简单的文本编辑器编辑如下的内容,分隔符是没被识别的,^A^B^C

第4章 DDL数据定义

第4章 DDL数据定义 4.1 创建数据库 1)创建一个数据库,数据库在HDFS上的默认存储路径是/user/hive/warehouse/*.db. hive (default)> create database db_hive; 2)避免要创建的数据库已经存在错误,增加if not exists判断.(标准写法) hive (default)> create database db_hive; FAILED: Execution Error, return code 1 from org.

03hive_DDL数据定义

一. DDL数据定义 创建数据库 1)create database db_hive; 2)避免要创建的数据库已经存在错误,增加 if not exists 判断. create database if not exists db_hive; 3)创建一个数据库,指定数据库在 HDFS 上存放的位置 create database db_hive2 location '/db_hive2.db'; 二. 查询数据库 1.显示数据库 1)show databases; 2)模糊查询 show da

DDL(Data Definition Language)数据定义语言基础

数据定义语言DDL(Data Definition Language)是SQL语言的三个主要组成部分之一(另外两个分别是数据操作语言DML(Data Mainpulation Language)和数据控制语言(Data Control Language)). 1:创建用户Create User 创建用户使用如下的语法 Create user 用户名 identified by 密码 [default tablespace 缺省表空间] [temporary tablespace 临时表空间].如

Hive基础之Hive体系架构&amp;运行模式&amp;Hive与关系型数据的区别

Hive架构 1)用户接口: CLI(hive shell):命令行工具:启动方式:hive 或者 hive --service cli ThriftServer:通过Thrift对外提供服务,默认端口是10000:启动方式:hive --service hiveserver WEBUI(浏览器访问hive):通过浏览器访问hive,默认端口是9999:启动方式:hive --service hwi 2)元数据存储(Metastore):启动方式:hive -service metastore

3-2数据定义

3-2数据定义 tags:数据库 关系数据库系统支持三级模式结构,其模式.外模式和内模式中单基本对象幽默师表,视图和索引等.因此SQL的数据定义功能包括,模式定义,表定义,视图和索引定义. 操作对象 创建 删除 修改 模式 create schema drop schema 表 create table drop table alter table 视图 create view drop view 索引 create index drop index alter 架构(模式schema) 为避免