PySpark将dataframe写入本地文件,只生成文件夹含有SUCCESS

  1. 代码
results = res.rdd\
          .map(lambda word: (word[0].replace(u"(", u"(").replace(u")", u")"), word[1], word[2]))\
          .filter(lambda word: word[0] in companys_list)\
          .map(lambda word: (companys_dic.get(word[0], word[0]),word[1], word[2]))\
          .filter(lambda word: word[1] != None and word[1] != u'None')
 
schemaString = "company_name,query,keyword"
fields = list(map(lambda fieldName : StructField(fieldName, StringType(), nullable = True), schemaString.split(",")))
schema = StructType(fields)
results_df = self.spark.createDataFrame(results, schema).distinct()
 
#results_df.show(100)
output_file = "file:///home/spark/query_20200520_uniq.csv"
results_df.write.mode("overwrite").options(header="true").csv(output_file, sep='\t')

但是运行结束只得到一个文件夹,并没有得到期望的结果,查阅资料发现保存到本地文件系统(file:///)只有再local模式下才能生效,在cluster模式下(不论是yarn-client还是yarn-cluster)都无法使用。

因此需要修改运行脚本,设置--master local[*] 。最终得到了期望的数据。

推荐阅读更多精彩内容

  • 一、Spark运行模式 1.1 local 本地模式:常用于本地开发测试 例如,程序中或-Master中传参数lo...
    吾系司机阅读 529评论 0 0
  • Zookeeper用于集群主备切换。 YARN让集群具备更好的扩展性。 Spark没有存储能力。 Spark的Ma...
    Yobhel阅读 6,760评论 0 34
  • 下载 Spark 1.官网地址http://spark.apache.org/ 2.文档查看地址https://s...
    shawn_3381阅读 204评论 0 0
  • 17.分区分桶的区别,为什么要分区 分区表:原来的一个大表存储的时候分成不同的数据目录进行存储。如果说是单分区表,...
    qydong阅读 420评论 0 0
  • 1、 性能调优 1.1、 分配更多资源 1.1.1、分配哪些资源? Executor的数量 每个Executor所...
    Frank_8942阅读 4,253评论 2 36