電商大資料分析平臺(三)nginx配置及flume讀取日誌資訊
阿新 • • 發佈:2018-12-10
一、nginx配置
在本專案中nginx的作用只是接收客戶端傳送的事件,並將相應的session寫入日誌檔案中,所以配置較為簡單,只需要配置寫入的日誌檔案和寫入的格式
1.地址配置
server { listen 80; location = /index{ index index.html index.html; root /data/www/vhost1; } location /log.gif{ root /data/www/vhost1; access_log /var/log/nginx/access.log main; }
2.日誌格式設定
log_format main '$remote_addr - $http_referer - $server_addr - $request_uri';
二、flume配置
flume在這裡主要是對nginx產生的access.log檔案進行監聽,當有session記錄時flume將其提取並寫入HDFS中,flume的source端用的是exec的source,即通過命令監控,命令的話為tail -F /var/log/nginx/access.log即持續監控日誌檔案尾部。sink部分用的就是hdfs的sink,設定寫入路徑為hdfs的flume資料夾下的以年月日作為名稱的資料夾中,再設定一些自己的引數即可,flume配置如下
# example.conf: A single-node Flume configuration # Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = exec a1.sources.r1.command = tail -F /var/log/nginx/access.log # Describe the sink a1.sinks.k1.type = hdfs a1.sinks.k1.hdfs.path = hdfs://master:8020/flume/%Y-%m-%d a1.sinks.k1.hdfs.rollsize = 10240000 a1.sinks.k1.hdfs.rollInterval = 0 a1.sinks.k1.hdfs.fileType = DataStream a1.sinks.k1.hdfs.idleTimeout = 0 a1.sinks.k1.hdfs.useLocalTimeStamp = true # Use a channel which buffers events in memory a1.channels.c1.type = memory a1.channels.c1.capacity = 1000 a1.channels.c1.transactionCapacity = 100 # Bind the source and sink to the channel a1.sources.r1.channels = c1 a1.sinks.k1.channel = c1
三、測試執行
- 首先先在windows上新建一個web測試專案,最簡單的就可以,然後在WebContent資料夾下新建一個叫js的資料夾,把寫的js sdk放進去,在web專案的jsp檔案中加上這麼一行程式碼
<script type="text/javascript" src="js/analytics.js"></script>
這樣一來,使用者每次點選該頁面都會自動呼叫js的sdk傳送事件資訊給nginx
-
啟動主結點上的nginx伺服器,持續監測access.log檔案,即tail -F /var/log/nginx/access.log
-
在瀏覽器中輸入我們新建好的web專案的地址,點選進入內嵌sdk的頁面,這是發現access.log檔案增加了這樣的資訊
192.168.174.1 - http://localhost:8080/WebTest/ - 192.168.174.141 - /log.gif?en=e_pv&p_url=http%3A%2F%2Flocalhost%3A8080%2FWebTest%2F&tt=Insert%20title%20here&ver=1&pl=website&sdk=js&u_ud=fd309e7b-5740-3930-00f5-e7f529c26a72&u_mid=8059a633-e4ac-afe4-0d5e-e44ba2aed824&c_time=1537066699170&l=zh-CN&b_iev=Mozilla%2F5.0%20(Windows%20NT%2010.0%3B%20WOW64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F68.0.3440.106%20Safari%2F537.36&b_rst=1536*864
說明我們的sdk已經成功地想nginx伺服器傳送了session。
-
啟動hadoop
-
啟動flume
flume-ng agent –conf ../conf –conf-collect.conf(之前配置flume的檔名稱) ../conf/flume-spool.conf –name agent -Dflume.root.logger=INFO,console
-
這時我們再點選幾下web頁面,然後進入自己的hdfs中,發現新建了一個年-月-日為名的資料夾,上面的session資訊已經被寫到裡面的檔案中了。
-
測試成功,可以進行下一步了,也就是通過mapreduce對hdfs中的資料進行ETL並且寫到hbase中。