- Spark Connector R Guide >
- Write to MongoDB
Write to MongoDB¶
On this page
To create a DataFrame, first create a SparkSession object, then use the object’s createDataFrame()
function.
The sparkR
shell provides a default SparkSession object called
spark
.
To create a DataFrame, use the createDataFrame
method
to convert an R data.frame
to a Spark DataFrame. To save the
DataFrame to MongoDB, use the write.df()
method:
Note
The empty argument (“”) refers to a file to use as a data source. In this case our data source is a MongoDB collection, so the data source argument is empty.
The above operation writes to the MongoDB database and collection
specified in the spark.mongodb.output.uri option
specified in the sparkR
shell arguments or SparkSession
configuration.
To read the first few rows of the DataFrame, use the head()
method.
The operation prints the following output:
The printSchema()
method prints out the DataFrame’s schema:
In the sparkR
shell, the operation prints the following output:
Writing with Options¶
You can add arguments to the write.df()
method to specify
a MongoDB database and collection.
The following operation writes the charactersSparkdf
data to
a MongoDB collection called ages
in a database called
characters
.