MapReduce读取HDFS,将结果写入MongoDB

链接地址   https://www.iteye.com/blog/superlxw1234-1879268

 

 

MapReduce读取HDFS,将结果写入MongoDB

hadoopmongodb 

参考:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-hadoop/

附件是我编译和下载好的jar包(hadoop版本:hadoop-0.20.2-cdh3u3),需要的可下载直接使用。

 

首先,编译MongoDB Adapter

1.下载源码:

https://github.com/mongodb/mongo-hadoop

 

2.修改build.sbt

hadoopRelease in ThisBuild := "cdh3"

 

3.编译:

./sbt package

 

4.编译好后

在target目录生成mongo-hadoop_cdh3u3-1.1.0-SNAPSHOT.jar

在core/target目录生成mongo-hadoop-core_cdh3u3-1.1.0-SNAPSHOT.jar

 

5.下载mongo-2.7.3.jar:

wget --no-check-certificate https://github.com/downloads/mongodb/mongo-java-driver/mongo-2.7.3.jar

 

6. 将这两个jar包拷贝至hadoop集群每个节点的$HADOOP_HOME/lib目录

cp mongo-2.7.3.jar $HADOOP_HOME/lib/

cp core/target/mongo-hadoop-core_cdh3u3-1.1.0-SNAPSHOT.jar $HADOOP_HOME/lib/

 

7.准备input文件:hadoop fs -put input.txt /tmp/input/

vi input.txt

Java代码 

 收藏代码

  1. Hello,MongoDB!  
  2. MongoDB  
  3. Hello  
  4. Hello,Hadoop  
  5. Hadoop with MongoDB  

 

8.wordcount程序:(我是在windows上通过eclipse连接hadoop的)

 

Java代码 

 收藏代码

  1. import java.io.*;  
  2. import java.util.*;  
  3.   
  4. import org.apache.commons.logging.*;  
  5. import org.apache.hadoop.conf.*;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.io.*;  
  8. import org.apache.hadoop.mapreduce.*;  
  9. import org.apache.hadoop.mapreduce.Mapper.Context;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. import org.bson.*;  
  12.   
  13. import com.mongodb.hadoop.*;  
  14. import com.mongodb.hadoop.util.*;  
  15.   
  16. public class WordCount {  
  17.   
  18.     public static class TokenizerMapper extends  
  19.             Mapper<Object, Text, Text, IntWritable> {  
  20.   
  21.         private final static IntWritable one = new IntWritable(1);  
  22.         private Text word = new Text();  
  23.   
  24.         public void map(Object key, Text value, Context context)  
  25.                 throws IOException, InterruptedException {  
  26.             StringTokenizer itr = new StringTokenizer(value.toString());  
  27.             while (itr.hasMoreTokens()) {  
  28.                 word.set(itr.nextToken());  
  29.                 context.write(word, one);  
  30.             }  
  31.         }  
  32.     }  
  33.   
  34.     public static class IntSumReducer extends  
  35.             Reducer<Text, IntWritable, Text, IntWritable> {  
  36.   
  37.         private final IntWritable result = new IntWritable();  
  38.   
  39.         public void reduce(Text key, Iterable<IntWritable> values,  
  40.                 Context context) throws IOException, InterruptedException {  
  41.   
  42.             int sum = 0;  
  43.             for (final IntWritable val : values) {  
  44.                 sum += val.get();  
  45.             }  
  46.             result.set(sum);  
  47.             context.write(key, result);  
  48.         }  
  49.     }  
  50.   
  51.     public static void main(String[] args) throws Exception {  
  52.   
  53.         final Configuration conf = new Configuration();  
  54.         conf.addResource(new Path("F:/lxw-hadoop/hdfs-site.xml"));  
  55.         conf.addResource(new Path("F:/lxw-hadoop/mapred-site.xml"));  
  56.         conf.addResource(new Path("F:/lxw-hadoop/core-site.xml"));  
  57.         conf.set("mapred.job.tracker", "10.133.103.21:50021");  
  58.         MongoConfigUtil.setOutputURI(conf, "mongodb://10.133.103.23/test.out");  
  59.         System.out.println("Conf: " + conf);  
  60.   
  61.         final Job job = new Job(conf, "word count");  
  62.   
  63.         job.setJarByClass(WordCount.class);  
  64.   
  65.         job.setMapperClass(TokenizerMapper.class);  
  66.   
  67.         job.setReducerClass(IntSumReducer.class);  
  68.   
  69.         job.setOutputKeyClass(Text.class);  
  70.         job.setOutputValueClass(IntWritable.class);  
  71.   
  72.         job.setOutputFormatClass(MongoOutputFormat.class);  
  73.   
  74.         FileInputFormat.addInputPath(job, new Path("/tmp/input/"));  
  75.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  76.     }  
  77. }  

 

9.运行结果:



 

10. 在MongoDB中查看结果:

 

Java代码 

 收藏代码

  1. > use test  
  2. switched to db test  
  3. > show collections  
  4. blog  
  5. out  
  6. system.indexes  
  7. > db.out.find()  
  8. { "_id" : "Hadoop", "value" : 2 }  
  9. { "_id" : "Hello", "value" : 4 }  
  10. { "_id" : "MongoDB", "value" : 2 }  
  11. { "_id" : "MongoDB!", "value" : 1 }  
  12. { "_id" : "with", "value" : 1 }  
  13. { "_id" : "world!", "value" : 1 }  

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值