擅长:python、mysql、java
<p>如果您的要求是将所有国家/地区的数据保存在不同的文件中,您可以通过对数据进行分区来实现,但您将获得每个国家/地区的文件夹,而不是文件,因为spark无法将数据直接保存到文件中</p>
<p>Spark在调用数据帧编写器时创建文件夹</p>
<pre><code>df.write.partitionBy('country_code').csv(path)
</code></pre>
<p>输出将是包含相应国家/地区数据的多个文件夹</p>
<pre><code>path/country_code=india/part-0000.csv
path/country_code=australia/part-0000.csv
</code></pre>
<p>如果您希望每个文件夹中都有一个文件,您可以将数据重新分区为</p>
<pre><code>df.repartition('country_code').write.partitionBy('country_code').csv(path)
</code></pre>