hive的基本操作
一,认识hive
1,hive介绍
The Apache Hive ™ data warehouse software facilitates reading, writing, and managing large datasets residing in distributed storage using SQL. Structure can be projected onto data already in storage.A command line tool and JDBC driver are provided to connect users to Hive.
简单解释一下: Apache下的hive数据仓库软件便于读写和使用sql管理大型分布式存储的数据集。结构可以投影到存储的数据上。提供一个命令行工具和jdbc驱动程序来将用户连接到hive
2,什么是OLAP和OLTP
OLAP //on line analyze process,在线分析处理
OLTP //on line transaction process,在线事务处理
3, 什么是sql 和 nosql 和 hql
nosql : 很多人会认为不是sql,然而并不是这样 not only sql : 不仅仅是SQL、不是关系型数据库(RDBMS)、类似文档型 的数据库(如hbase,mongdb,redis等等)
sql :structured query language (结构化查询语言)sql数据库都必须是RDBMS(关系型数据库管理系统) 这样的数据库我 们一般都是很常用的(mysql,oracle,sql server等等)
hql :hive query langrage (hive 查询语言)
1,hive支持的三种数据结构(table(表) + paritition(分区) + bucket(分桶))
2,缺陷
不是关系型数据库(RDMRS)
不是OLTP也就是说他是OLAP(在线分析数据)
不适合实时查询和行级更新
3,特性
schema存储在数据库中,处理hdfs的数据
OLAP
HQL
伸缩性、可扩展、批处理大量数据速度很快
4,hive构架
client : webui(web访问)和CLI(命令行)和JDBC/ODBC连接
hive : meta store(元数据)+Hive 处理引擎(以及执行引擎)
底层:hadoop(hdfs+mr)
5,hive组件
1,metadata,存放在RDMRS中的(元数据:table + basebase + colum)
2, HQL处理引擎:
写查询语句用于MR作业
3,execute engine
执行引擎,执行查询工作,得到结果
4,HDFS/HBase
存储数据
6,hive执行过程图:
二,hive的操作
1,hive的基本类型:
1,数字类型:tinyint 相当于java的 byte 占用 1个字节
smallint sort 2个字节
int int 4个字节
bigint long 8个字节
2,字符串类型:他们用' '和" "都可以
varchar char string
3,TImestamp //YYYY-MM-DD hh:mm:ss.ffffffff 精确到纳秒
data //YYYY-MM-DD
4,浮点型
double
float
decimal //常量,decimal(10,0)=(刻度,精度)
(还用比如一下集合类型,array集合,map集合,struct集合)
2,hive的基本hql语句(hive的语法和mysql几乎一样所以会sql的学hive真的太简单了)
create database myhive; //对应创建 hdfs://t124:8020/user/hive/warehouse/myhive.db
select * from hive.dbs ; //在mysql查看数据库中的使用记录
select * from hive.tabs ; //在mysql查看表的使用记录
select * from hive.colmuns_v2;//在mysql查看字段的使用记录
show databases; //显示数据库信息
drop database myhive; //删除数据库信息
use myhive; 使用数据库
desc formatted usertalbes; //显示格式化的表信息,(带库的信息)
建表的简单格式
create table usertables(
id int,name string,money double,
subject array<string>,sorce map<string,int>,
address struct(street:string,city:string,state:string)
)
建立表的标准格式:
create table usertables(
id int, name string ,age int
) row format delimited
fields terminated by "\t"
lines terminated by "\n"
stored as textfile;
建表高级格式: (主要介绍)
create table usertables(
id int,name string,money double,
subject array<string>,sorce map<string,int>,
address struct(street:string,city:string,state:string)
) row format delimited // 这个必须写在前面(除了 stored as ***)这行代表一个下面的条件的一个规定 ( 除了stored as ***)
fields terminated by " " // 相当于row format delimited fields terminated by " "表示字段以空格分隔
lines terminated by "\n" // 相当于row format delimited lines terminated by "\n" 表示行以制表符/n分隔
事实上lines terminated by 只能以/n分隔默认也是以/n分隔
collection items terminated by "," // 相当于row format delimited collection items terminated by "," 表示集合以,分隔
map keys terminated by ":" // 相当于row format delimited map keys terminated by ":" 表示map以:分隔
stored as textfile; //使用文本格式来加载文件
3,使用load命令加载本地文件hive(hadoop)
语法:
load data [local] inpath 'filepath' [overwirte] into table tablename (partcol1=val1,partcol2=val2.......)
local //指定本地文件,如果不指定会使用hdfs上的文件路径
overwrite //覆盖表数据(全部表数据)
partition //分区表
4,修改表结构
alter table t1 rename to t2 //表的重命名
alter table t1 add columns (name string) //添加列
alter table t1 drop column name; //删除列
alter table t1 change name name1 int; //修改列(可以改列名)
alter table t1 replace name int; //替换列(全部替换)
5, hive脚本命令(就是不开起hive命令行等执行hql语句)
hive --hlep //hive帮助文档
hive -e "show databases" //一次性命令
hive -S -e "show databases" //执行静态信息
hive -f "xx.sql" //执行脚本
hive -i .hiverc //-i在启动hive时,执行指定文件
hive>tab键 //查看帮助帮助命令
hive>!cmd xx //执行shell命令如: !echo "hello world"
hive>dfs -lsr /; //执行dfs命令
hive>set hive.cli.print.header=true ; //动态设置头信息
hive>show databases like 'my*' //统配符
hive>create database myhive location '/x/x/x' //指定数据库在hdfs的位置
hive>drop database myhive cascde //级联删除数据库
6,hive的复杂类型
1.array,数组,类型相同。
a.创建表
create table arrtest(id int,hobbies<string>,name string)
row format delimited
fields terminated by ' '
collection items terminated by ','
lines terminated by '\n'
stored as textfile ;b.加载数据
1,load data local inpath '/home/had/arr.dat' into arrtest ;
c.查询
select * from arrtest ;
select id,hobbies[0] from arrtest ;//集合类型的获取方式,用下标的方式
select id,array('xx','yy') from arrtest ;//array函数(用于生成集合类型的数)
2.struct<>结构体,和javabean差不多,可以设置里面的属性一旦设置不可更改
a,创建表 create table stru1(id int,addr struct<province:string,city:string,street:string>)
row format delimited fields terminated by ' '
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
stored as textfile ;b.加载数据
1,load data local inpath '/home/had/strul.dat' into strul;
c.查询
select * from strul;
select id,addr.privince from strul;//通过结构体的key值查询3.map<key,value>map结构类型他的键值可以任意变可以理解成一个动态的结构体
a创建表
create table map1(id int,scores map<string:int>)
row format delimited fields terminated by ' '
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
stored as textfile ;
b.加载数据
1,load data local inpath '/home/had/map.dat' into mapl;
c.查询
select * from mapl;
select id,scores[key值] from mapl;通过结构体的key值查询