您的位置:首页 > 数据库 > MySQL

用sqarkSQL往MySQL写入数据

2020-04-07 12:16 1681 查看

先设置表头,再写内容,内容得通过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)
  • 点赞
  • 收藏
  • 分享
  • 文章举报
try to stay simple 发布了38 篇原创文章 · 获赞 0 · 访问量 1731 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: