首先要下载mongo-hadoop adapter
git clone https://github.com/mongodb/mongo-hadoop.git
git checkout release-1.0
然后进入mongo-hadoop目录,找到build.sbt将 hadoopRelease in ThisBuild 修改成如下所示:
hadoopRelease in ThisBuild := "0.20"
然后运行 ./sbt package(关于sbt https://github.com/harrah/xsbt/wiki)
运行时需要FQ才行。
运行结束之后会再core/target目录下生成一个jar文件mongo-hadoop-core_0.20.205.0-1.0.1.jar,然后下载mongodb的驱动包。
wget --no-check-certificate https://github.com/downloads/mongodb/mongo-java-driver/mongo-2.7.3.jar 下载完之后就可以开始开发mongo-hadoop的程序了。
运行自带示例: 首先将数据导入到mongodb中,命令如下。
./sbt load-sample-data
然后再eclipe中新建一个项目,例如treasury,将mongo-hadoop/example/treasury_field中的源文件和资源文件复制到新建的项目下。
如图所示:
然后修改mongo-treasury_yield.xml文件中mongodb的url和存放collection
<property>
<!-- If you are reading from mongo, the URI -->
<name>mongo.input.uri</name>
<value>mongodb://127.0.0.1/mongo_hadoop.yield_historical.in</value>
</property>
<property>
<!-- If you are writing to mongo, the URI -->
<name>mongo.output.uri</name>
<value>mongodb://127.0.0.1/mongo_hadoop.yield_historical.out</value>
</property>
<!-- If you are reading from mongo, the URI -->
<name>mongo.input.uri</name>
<value>mongodb://127.0.0.1/mongo_hadoop.yield_historical.in</value>
</property>
<property>
<!-- If you are writing to mongo, the URI -->
<name>mongo.output.uri</name>
<value>mongodb://127.0.0.1/mongo_hadoop.yield_historical.out</value>
</property>
然后修改TreasuryYieldXMLConfig.java如下:
Configuration.addDefaultResource( "resources/mongo-treasury_yield.xml");
Configuration.addDefaultResource( "resources/mongo-defaults.xml" );
Configuration.addDefaultResource( "resources/mongo-defaults.xml" );
后将项目打包成jar文件。
运行 hadoop jar treasury.jar com.mongodb.hadoop.treasury.TreasuryXMLConfig 即可运行hadoop程序。运行结果如下图:mongodb中的数据。