hive是基于 hadoop 的一个数据仓库工具:
hive利用hdfs存储数据,利用mapreduce查询数据,聚合函数需要经过mapreduce,非聚合函数直接读取hdfs块信息,不通过mapreduce。
db:在hdfs中表现为${hive.metastore.warehouse.dir}目录下一个文件夹
table:在hdfs中表现所属db目录下一个文件夹
external table:与table类似,不过其数据存放位置可以在任意指定路径
partition:在hdfs中表现为table目录下的子目录
bucket:在hdfs中表现为同一个表目录下根据hash散列之后的多个文件
1.1.6 hive的架构原理
driver是核心,driver可以解析语法,最难的就是解析sql的语法,只要把sql的语法解析知道怎么做了,它内部用mapreduce模板程序,它很容易把它组装起来,比如做一个join操作,最重要的是识别语法,认识你的语法,知道你语法有什么东西,解析出来会得到一个语法树,根据一些语法树,去找一些mapreduce模板程序,把它组装起来。
例如:有二个表去join,内部有一个优化机制,有一个默认值,如果小表小于默认值,就采用map—join ,如果小表大于默认值,就采用reduce——join(其中map——join是先把小表加载到内存中),组装时候就是输入一些参数:比如:你的输入数据在哪里,你的表数据的分隔符是什么,你的文件格式是什么:然而这些东西是我们建表的时候就指定了,所以这些都知道了,程序就可以正常的跑起来。
hive有了driver之后,还需要借助metastore,metastore里边记录了hive中所建的:库,表,分区,分桶的信息,描述信息都在metastore,如果用了mysql作为hive的metastore:需要注意的是:你建的表不是直接建在mysql里,而是把这个表的描述信息放在了mysql里,而tables表,字段表是存在hdfs上的hive表上,hive会自动把他的目录规划/usr/hive/warehouse/库文件/库目录/表目录,你的数据就在目录下
内部表在删除表的时候,会删除元数据和数据;外部表在删除表的时候,只删除元数据,不删除数据
内部表和外部表使用场景:
分桶操作:按照用户创建表时指定的分桶字段进行hash散列,跟mapreduce中的hashpartitioner的原理一模一样。
mapreduce中:按照key的hash值去模除以reducttask的个数;hive中:按照分桶字段的hash值去模除以分桶的个数
hive分桶操作的效果:把一个文件按照某个特定的字段和桶数散列成多个文件好处:
1、方便抽样;
2、提高join查询效率
做统计的时候少统计,把我们的数据放在多个文件夹里边,我们统计时候,可以指定分区,这样范围就会小一些,这样就减少了运行的时间。
db: myhive, table: student 元数据:hivedb
hive的元数据指的是 myhive 和 student等等的库和表的相关的各种定义信息,该元数据都是存储在mysql中的:
myhive 和 hivedb的区别:
创建表:create table pokes (foo int, bar string);
creates a table called pokes with
two columns, the first being an integer and the other a string
创建一个新表,结构与其他一样
hive> create table new_table like records;
创建分区表:
hive> create table logs(ts bigint,line string) partitioned by (dt
string,country string);
加载分区表数据:
hive> load data local inpath '/home/hadoop/input/hive/partitions/file1' into
table logs partition (dt='2001-01-01',country='gb');
展示表中有多少分区:
hive> show partitions logs;
展示所有表:
hive> show tables;
lists all the tables
hive> show tables '.*s';
lists all the table that end with 's'.
the pattern matching follows java regular
expressions. check out this link for documentation
显示表的结构信息
hive> describe invites;
shows the list of columns
更新表的名称:
hive> alter table source rename to target;
添加新一列
hive> alter table invites add columns (new_col2 int comment 'a comment');
删除表:
hive> drop table records;
删除表中数据,但要保持表的结构定义
hive> dfs -rmr /user/hive/warehouse/records;
从本地文件加载数据:
hive> load data local inpath '/home/hadoop/input/ncdc/micro-tab/sample.txt'
overwrite into table records;
显示所有函数:
hive> show functions;
查看函数用法:
hive> describe function substr;
查看数组、map、结构
hive> select col1[0],col2['b'],col3.c from complex;
其他同oracle相同
hive的元数据是存放在mysql中的,首先需要安装mysql数据库;然后修改hive配置文件,主要是数据库用户、密码、hadoop文件存放的路径等等。--请看mysql的环境搭建
如对本文有疑问, 点击进行留言回复!!
HBase Filter 过滤器之FamilyFilter详解
去 HBase,Kylin on Parquet 性能表现如何?
如何找到Hive提交的SQL相对应的Yarn程序的applicationId
网友评论