Skip to content

Instantly share code, notes, and snippets.

@danared
Created October 26, 2015 21:13
Show Gist options
  • Select an option

  • Save danared/dd1bc6401872a199159f to your computer and use it in GitHub Desktop.

Select an option

Save danared/dd1bc6401872a199159f to your computer and use it in GitHub Desktop.
//Import the relevant packages and classes
import com.mongodb.casbah.{WriteConcern => MongodbWriteConcern}
import com.stratio.provider._
import com.stratio.provider.mongodb._
import com.stratio.provider.mongodb.schema._
import com.stratio.provider.mongodb.writer._
import org.apache.spark.sql.hive.HiveContext
import MongodbConfig._
//Configure which database and collection to read from, with optional parameters too
val mcInputBuilder = MongodbConfigBuilder(Map(Host -> List("localhost:27017"), Database -> "marketdata", Collection -> "minbars", SamplingRatio -> 1.0, WriteConcern -> MongodbWriteConcern.Normal))
val readConfig = mcInputBuilder.build()
//HiveContext uses Hive's SQL parser with a superset of features of SQLContext so I used that one
// See http://spark.apache.org/docs/1.4.0/sql-programming-guide.html#starting-point-sqlcontext for more info
val sqlContext = new HiveContext(sc) //sc is already defined as a SparkContext by the shell
val dfOneMin = sqlContext.fromMongoDB(readConfig) //set up the MongoDB collection to read from as a DataFrame
dfOneMin.registerTempTable("minbars") //make the table minbars available to the SQL expressions later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment