Hive数据库

来源:互联网 发布:linux启动mysql 编辑:程序博客网 时间:2024/06/03 13:36

Hive数据库:

Hive构建在基于静态批处理的Hadoop之上,Hadoop通常都有较高的延迟并且在作业提交和调度的时候需要大量的开销。因此,Hive并不能够在大规模数据集上实现低延迟快速的查询

hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能

Hive定义了简单的类 SQL查询语言,称为 HQL,它允许熟悉 SQL 的用户查询数据。

--创建数据库:

   create database if not exists库名

   comment 'this is test database'

   with dbproperties(‘creator’ = ‘hy’,’date’=’2017-6-20’)  ----数据库键值对属性信息

    location '库在的目录';

--查看数据的描述信息和文件目录位置信息

   describe database库名;

   describe database extended库名;

--过滤数据库:

 show databases like  " f . * " ;

----删除数据库

   drop database if exists库名;

--级联删除数据库(当数据库还有表时,级联删除表后在删除数据库),默认是restrict

   drop database if exists库名 cascade;

--修改数据库:只能修改数据库的键值对属性值。数据库名和数据库所在的目录位置不能修改

   alter database库名set dmproperties(‘edited-by’=’hy1’);

--创建表:其中tblproperties作用:按照键值对的格式为表增加额外的文档说明,也可用来表示数据库连接的必要的元数据信息

--hive会自动增加二个表属性:last_modified_by(最后修改表的用户名)last_modified_time(最后一次修改的时间)

   create table if not exists库名.表名(字段名字段类型comment ‘字段名解释’,salary float comment‘薪水’)

   comment‘这是一个测试的表’

   tblproperties(‘creator’=’hy,’created_at’=’2017-6-2309:50:33’)

   location ‘/路径/库名.db/表名’;

--查看和列举表的tblproperties属性信息

   show tblproperties table_name;

--使用like在创建表的时候,拷贝表模式(而无需拷贝数据)

   create table if not exists库名.2 like库名.1;

--查看表的详细结构信息(也可以显示表是管理表,还是外部表。还有分区信息)

   describe extended库名.表名;

--载入数据:先选择指定的库

   load data inpath'/group1/zhtest/data.txt' intotable表名;

--查询表中数据

 select * from表名;

--查询前两条

 select * from表名 limit 2;

--统计表的行数

  select  count(*) from表名 ;

--求某个字段和

   select sum(字段名) from 表名 ;

--删除表

   drop table if exists表名;

--修改表-表重命名

   alter table old_table_name rename to new_table_name;

--修改列信息

   alter table table_name

   change column old_name new_name int

   comment ‘this is comment’

    after severity; --字段移到severity字段之后(移动到第一个位置,使用first关键字)

--增加列

    alter table table_name add columns(app_name stringcomment ‘application name’);

--删除或者替换列

    alter table table_name replace columns(hms int comment‘hhh’);

--修改表属性

   alter table table_name set tblproperties(‘notes’=’thisis a notes’);

原创粉丝点击