下载
https://github.com/mongodb/mongo-hadoop/releases
解压到/home/kevin/hadoop/hadoop/share/mongo-hadoop-2.2-1.2.0
(其实是任意,真正分布式的时候要注意这个jar包的问题)
Mapreduce结果输出到MongDB
要修改的代码(input类似 ps:extends Mapper<Object,BSONObject, IntWritable,DoubleWritable>)
//FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); MongoConfigUtil.setOutputURI(conf, "mongodb://localhost:27017/test.out"); job.setOutputFormatClass(MongoOutputFormat.class);
注意:
import的不要是 com.mongodb.hadoop.mapred.MongoInputFormat;,而是
import com.mongodb.hadoop.MongoOutputFormat;
import com.mongodb.hadoop.MongoInputFormat;
import com.mongodb.hadoop.util.MongoConfigUtil;
BasicBSONObject
output
=
new
BasicBSONObject
();
output
.
put
(
"count"
,
count
);
output
.
put
(
"avg"
,
avg
);
output
.
put
(
"sum"
,
sum
);
pContext
.
write
(
pKey
,
new
BSONWritable
(
output
));