从星火读巨​​大的MongoDB的集合与工人的帮助 [英] Reading Huge MongoDB collection from Spark with help of Worker

查看:212
本文介绍了从星火读巨​​大的MongoDB的集合与工人的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要读星火一个巨大的MongoDB集合创建一个持久的RDD并做进一步的数据上的分析。

I want to read a huge MongoDB collection from Spark create an persistent RDD and do further data analysis on it.

有没有什么方法可以让我更快的读取MongoDB的数据。
试图与MongoDB中的Java +卡斯巴的做法

Is there any way I can read the data from MongoDB faster. Have tried with the approach of MongoDB Java + Casbah

我可以用工人/从读取MongoDB的并行数据,然后保存为持久性数据,并使用它。

Can I use the worker/slave to read data in parallel from MongoDB and then save it as persistent data and use it.

推荐答案

有正从MongoDB的数据到Apache星火两种方式。

There are two ways of getting the data from MongoDB to Apache Spark.

方法1:
(上MongDB Java驱动程序层)使用卡斯巴

val uriRemote = MongoClientURI("mongodb://RemoteURL:27017/")
val mongoClientRemote =  MongoClient(uriRemote)
val dbRemote = mongoClientRemote("dbName")
val collectionRemote = dbRemote("collectionName")
val ipMongo = collectionRemote.find
val ipRDD = sc.makeRDD(ipMongo.toList)
ipRDD.saveAsTextFile("hdfs://path/to/hdfs")

在这里我们使用Scala和卡斯巴先获取数据,然后将其保存到HDFS。

Over here we are using Scala and Casbah to get the data first and then save it to HDFS.

方法2:在我们使用星火工人

更好的版本code的:使用星火工人和多核心使用来获得在较短的时间数据

Better version of code: Using Spark worker and multiple core to use to get the data in short time.

val config = new Configuration()
config.set("mongo.job.input.format","com.mongodb.hadoop.MongoInputFormat")
config.set("mongo.input.uri", "mongodb://RemoteURL:27017/dbName.collectionName")
val keyClassName = classOf[Object]
val valueClassName = classOf[BSONObject]
val inputFormatClassName = classOf[com.mongodb.hadoop.MongoInputFormat]
val ipRDD = sc.newAPIHadoopRDD(config,inputFormatClassName,keyClassName,valueClassName)
ipRDD.saveAsTextFile("hdfs://path/to/hdfs") 

这篇关于从星火读巨​​大的MongoDB的集合与工人的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆