from pyspark import SparkContext
from pyspark.streaming import StreamingContext
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql.functions import *
from pyspark.sql.types import StructType,TimestampType, DoubleType, StringType, StructField
spark = SparkSession.\
        builder.\
        appName("project").\
        config("spark.executor.memory", "5gb").\
        config("spark.mongodb.input.uri","mongodb://mongo:27017").\
        config("spark.mongodb.output.uri","mongodb://mongo:27017").\
        config('spark.jars.packages', 'org.mongodb.spark:mongo-spark-connector:10.0.4').\
        getOrCreate()

people = spark.createDataFrame([("JULIA", 50), ("Gandalf", 1000), ("Thorin", 195), ("Balin", 178), ("Kili", 77),
                            ("Dwalin", 169), ("Oin", 167), ("Gloin", 158), ("Fili", 82), ("Bombur", 22)], ["name", "age"])

people.write.format("mongodb").mode("append").option("database",
"Project").option("collection", "events").save()