解决方案核心思想:
增加 临时写入文件数目,对众多临时文件文件进行随机读写,以降低并发的可能性。
实例:
在对用户访问日志进行记录时,这种方案似乎被采用的比较多。
先前需要定义一个随机空间,空间越大,并发的的可能性就越小,这里假设随机读写空间为[1-500],那么我们的日志文件的分布就为log1~到log500不等。每一次用户访问,都将数据随机写到log1~log500之间的任一文件。在同一时刻,有2个进程进行记录日志,A进程可能是更新的log32文件,而B进程呢?则此时更新的可能就为log399.要知道,如果要让B进程也操作log32,概率基本上为1/500,差不多约等于零。在需要对访问日志进行分析时,这里我们只需要先将这些日志合并,再进行分析即可。使用这种方案来记录日志的一个好处时,进程操作排队的可能性比较小,可以使进程很迅速的完成每一次操作。
php代码实例:
//报错屏蔽设置
date_default_timezone_set('PRC');//设置默认时区
$randam1 = rand(1,10);
$randam2 = rand(1,10);
$file1 = "spark_sql_data"."_".$randam1.".json";
$file2 = "spark_sql_tanzhen"."_".$randam2.".json";
$fp1 = fopen($file1,"a+");
$fp2 = fopen($file2,"a+");
//..........
//中间逻辑代码
//..........
flock($fp1,LOCK_EX);
fwrite($fp1,$detail_infor_json);
flock($fp1,LOCK_UN);
flock($fp2,LOCK_EX);
fwrite($fp2,$tanzhen_infor_json);
flock($fp2,LOCK_UN);
fclose($file1);
fclose($file2);
?>
完整代码展示(高端玩家选读):
set('PRC');//设置默认时区
$randam1 = rand(1,10);
$randam2 = rand(1,10);
$file1 = "spark_sql_data"."_".$randam1.".json";
$file2 = "spark_sql_tanzhen"."_".$randam2.".json";
$fp1 = fopen($file1,"a+");
$fp2 = fopen($file2,"a+");
/* 本例用到的数据:
data={"id":"0029c591","mmac":"5e:cf:7f:29:c5:91","rate":"1","wssid":"datasky1","wmac":"00:0c:43:76:20:59","time":"Tue Feb 21 08:13:31 2017", "lat":"30.747988","lon":"103.973152","addr":"四川省成都市郫县犀浦镇两河新街;围城南路与兴业南街路口东","data":[{"mac":"a4:56:02:61:7f:57","rssi":"-91 ","range":"91.5"},{"mac":"8c:a6:df:62:2d:3d","rssi":"-93","range":"108.5"},{"mac":"a4:56:02:71:be:b3","rssi":"-96","range":"140.1"},{"mac":" cc:34:29:97:4d:0d","rssi":"-95","range":"128.6"},{"mac":"44:33:4c:aa:71:82","rssi":"-94","range":"118.1"},{"mac":"b0:48:7a:5a:10:f8","rssi": "-86","range":"59.7"},{"mac":"a8:57:4e:9d:ca:d8","rssi":"-96","range":"140.1"},{"mac":"5e:cf:7f:93:3d:0e","rssi":"-56","range":"4.6"},{"mac ":"5e:cf:7f:93:3d:0f","rssi":"-58","range":"5.5"},{"mac":"5e:cf:7f:93:3d:10","rssi":"-63","range":"8.4"},{"mac":"5e:cf:7f:93:3d:0b","rssi":" -68","range":"12.9"},{"mac":"5e:cf:7f:93:3d:0c","rssi":"-53","range":"3.5"},{"mac":"5e:cf:7f:93:3d:0d","rssi":"-69","range":"14.0"},{"mac":" e4:f3:f5:24:2c:d8","rssi":"-89","range":"77.1"},{"mac":"14:cf:92:8a:8f:f0","rssi":"-96","range":"140.1"}]}POST /dsky HTTP/1.0*/
// 参数定义
$flow_range = 10;
$customer_range = 3;
$insert_interval = 60;//插入相同用户的时间间隔
//获取来自探针通过 post传值方式 传过来的数据
$data=$_POST['data'];
//解析数据
$obj=json_decode($data,TRUE);
$id = $obj['id'];
$mmac = $obj['mmac'];
$rate = $obj['rate'];
$wssid = $obj['wssid'];
$wmac = $obj['wmac'];
//$time = $obj['time'];
$time = time();
$lat = $obj['lat'];
$lon = $obj['lon'];
$addr = $obj['addr'];
$detail_data = $obj['data'];
//--得到 Json_list 数组长度
$num=count($obj['data']);
$i=0;//循环变量
$j=0;//在店人数统计
$k=0;//人流量统计
$detail_infor_json = "";
//--遍历数组,将对应信息输出
for ($i;$i<$num;$i++) {
$mac=$detail_data [$i]["mac"];
$rssi=$detail_data [$i]["rssi"];
$range=pow(10,(float)((int)abs($rssi)-59)/(10*2.0));
$ts=$detail_data [$i]["ts"];
$tmc=$detail_data [$i]["tmc"];
$tc=$detail_data [$i]["tc"];
$ds=$detail_data [$i]["ds"];
$essid1=$detail_data [$i]["essid1"];
$essid2=$detail_data [$i]["essid2"];
$essid3=$detail_data [$i]["essid3"];
$essid4=$detail_data [$i]["essid4"];
$essid5=$detail_data [$i]["essid5"];
$essid6=$detail_data [$i]["essid6"];
if((int)$range<= $flow_range&&(int)$range>=$customer_range){
$k++;//客流量统计
}
//若检测到信号距离小于设定值,则认定为在店有效数据,即插入数据库
if((float)$range<= $customer_range){
$j++;//在店人数统计
$detail_info["tanzhen_id"] = $id ;
$detail_info["time"] = $time ;
$detail_info["mac"] = $mac ;
$detail_info["rssi"] = $rssi ;
$detail_info["range"] = number_format($range,2) ;
$detail_info["ts"] = $ts ;
$detail_info["tmc"] = $tmc ;
$detail_info["tc"] = $tc ;
$detail_info["ds"] = $ds ;
$detail_info["essid1"] = $essid1 ;
$detail_info["essid2"] = $essid2 ;
$detail_info["essid3"] = $essid3 ;
$detail_info["essid4"] = $essid4 ;
$detail_info["essid5"] = $essid5 ;
$detail_info["essid6"] = $essid6 ;
$detail_infor_json = $detail_infor_json.json_encode($detail_info,TRUE)."\n";
}
}
flock($fp1,LOCK_EX);
fwrite($fp1,$detail_infor_json);
flock($fp1,LOCK_UN);
$tanzhen_info["id"] = $id ;
$tanzhen_info["mmac"] = $mmac ;
$tanzhen_info["rate"] = $rate ;
$tanzhen_info["wssid"] = $wssid ;
$tanzhen_info["time"] = $time ;
$tanzhen_info["lat"] = $lat ;
$tanzhen_info["lon"] = $lon ;
$tanzhen_info["addr"] = $addr ;
$tanzhen_info["flow_num"] = $k ;
$tanzhen_info["customer_num"] = $j ;
$tanzhen_infor_json = json_encode($tanzhen_info,TRUE);
$tanzhen_infor_json=$tanzhen_infor_json."\n";
flock($fp2,LOCK_EX);
fwrite($fp2,$tanzhen_infor_json);
flock($fp2,LOCK_UN);
fclose($file1);
fclose($file2);
?>