天天看點

mongodb php 日志分析,JavaScript_Node.js和MongoDB實作簡單日志分析系統,在最近的項目中,為了便于分 - phpStudy...

jqPlot Chart 。首先使用一個靜态的HTML頁面,用來作為圖形顯示的容器:

Rendezvous Monitor System

幾乎是jqPlot的示例中的完整拷貝,好吧,我承認我太懶了。

下面是看用來顯示生成圖形的chart.js:

// Store all chart drawing function, if we want to disable one chart, only need

// comment the push line when putting fucntion into the array.

var draws = [];

document.write('

var drawUserActionTop10Chart = function(){

if(!$("#userActionTop10Chart").attr('class')){

$("#userActionTop10Chart").attr('class', 'small_chart');

}

$.ajax({

async:false,

url: '/userActionTop10',

dataType:'json',

cache: false,

success:function(data){

try{

$('#userActionTop10Chart').html('');

$.jqplot('userActionTop10Chart', [data.count], {

title: "TOP 10 User Action",

seriesDefaults:{

renderer:$.jqplot.BarRenderer,

rendererOptions: {fillToZero: true},

pointLabels: {

show:true,

ypadding:1

}

},

axesDefaults:{

tickRenderer:$.jqplot.CanvasAxisTickRenderer,

tickOptions: {

angle: -30,

fontSize: '12px'

}

},

axes: {

xaxis: {

renderer: $.jqplot.CategoryAxisRenderer,

ticks: data.action

},

yaxis: {

pad: 1.05

}

}

});

}catch(e){

//alert(e.message);

}

}

});

}

draws.push('drawUserActionTop10Chart');

//Put your chart drawing function here

//1. insert a div for the chart

//2. implement the function drawing chart

//3. push the function name into the array draws

// Draw all charts

var drawAllCharts = function(){

for(var i = 0; i < draws.length; i ++){

eval(draws[i] + "()");

}

//Recall itself in 5 minute.

window.setTimeout(drawAllCharts, 5 * 60 * 1000);

}

//

$(function(){

drawAllCharts();

});

伺服器端和用戶端的代碼都有了,那就跑起來看效果吧:

好像忘了什麼?日志的分析代碼。

三、使用MongoDB 增量式MapReduce實作日志分析

在MongoDB的文檔中有關于Incremental MapReduce的介紹。剛開始一直以為MongoDB實作Streaming處理,可以自動執行增量式的MapReduce。最後發現原來是我了解有誤,文檔裡并沒有寫這一點,隻是說明了如何設定才能增量執行MapReduce。

為了友善,我把MapReduce使用MongoDB的JavaScript寫在了單獨的js檔案中,然後通過crontab定時執行。stats.js的代碼:

var action_count_map = function(){

emit(this.action, {action:this.action, count:1});

}

var action_count_reduce = function(key, values){

var count = 0;

values.forEach(function(value){

count += value.count;

});

return {action:key, count : count};

}

db.log.mapReduce(action_count_map, action_count_reduce, {query : {'action_count' : {$ne:1}},out: {reduce:'action_count'}});

db.log.update({'action_count':{$ne:1}}, {$set:{'action_count':1}}, false, true);

思路很簡單:

1. 在map中将每個action通路次數設為1

2. reduce中,統計相同action的通路次數

3. 執行mapReduce。指定了查詢為‘action_count'不等于1,也就是沒有執行過該統計;将結果存儲在‘action_count'集合,并且使用reduce選項表示該結果集作為下次reduce的輸入。

4. 在目前所有日志記錄設定'action_count'的值為1,表示已經執行過該統計。不知道這種是否會造成沒有還沒有統計過的記錄也被更新??望有經驗的大俠賜教!

定時執行stats.js的shell:

*/5 * * * * root cd /root/log; mongo localhost:27017/log stats.js

好了,這就是全部的代碼,沒有什麼特别玄妙的地方,不過Node.js真的是個好東西。相關閱讀:

C#中GraphicsPath的Flatten方法用法執行個體

Android 簡單的照相機程式的執行個體代碼

Android自定義元件ListPopWindow

淺談c++中的輸入輸出方法

js驗證電話号碼手機号碼的正規表達式

javascript中取前n天日期的兩種方法分享

php中curl、fsocket、file_get_content三個函數的使用比較

C#中委托和事件的差別執行個體解析

win7更新到win10正式版後怎麼再重新傳回win7系統?

C#使用Ado.net讀取Excel表的方法

ThinkPHP獨立分組使用的注意事項

Windows中使用指令建立和使用IP安全政策(IPSec)示例

SSH架構網上商城項目第9戰之添加和更新商品類别功能實作

簡介AngularJS中$http服務的用法