hadoop读写mongodemo

MongoMapper.java

public class MongoMapper extends Mapper<Object,BSONObject, IntWritable, Text> {

 @Override
 protected void map(Object key, BSONObject value,
   org.apache.hadoop.mapreduce.Mapper.Context context)
   throws IOException, InterruptedException {
  
  double id=(Double)value.get("myid");
  String name=(String)value.get("name");
  
  context.write(new IntWritable((int)id),new Text(name));
 }
}

 

MongoReducer.java

public class MongoReducer extends Reducer<IntWritable,DoubleWritable, IntWritable, BSONWritable> {
 protected void reduce(IntWritable key, Iterable<DoubleWritable> values,
   org.apache.hadoop.mapreduce.Reducer.Context context)
   throws IOException, InterruptedException {

  BasicBSONObject output = new BasicBSONObject();
  int count=0;
        for (Iterator iterator = values.iterator(); iterator.hasNext();) {
   count++;
   
  }
        output.put("count", count);
        context.write( key, new BSONWritable( output ) );

 }
}

 

RunTool.java

class RunTool extends PluginUtil{
  
 public static void main(String[] args) throws Throwable{
     System.setProperty("HADOOP_USER_NAME","hadoop");
        
   final JobConf conf = new JobConf(); 
      // 定义MongoDB数据库的输入与输出表名,这里是调用本地的MongoDB,默认端口号为27017 
         MongoConfigUtil.setInputURI( conf, "mongodb://192.168.4.73/foo.person" ); 
         MongoConfigUtil.setOutputURI( conf, "mongodb://192.168.4.73/foo.out" ); 
         MongoConfigUtil.setAuthURI(conf, "mongodb://mongo:mongo@192.168.4.73/admin");
         System.out.println( "Conf: " + conf ); 
   
         conf.set("fs.default.name", "hdfs://hadoop1:9000");
     //  conf.set("hadoop.job.user", "hadoop");
       conf.set("mapred.job.tracker", "hadoop1:9001");
       conf.set("dfs.permissions","false");
       String jarName = runonhadoop().toString();
         conf.setJar(jarName);
 
         final Job job = new Job( conf , "mongohadoop" );   
         job.setMapperClass( MongoMapper.class );  
         job.setReducerClass( MongoReducer.class );  
    
      // 定义Mapper与Reduce的输出key/value的类型 
         job.setOutputKeyClass( IntWritable.class ); 
         job.setOutputValueClass( BSONWritable.class ); 
   
         job.setMapOutputKeyClass(IntWritable.class);
         job.setMapOutputValueClass(DoubleWritable.class);
   
      // 定义InputFormat与OutputFormat的类型 
         job.setInputFormatClass( MongoInputFormat.class ); 
         job.setOutputFormatClass( MongoOutputFormat.class );  
         System.exit( job.waitForCompletion( true ) ? 0 : 1 ); 
 }

}

转载于:https://my.oschina.net/caolinsheng/blog/281118

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值