当前位置:网站首页>PySpark的存储不同格式文件
PySpark的存储不同格式文件
2022-08-05 13:06:00 【u013250861】
PySpark的存储不同格式文件,如:存储为csv格式、json格式、parquet格式、compression格式、table
from future import print_function, division
from pyspark import SparkConf, SparkContext
from pyspark.sql import SparkSession
启动 Spark (如果你已经启动就不需要)
spark = SparkSession.builder.master(“local[2]”).appName(“test”).enableHiveSupport().getOrCreate()
sc = spark.sparkContext
存储为csv格式
df_csv = spark.read.csv(“…/data/ratings.csv”, header=True)
df_csv.show()
df_csv.write.csv(‘…/output/rating.csv’, header = True, mode = ‘error’) #保存数据
将文档保存在一个文件夹中
!ls -lh …/output/rating.csv #根据数量保存多个文件
!head …/output/rating.csv/part-00001-aece805c-20a7-4225-b152-40316bc8fc5e-c000.csv
df_csv.coalesce(1).write.csv(‘…/output/rating2.csv’, header = True)
!ls -lh …/output/rating.csv
存储为json格式
df_csv.write.json(‘…/output/rating.json’,mode = ‘overwrite’)
!ls -lh …/output/rating.json #根据数量保存多个文件
##注意:其中json的内存要比csv大(存储空间)
存储为parquet格式
df_csv.write.parquet(‘…/output/rating.parquet’,mode = ‘overwrite’)
!ls -lh …/output/rating.parquet #根据数量保存多个文件
存储为compression格式—压缩
df_csv.write.csv(‘…/output/rating_gzip.csv’, header = True, compression = ‘gzip’)
!ls -lh …/output/rating_gzip.csv #根据数量保存多个文件
存储为table
spark.sql(‘show tables’).show()
df_csv.write.saveAsTable(‘rating_csv’)
spark.sql(“select * from ratings_csv”).show()
边栏推荐
猜你喜欢
随机推荐
《MySQL核心知识》第6章:查询语句
js实现随机验证码(带干扰线)
Simple clock animation
为什么设计思维是有用的?
《MySQL核心知识》第3章:MySQL中的运算符
二分查找处理不当引起的堆栈溢出异常
吉时利静电计在高电阻率测量解决方案应用
Easy to understand and play QT: QT program release packaging
《MySQL核心知识》第1章:开篇:专栏介绍
华朗复读衔接营励志开营!全名师阵容护航 解读高考成功秘钥
A brief explanation of permutation and combination
一夜成名的航班追踪网站,什么来头?
What the hell is a DAO
Dialogue with Zhuang Biaowei: The first lesson of open source
爱可可AI前沿推介(8.5)
源码解析二 模型转换 export.py
电脑端微信无法打开腾讯文档
对比服务器,进行正确配置
RT-Thread recording (2. RT-Thread kernel startup process - startup file and source code analysis)
一种3D视频格式转换(H264 MVC至SBS / OU)方案









