看word版的图片更加丰富哦!博客,需要用插入功能,才能上传图片,所以,博客中的图片不全,全的放在百度网盘里。
http://pan.baidu.com/s/1o8E9SRS
1 下载解压hive
输入网址:http://apache.claz.org/hive/stable-2/,包含了两个文件:
apache-hive-2.1.1-bin.tar.gz
apache-hive-2.1.1-src.tar.gz
2 使用xftp上传至虚拟机
3 安装
解压目标文件
tar -xvf apache-hive-2.1.1-bin.tar.gz
后面没有指定目录,即当前目录即可。
将解压好的文件放到有hadoop的目录。
mv apache-hive-2.1.1-bin /usr/local
到指定目录,
cd /usr/local
ls
重命名apache-hive-2.1.1-bin为hive
mv apache-hive-2.1.1-bin hive
改简单些
4 环境配置
HADOOP_HOME
HIVE_HOME
把Hadoop s bin 目录加到 path
vim /etc/profile
export HADOOP_HOME=/usr/local/hadoop
export HIVE_HOME=/usr/local/hive
export PATH=$PATH:$HIVE_HOME/bin
:wq
为了使修改起作用
运行
source /etc/profile
显示配置
echo $HIVE_HOME
5 修改配置文件hive-site.xml
复制:
cp hive-default.xml.template hive-site.xml
再修改hive-site.xml文件
由于此文件较大,在notepad中修改会比较方便,使用xftp中打开。
修改(用查找功能快速定位)
<name>hive.metastore.schema.verification</name>
<value>false</value>
将true改为false
创建目录
在/usr/local/hive下,创建目录tmp
并且,对hive-site.xml操作,将${system:java.io.tmpdir}都替换为/usr/local/hive/tmp。
并且,对hive-site.xml操作,将${system:user.name}都替换为root。
6 执行schematool命令
schematool -initSchema -dbType derby
显示
Initialization script completed
schemaTool completed
说明成功
并且,可以在hive文件下多出文件metastore_db
db其实是一个数据库。不过,运行schematool-initSchema -dbType derby的命令需要在一个目录下,不能一会儿在这里,一会儿在那里执行。这里,就暂定为hive目录下即可。在实际工作中,常把mysql作为metastore的数据。
7启动hive
有一个前提,机器的dfs和yarn都已经启动
并且,在hive目录下,即/cd/usr/local/hive
hive
8 一些尝试
hive> show databases;
9 创建一个表(记住句尾的;号哦!)
create table wordcount(line string);
show tables;
desc wordcount;
~~~~~~~~~~
hive> dfs -ls /tmp/hive/root;
hive> dfs -ls /usr/hive/warehouse/;
执行完hive创建表后,会在dfs目录下,创建/tmp/hive/root和/usr/hive/warehouse/目录。
即结果文件存在了hdfs中
~~~~~~~~~~
note:
加载dfs数据到表的过程,实际上并没有移动数据,而是使用户可以通过表来访问这些数据了-------映射
10 load data
load data inpath ‘/wcinput/’ overwrite into table wordcount;
11 split
select split(line,' ') from wordcount;
12 explode
hive> selectexplode(split(line,' ')) from wordcount;
13 as word
hive> select explode(split(line,' ')) as word from wordcount;
14 wordcount
hive> select word, count(1) as count from (select explode(split(line,'')) as word from wordcount) w group by word;
exit;
退出hive
总的过程,说起来就是,所以运行hive,在hive中创建一张表,再将dfs中的数据load到表中,后就可以进行操作了。
15 搜狗热搜版的实例
准备工作:把sougou.dic文件存放到hdfs下
(1)通过xftp放到master机器上的/usr/local下
(2)将/usr/local下的sougou.dic文件上传到dfs的根目录下
OK,准备工作完成!
15.1 创建一张表
此表有四个关键字,为时间,ID,word,url
hive> create table sgr(qtime string, qid string, qword string, urlstring) row format delimited fields terminated by ',';
desc sgr;
15.2 加载数据
load data inpath'/sougou.dic' into table sgr;
15.3 查看记录条数
select count(*) from sgr;
结果显示,一共有1724264条记录。
15.4 完成对qword的排序
select qword作为一张表,并count,再group,出来的表命名为sgr_result
create table sgr_results asselect keyword, count(1) as count from (select qword as keyword from sgr) tgroup by keyword order by count desc;
15.5 结果查看
取前十条查看即可
select * from sgr_resultslimit 10;
The end 22:24 2017/11/4