Hadoop安装: http://www.powerxing.com/install-hadoop/
Hadoop集群配置: http://www.powerxing.com/install-hadoop-cluster/
Hive安装: https://chu888chu888.gitbooks.io/hadoopstudy/content/Content/8/chapter0807.htmlhtml
安装具体教程请看上面连接,本地测试只用了单机配置,集群配置(后面的flume用到)看上面的详细连接, 由于以前没有接触过java的相关,这里说下遇到的几个问题.java
Hadoop
和Hive
的1.x和2.x版本要对应 JAVA/Hadoop
相关的环境变量配置,习惯了PHP的童鞋在这块可能容易忽略
启动Hadoop
提示Starting namenodes on []
,namenodes为空,是由于没有指定ip或端口,修改hadoop/core-site.xml
以下node
<configuration> <property> <name>dfs.namenode.rpc-address</name> <value>127.0.0.0:9001</value> </property> </configuration>
安装完成后输入jps能够查看到NameNode,DataNode等mysql
swoole和workerman都有简单版本实现的数据监控,包括上报,接收,存储,展现, 主要使用udp上传(swoole版本已升级为tcp长链接),redis缓存,文件持久化,highcharts展现,能够做为思路参考 swoole-statistics
: https://github.com/smalleyes/statistics workerman-statistics
: https://github.com/walkor/workerman-statistics
本例使用swoole提供的接口实现UDP传输,由于上报数据是必定程度能够容错,因此选择UDP效率优先
接收数据临时存储在Redis中,每隔几分钟刷到文件中存储,文件名按模块和时间分割存储,字段|分割(后面与hive对应)android
建立Hive数据表ios
根据文件数据格式编写Hive数据表, TERMINATED BY字段与前面文件字段分隔符想对应
对表按日期分区PARTITIONED BYgit
CREATE TABLE login ( time int comment '登录时间', type string comment '类型,email,username,qq等', device string comment '登录设备,pc,android,ios', ip string comment '登录ip', uid int comment '用户id', is_old int comment '是否老用户' ) PARTITIONED BY ( `date` string COMMENT 'date' ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|';
定时(Crontab)建立hadoop分区github
hive -e "use web_stat; alter table login add if not exists partition (date='${web_stat_day}')"
转存web
Flume监听文件目录,将数据传输到能访问Hdfs集群的服务器上,这里传输到了224机器的7000端口
#agent3表示代理名称 login agent3.sources=source1 agent3.sinks=sink1 agent3.channels=channel1 配置source1
配置source1
agent3.sources.source1.type=spooldir agent3.sources.source1.spoolDir=/data/releases/stat/Data/10001/ agent3.sources.source1.channels=channel1 agent3.sources.source1.fileHeader = false
配置sink1
agent3.sinks.sink1.type=avro agent3.sinks.sink1.hostname=192.168.23.224 agent3.sinks.sink1.port=7000 agent3.sinks.sink1.channel=channel1
配置channel1
agent3.channels.channel1.type=file agent3.channels.channel1.checkpointDir=/data/flume_data/checkpoint_login agent3.channels.channel1.dataDirs=/data/flume_data/channelData_login
启动flume
加到supervisor守护进程
/home/flume/bin/flume-ng agent -n agent3 -c /home/flume/conf/ -f /home/flume/conf/statistics/login_flume.conf -Dflume.root.logger=info,console
224机器监听7000端口,将数据写到hdfs集群
agent1表示代理名称
agent4.sources=source1 agent4.sinks=sink1 agent4.channels=channel1
配置source1
agent4.sources.source1.type=avro agent4.sources.source1.bind=192.168.23.224 agent4.sources.source1.port=7000 agent4.sources.source1.channels=channel1
配置sink1
agent4.sinks.sink1.type=hdfs agent4.sinks.sink1.hdfs.path=hdfs://hdfs/umr-ubvzlf/uhiveubnhq5/warehouse/web_stat.db/login/date\=%Y-%m-%d agent4.sinks.sink1.hdfs.fileType=DataStream agent4.sinks.sink1.hdfs.filePrefix=buffer_census_ agent4.sinks.sink1.hdfs.writeFormat=TEXT agent4.sinks.sink1.hdfs.rollInterval=30 agent4.sinks.sink1.hdfs.inUsePrefix = . agent4.sinks.sink1.hdfs.rollSize=536870912 agent4.sinks.sink1.hdfs.useLocalTimeStamp = true agent4.sinks.sink1.hdfs.rollCount=0 agent4.sinks.sink1.channel=channel1
配置channel1
agent4.channels.channel1.type=file agent4.channels.channel1.checkpointDir=/data/flume_data/login_checkpoint agent4.channels.channel1.dataDirs=/data/flume_data/login_channelData
启动
加到supervisor守护进程
/usr/local/flume/bin/flume-ng agent -n agent4 -c /usr/local/flume/conf/ -f /usr/local/flume/conf/statistics/login_flume.conf -Dflume.root.logger=info,console
经过Thrift的PHP扩展包调用Hive,编写类SQL的HQL转换为MapReduce任务读取计算HDFS里的数据, 将结果存储在MySQL中
php-thrift-client下载地址: https://github.com/garamon/php-thrift-hive-client
define('THRIFT_HIVE' , ROOT .'/libs/thrift'); $GLOBALS['THRIFT_ROOT'] = THRIFT_HIVE . '/lib'; require_once $GLOBALS['THRIFT_ROOT'] . '/packages/hive_service/ThriftHive.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/transport/TSocket.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/protocol/TBinaryProtocol.php'; require_once THRIFT_HIVE . '/ThriftHiveClientEx.php'; $transport = new \TSocket('127.0.0.1', 10000); $transport->setSendTimeout(600 * 1000); $transport->setRecvTimeout(600 * 1000); $this->client = new \ThriftHiveClientEx(new \TBinaryProtocol($transport)); $this->client->open(); $this->client->execute("show databases"); $result = $this->client->fetchAll(); var_dump($result); $this->client->close();
HQL语法说明: https://chu888chu888.gitbooks.io/hadoopstudy/content/Content/8/chapter0803.html
注意的是,尽可能要将HQL语句能转换为MapReduce任务,否则没利用上Hadoop的大数据计算分析,就没意义
例以下面的逻辑,取出来在内存里分析,这样的逻辑尽可能避免,由于sql在hive里执行就是普普统统的数据,没有转换为mapreduce
select * from login limit 5; // php处理 $count = 0; foreach ($queryResult as $row) { $count ++; }
一次性转换为MapReduce,利用Hadoop的计算能力
select type,count(*) from login group by type; // 这样就用到了
建表使用了PARTITIONED BY分区断言后,查询就能够利用分区剪枝(input pruning)的特性,可是断言字段必须离where关键字最近才能被利用上
// 如前面的login表使用到了date分区断言,这里就得把date条件放在第一位
select count(*) from login where date='2016-08-23' and is_old=1;
Hive中不支持等值连表,以下
select * from dual a,dual b where a.key = b.key;
应写为:
select * from dual a join dual b on a.key = b.key;
Hive中不支持insert,并且逻辑上也不容许,应为hadoop是咱们用来作大数据分析,而不该该做为业务细分数据
这一步就简单了,读取MySQL数据,使用highcharts等工具作各类展现,也能够用crontab定时执行php脚本发送日报,周报等等
最近看一些资料和别人沟通发现,清洗数据这一步彻底不用php,能够专一于HQL实现清洗逻辑,将结果保存在hadoop中,再用Sqoop将hadoop数据和MySQL数据同步。即简化了流程,免去mysql手工插入,又作到了数据更实时,为二次清洗逻辑的连表HQL作了铺垫