PHP+Hadoop實現資料統計分析

來源:互聯網
上載者:User

標籤:進程   容錯   分隔字元   workerman   mysq   訪問hdfs   類型   res   androi   

 

記一次完全獨立完成的統計分析系統的搭建過程,主要用到了PHP+Hadoop+Hive+Thrift+Mysql實現

安裝

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.html

安裝具體教程請看上面連結,本地測試只用了單機配置,叢集配置(後面的flume用到)看上面的詳細連結, 因為之前沒有接觸過java的相關,這裡說下遇到的幾個問題.

  • Hadoop和Hive的1.x和2.x版本要對應
  • JAVA/Hadoop相關的環境變數配置,習慣了PHP的童鞋在這塊可能容易忽略
  • 啟動Hadoop提示Starting namenodes on [],namenodes為空白,是因為沒有指定ip或連接埠,修改hadoop/core-site.xml如下
<configuration><property><name>dfs.namenode.rpc-address</name><value>127.0.0.0:9001</value></property></configuration>
  • 安裝完成後輸入jps可以查看到NameNode,DataNode等
上報和接收
  • 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對應)
資料轉存建立Hive資料表
  • 根據檔案資料格式編寫Hive資料表, TERMINATED BY欄位與前面檔案欄位分隔符號想對應
  • 對錶按日期分區PARTITIONED BY
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分區
hive -e "use web_stat; alter table login add if not exists partition (date=‘${web_stat_day}‘)"
轉存
  • Flume監聽檔案目錄,將資料轉送到能訪問Hdfs叢集的伺服器上,這裡傳輸到了224機器的7000連接埠
#agent3表示代理名稱 loginagent3.sources=source1agent3.sinks=sink1agent3.channels=channel1#配置source1agent3.sources.source1.type=spooldiragent3.sources.source1.spoolDir=/data/releases/stat/Data/10001/agent3.sources.source1.channels=channel1agent3.sources.source1.fileHeader = false#配置sink1agent3.sinks.sink1.type=avroagent3.sinks.sink1.hostname=192.168.23.224agent3.sinks.sink1.port=7000agent3.sinks.sink1.channel=channel1#配置channel1agent3.channels.channel1.type=fileagent3.channels.channel1.checkpointDir=/data/flume_data/checkpoint_loginagent3.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=source1agent4.sinks=sink1agent4.channels=channel1#配置source1agent4.sources.source1.type=avroagent4.sources.source1.bind=192.168.23.224agent4.sources.source1.port=7000agent4.sources.source1.channels=channel1#配置sink1agent4.sinks.sink1.type=hdfsagent4.sinks.sink1.hdfs.path=hdfs://hdfs/umr-ubvzlf/uhiveubnhq5/warehouse/web_stat.db/login/date\=%Y-%m-%dagent4.sinks.sink1.hdfs.fileType=DataStreamagent4.sinks.sink1.hdfs.filePrefix=buffer_census_agent4.sinks.sink1.hdfs.writeFormat=TEXTagent4.sinks.sink1.hdfs.rollInterval=30agent4.sinks.sink1.hdfs.inUsePrefix = .agent4.sinks.sink1.hdfs.rollSize=536870912agent4.sinks.sink1.hdfs.useLocalTimeStamp = trueagent4.sinks.sink1.hdfs.rollCount=0agent4.sinks.sink1.channel=channel1#配置channel1agent4.channels.channel1.type=fileagent4.channels.channel1.checkpointDir=/data/flume_data/login_checkpointagent4.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做了鋪墊

 

 

 

 

 

 

偉哥 

 

PHP+Hadoop實現資料統計分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.