Mongodb MapReduce for PHP 操作
下面让我们使用group操作,根据group_id分组,汇总计算count,用MapReduce实现:<?php
ini_set('mongo.native_long', 1);
$instance = new Mongo();
$instance = $instance->selectCollection('test', 'test');
for ($i = 0; $i < 10; $i++) {
$instance->insert(array(
'group_id' => rand(1, 5),
'count' => rand(1, 5),
));
}
?>
参数说明:db.runCommand(
{ mapreduce : <collection>,
map : <mapfunction>,
reduce : <reducefunction>,
out : <see output options below>
[, query : <query filter object>]
[, sort : <sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces>]
[, limit : <number of objects to return from collection, not supported with sharding>]
[, keeptemp: <true|false>]
[, finalize : <finalizefunction>]
[, scope : <object where fields go into javascript global scope >]
[, jsMode : true]
[, verbose : true]
}
);
<?php
ini_set('mongo.native_long', 1);
$instance = new Mongo();
$instance = $instance->selectDB('test');
$map = '
function() {
emit(this.group_id, this.count);
}
';
$reduce = '
function(key, values) {
var sum = 0;
for (var index in values) {
sum += values[index];
}
return sum;
}
';
$result = $instance->command(array(
'mapreduce' => 'test',
'map' => $map,
'reduce' => $reduce,'out' => 'test_res'
));
$resData = new Mongo();
$resData = $instance->selectDB('test_res');$result = $resData->find();//$result = iterator_to_array($instance->{$result['result']}->find());
var_dump($result);
?>
图片服务器和WEB应用服务器相分离的简单方案
一、两个域名:www.domain.com和img.domain.com
二、在www域名的服务器中上传文件:
up.html
Html代码
1.<form name="myform" action="save.php?action=ok" method="POST" enctype="multipart/form-data" >
2.<input type="file" name="itname" value="">
3.<input type="submit" name="tt" value="upit">
4.</form>
save.php
Php代码
1.if(@$_GET['action']=="ok")
2.{
3.$picname="upfile/".rand().".jpg";
4.move_uploaded_file($_FILES['itname']['tmp_name'],$picname);
5.}
6.
7.$ftp_server="img.domain.com";//要连接的服务器域名
8.$con=ftp_connect($ftp_server);//连接FTP服务器
9.ftp_login($con,"username","password");//发送用户名和密码
10.$tempstate=ftp_put($con,"drckness.jpg",$picname,FTP_BINARY)//以二进制方式上传文件
11.if($tempstate){
12.echo "上传成功";
13.unlik($picname);//删除www服务器上的图片当然你也可以保留
14.}else{
15.echo "上传未成功";
16.
17.};
18.ftp_quit($conn);// 关闭联接,不然会一直开着占用资源
以上只是说明如何实现图片和WEB分离的原理。在实际开发中,还要进行如:上传检测,水印,缩略图生成,冗余判断,延时间操作,图片路径记录等操作。
但只要明白了原理其它的应该不在话下了吧:)