Spark_SQL-DataFrame数据写出以及读写数据库(以MySQl为例)

一、数据写出

(1)SparkSQL统一API写出DataFrame数据

二、写出MySQL数据库


一、数据写出

(1)SparkSQL统一API写出DataFrame数据

统一API写法:

常见源写出:

python 复制代码
# cording:utf8

from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, IntegerType, StringType
import pyspark.sql.functions as F
if __name__ == '__main__':
    spark = SparkSession.builder.\
        appName('write').\
        master('local[*]').\
        getOrCreate()

    sc = spark.sparkContext

    # 1.读取文件
    schema = StructType().add('user_id', StringType(), nullable=True).\
        add('movie_id', IntegerType(), nullable=True).\
        add('rank', IntegerType(), nullable=True).\
        add('ts', StringType(), nullable=True)

    df = spark.read.format('csv').\
        option('sep', '\t').\
        option('header', False).\
        option('encoding', 'utf-8').\
        schema(schema=schema).\
        load('../input/u.data')

    # write text 写出,只能写出一个列的数据,需要将df转换为单列df
    df.select(F.concat_ws('---', 'user_id', 'movie_id', 'rank', 'ts')).\
        write.\
        mode('overwrite').\
        format('text').\
        save('../output/sql/text')

    # write csv
    df.write.mode('overwrite').\
        format('csv').\
        option('sep',';').\
        option('header', True).\
        save('../output/sql/csv')

    # write json
    df.write.mode('overwrite').\
        format('json').\
        save('../output/sql/json')

    # write parquet
    df.write.mode('overwrite').\
        format('parquet').\
        save('../output/sql/parquet')

二、写出MySQL数据库

API写法:

注意:

①jdbc连接字符串中,建议使用useSSL=false 确保连接可以正常连接( 不使用SSL安全协议进行连接)

②jdbc连接字符串中,建议使用useUnicode=true 来确保传输中不出现乱码

③save()不要填参数,没有路径,是写出数据库

④dbtable属性:指定写出的表名

python 复制代码
# cording:utf8

from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, IntegerType, StringType
import pyspark.sql.functions as F
if __name__ == '__main__':
    spark = SparkSession.builder.\
        appName('write').\
        master('local[*]').\
        getOrCreate()

    sc = spark.sparkContext

    # 1.读取文件
    schema = StructType().add('user_id', StringType(), nullable=True).\
        add('movie_id', IntegerType(), nullable=True).\
        add('rank', IntegerType(), nullable=True).\
        add('ts', StringType(), nullable=True)

    df = spark.read.format('csv').\
        option('sep', '\t').\
        option('header', False).\
        option('encoding', 'utf-8').\
        schema(schema=schema).\
        load('../input/u.data')

    # 2.写出df到MySQL数据库
    df.write.mode('overwrite').\
        format('jdbc').\
        option('url', 'jdbc:mysql://pyspark01:3306/bigdata?useSSL=false&useUnicode=true&serverTimezone=GMT%2B8').\
        option('dbtable', 'movie_data').\
        option('user', 'root').\
        option('password', '123456').\
        save()
    
    # 读取
    df.read.mode('overwrite'). \
        format('jdbc'). \
        option('url', 'jdbc:mysql://pyspark01:3306/bigdata?useSSL=false&useUnicode=true&serverTimezone=GMT%2B8'). \
        option('dbtable', 'movie_data'). \
        option('user', 'root'). \
        option('password', '123456'). \
        load()
    '''
    JDBC写出,会自动创建表的
    因为DataFrame中的有表结构信息,StructType记录的 各个字段的名称 类型 和是否运行为空
    '''
相关推荐
计算机毕业论文辅导2 天前
物联网实战:基于MQTT协议的智能家居数据传输系统设计与实现
1024程序员节
开开心心就好3 天前
支持批量处理的视频分割工具推荐
安全·智能手机·rust·pdf·电脑·1024程序员节·lavarel
liuyao_xianhui5 天前
Linux开发工具结尾 _make
linux·运维·服务器·数据结构·哈希算法·宽度优先·1024程序员节
学传打活7 天前
【边打字.边学昆仑正义文化】_21_爱的结晶(1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
数据皮皮侠AI13 天前
顶刊同款!中国地级市风灾风险与损失数据集(2000-2022)|灾害 / 环境 / 经济研究必备
大数据·人工智能·笔记·能源·1024程序员节
Fab1an15 天前
Busqueda——Hack The Box 靶机
linux·服务器·学习·1024程序员节
技术专家15 天前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
学传打活18 天前
古代汉语是源,现代汉语是流,源与流一脉相承。
微信公众平台·1024程序员节·汉字·中华文化
学传打活22 天前
【边打字.边学昆仑正义文化】_19_星际生命的生存状况(1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
unable code1 个月前
[HNCTF 2022 WEEK2]ez_ssrf
网络安全·web·ctf·1024程序员节