hadoop读写mongodemo
hadoop读写mongodemo
caolinsheng 发表于3年前
hadoop读写mongodemo
  • 发表于 3年前
  • 阅读 34
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

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 ); 
 }

}

共有 人打赏支持
粉丝 4
博文 9
码字总数 2532
×
caolinsheng
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: