Skip to content

Commit 59365bd

Browse files
authored
Update examples.scala
1 parent 5c9f900 commit 59365bd

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

spark/files/examples.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ val outputRDD = rdd.map(
3636
println("Spark RDD processing result: ")
3737
outputRDD.foreach(println)
3838

39-
4039
/* Similar aggregation as above, but utilising MongoDB aggregation pipeline */
4140
val aggRdd = rdd.withPipeline(Seq(
4241
Document.parse("{$sort:{timestamp:1}}"),
@@ -57,8 +56,10 @@ println("RDD is written to MongoDB")
5756
/* DataFrames examples */
5857
val sqlContext = SQLContext.getOrCreate(sc)
5958
val df = MongoSpark.load(sqlContext)
59+
6060
// Print schema
6161
df.printSchema()
62+
6263
// Filter by Integer and by String
6364
df.filter(df("myid") < 2).show()
6465
df.filter(df("doc") === "V ").show()
@@ -67,8 +68,12 @@ df.filter(df("doc") === "V ").show()
6768
df.registerTempTable("temporary")
6869
val sqlResult = sqlContext.sql("SELECT myid, doc, timestamp FROM temporary WHERE myid > 6 AND doc='V '")
6970
sqlResult.show()
71+
7072
// Save out the filtered DataFrame result
7173
MongoSpark.save(sqlResult.write.option("collection", "DF_times").mode("overwrite"))
74+
// Alternatively you could also specify uri
75+
// MongoSpark.save(sqlResult.write.option("uri", "mongodb://mongodb:27017/spark.DF_times").mode("overwrite"))
76+
7277
// Read it back in
7378
MongoSpark.load(sqlContext, ReadConfig(Map("collection" -> "DF_times"), Some(ReadConfig(sqlContext)))).show()
7479

0 commit comments

Comments
 (0)