上一篇文章分析了Hive1.2.2的安装,本节博主将分享Hive的体验&Hive服务端和客户端的使用方法。mysql
1、Hive与hadoop直接的关系web
Hive利用HDFS存储数据,利用MapReduce查询数据。sql
2、Hive与传统数据库对比shell
Hive | RDBMS | |
查询语言 | HQL | SQL |
数据存储 | HDFS | Raw Device or Local FS |
执行 | MapReduce、spark等 | Excutor执行引擎 |
执行延迟 | 高 | 低 |
处理数据规模 | 大 | 小 |
索引 | 0.8版本后加入位图索引 | 有完整的索引体系 |
总结:hive具备sql数据库的外表(包括sql命令行功能、sql语法等),但应用场景彻底不一样,hive只适合用来作批量数据统计分析。数据库
3、Hive的数据存储
a、Hive中全部的数据都存储在 HDFS 中,没有专门的数据存储格式(可支持Text,SequenceFile,ParquetFile,RCFILE等)
b、只须要在建立表的时候告诉 Hive 数据中的列分隔符和行分隔符,Hive 就能够解析数据。
c、Hive 中包含如下数据模型:DB、Table,External Table,Partition,Bucket。
db:在hdfs中表现为${hive.metastore.warehouse.dir}目录下一个文件夹
table:在hdfs中表现所属db目录下一个文件夹
external table:外部表, 与table相似,不过其数据存放位置能够在任意指定路径
普通表: 删除表后, hdfs上的文件都删了
External外部表删除后, hdfs上的文件没有删除, 只是把文件删除了
partition:在hdfs中表现为table目录下的子目录(如:日志按日期分区后,便于管理,且查询效率提高)
bucket:桶, 在hdfs中表现为同一个表目录下根据hash散列以后的多个文件, 会根据不一样的文件把数据放到不一样的文件中 (如:在进行笛卡尔积的时候,同一个id的数据必定在某个桶,效率会提高)apache
4、Hive使用方式centos
a、Hive交互shell服务器
bin/hiveapp
b、Hive thrift服务oop
thrift是一个跨平台的协议,只要客户端遵循thrift协议便可同hive交互;hive自带的thrift服务端bin/hiveserver2,客户端为bin/beeline
启动方式,(假如是在hadoop01上):
#启动为前台: bin/hiveserver2 #启动为后台: nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err &
启动成功后,能够在别的节点上用beeline去链接
方式(1)
#输入如下命令,回车,进入beeline的命令界面 hive/bin/beeline #输入命令链接hiveserver2,(hadoop01是hiveserver2所启动的那台主机名,端口默认是10000) beeline> !connect jdbc:hive2//hadoop01:10000
方式(2)
#启动就链接: bin/beeline -u jdbc:hive2://hadoop01:10000 -n hadoop
接下来就能够作正常sql查询了
方式(3)
[hadoop@centos-aaron-h1 ~]$ hive -e 'sql语句'
5、使用效果图
[hadoop@centos-aaron-h1 bin]$ ./hiveserver2
Last login: Wed Jan 23 07:07:45 2019 from 192.168.29.3 [hadoop@centos-aaron-h1 ~]$ ~/apps/apache-hive-1.2.2-bin/bin/beeline -u jdbc:hive2://centos-aaron-h1:10000 -n hadoop Connecting to jdbc:hive2://centos-aaron-h1:10000 Connected to: Apache Hive (version 1.2.2) Driver: Hive JDBC (version 1.2.2) Transaction isolation: TRANSACTION_REPEATABLE_READ Beeline version 1.2.2 by Apache Hive 0: jdbc:hive2://centos-aaron-h1:10000> show databases; +----------------+--+ | database_name | +----------------+--+ | default | | wcc_log | +----------------+--+ 2 rows selected (1.86 seconds) 0: jdbc:hive2://centos-aaron-h1:10000> use wcc_log; No rows affected (0.155 seconds) 0: jdbc:hive2://centos-aaron-h1:10000> show tables; +--------------+--+ | tab_name | +--------------+--+ | t_web_log01 | +--------------+--+ 1 row selected (0.07 seconds) 0: jdbc:hive2://centos-aaron-h1:10000> select * from t_web_log01; +-----------------+-------------------+--+ | t_web_log01.id | t_web_log01.name | +-----------------+-------------------+--+ | 1 | 张三 | | 2 | 李四 | | 3 | 王二 | | 4 | 麻子 | | 5 | 隔壁老王 | +-----------------+-------------------+--+ 5 rows selected (1.064 seconds) 0: jdbc:hive2://centos-aaron-h1:10000> select id , name from t_web_log01; +-----+-------+--+ | id | name | +-----+-------+--+ | 1 | 张三 | | 2 | 李四 | | 3 | 王二 | | 4 | 麻子 | | 5 | 隔壁老王 | +-----+-------+--+ 5 rows selected (0.077 seconds) 0: jdbc:hive2://centos-aaron-h1:10000> select id , name from t_web_log01 order by id desc; INFO : Number of reduce tasks determined at compile time: 1 INFO : In order to change the average load for a reducer (in bytes): INFO : set hive.exec.reducers.bytes.per.reducer=<number> INFO : In order to limit the maximum number of reducers: INFO : set hive.exec.reducers.max=<number> INFO : In order to set a constant number of reducers: INFO : set mapreduce.job.reduces=<number> INFO : number of splits:1 INFO : Submitting tokens for job: job_1548198552826_0001 INFO : The url to track the job: http://centos-aaron-h1:8088/proxy/application_1548198552826_0001/ INFO : Starting Job = job_1548198552826_0001, Tracking URL = http://centos-aaron-h1:8088/proxy/application_1548198552826_0001/ INFO : Kill Command = /home/hadoop/apps/hadoop-2.9.1/bin/hadoop job -kill job_1548198552826_0001 INFO : Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1 INFO : 2019-01-23 07:17:45,112 Stage-1 map = 0%, reduce = 0% INFO : 2019-01-23 07:17:52,354 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 1.22 sec INFO : 2019-01-23 07:18:04,679 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 2.13 sec INFO : MapReduce Total cumulative CPU time: 2 seconds 130 msec INFO : Ended Job = job_1548198552826_0001 +-----+-------+--+ | id | name | +-----+-------+--+ | 5 | 隔壁老王 | | 4 | 麻子 | | 3 | 王二 | | 2 | 李四 | | 1 | 张三 | +-----+-------+--+ 5 rows selected (38.171 seconds) 0: jdbc:hive2://centos-aaron-h1:10000>
注意:hive是能够设置远端登陆用户名和密码的,只是博主系统没设置,默认为hive启动用户hadoop,端口为10000. 看了博主的操做有没有种在使用mysql命令行的感受...
最后寄语,以上是博主本次文章的所有内容,若是你们以为博主的文章还不错,请点赞;若是您对博主其它服务器大数据技术或者博主本人感兴趣,请关注博主博客,而且欢迎随时跟博主沟通交流。