先设置表头,再写内容,内容得通过Row再转换成dataframe,再把内容与表头连接,再插入到MysqL中
#!/usr/bin/env python3
from pyspark.sql import Row
from pyspark.sql.types import *
from pyspark import SparkContext,SparkConf
from pyspark.sql import SparkSession
spark = SparkSession.builder.config(conf=SparkConf()).getorCreate()
schema=StructType([StructField("id",IntegerType(),True),\#true代表可以为空
StructField("name",StringType(),True),\
StructField("gender",StringType(),True),\
StructField("age",IntegerType,True])
studentRDD = spark.saprkContext.parallelize(["3 HuangYukai M 26"]).map(lambda x:x.split(" "))
rowRDD = studentRDD.map(lamda x:Row(int(x[0].strip()),x[1].strip[],x[2].strip().int(x[3].strip())))
studentDF = spark.createDataFrame(rowRDD,schema)
prop={}
prop['user']='hadoop'
prop['password']='hadoop'
prop['driver']= "com.MysqL.jdbc.Driver"
studentDF.write.jdbc("jdbc:MysqL://localhost:3306/spark",'student','append',prop)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。