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').\
opti
Spark_SQL-DataFrame数据写出以及读写数据库(以MySQl为例)_dataframe mysql option
最新推荐文章于 2024-04-27 21:28:43 发布