pyspark写入mongodb

import datetime

# data="8/18/2019 6:53:14 PM"
# strptime = datetime.datetime.strptime(data, '%d/%m/%Y %H:%M:%S PM')
# print(strptime)

a={'a':1,'b':2}
print(a['a'])
print(a.get('c'))
# from pyspark import SparkConf,SparkContext
# sc = SparkContext("local","Simple App")
# print(sc)



from pyspark import SparkConf, SparkContext
from pyspark.sql import SQLContext
from pyspark.sql.types import *

from pyspark.sql import SparkSession
spark = SparkSession\
    .builder\
    .master('local')\
    .config("spark.mongodb.output.uri", "mongodb://127.0.0.1:27017/wilsoner.wilsonuser") \
    .config('spark.jars.packages', 'org.mongodb.spark:mongo-spark-connector_2.11:2.3.1')\
    .getOrCreate()


schema = StructType([
    StructField("name", StringType()),
    StructField("age", IntegerType()),
    StructField("sex", StringType())
])

df = spark.createDataFrame([('wilson', 36, 'male'), ('sunquan', 26, 'male'), ('zhugeliang', 26, 'male')], schema)

df.show()
df.write.format('com.mongodb.spark.sql.DefaultSource').mode("append").save()

你可能感兴趣的:(spark,mongdb)