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记录的 各个字段的名称 类型 和是否运行为空
    '''
相关推荐
✿ ༺ ོIT技术༻4 天前
Linux:TCP和守护进程
linux·运维·服务器·网络·tcp/ip·1024程序员节
辅助东皇燕双鹰8 天前
行测知识()
1024程序员节
深蓝易网11 天前
探寻制造型企业MES管理系统:功能、架构与应用全解析
大数据·运维·人工智能·架构·制造·1024程序员节
Lenyiin16 天前
2848、与车相交的点
c++·算法·leetcode·1024程序员节
earthzhang202122 天前
《深入浅出HTTPS》读书笔记(31):HTTPS和TLS/SSL
开发语言·网络·python·https·1024程序员节
不讲废话的小白24 天前
怎么样把pdf转成图片模式(不能复制文字)
pdf·1024程序员节
明明真系叻25 天前
2025.1.26机器学习笔记:C-RNN-GAN文献阅读
人工智能·笔记·深度学习·机器学习·生成对抗网络·1024程序员节
Joeysoda1 个月前
Java数据结构 (从0构建链表(LinkedList))
java·linux·开发语言·数据结构·windows·链表·1024程序员节
清风-云烟1 个月前
使用redis-cli命令实现redis crud操作
java·linux·数据库·redis·spring·缓存·1024程序员节
Joeysoda1 个月前
Java数据结构 (链表反转(LinkedList----Leetcode206))
java·linux·开发语言·数据结构·链表·1024程序员节