如何在 Azure Blob 存储中覆盖后命名 csv 文件
How to name a csv file after overwriting in Azure Blob Storage
我正在使用 Databricks notebook 将文件读取和写入同一位置。但是当我写入文件时,我得到了很多不同名称的文件。
像这样:
我不确定为什么会在我指定的位置创建这些文件。
另外,在我执行写操作后创建了另一个名为“new_location”的文件
我想要的是,在从 Azure Blob 存储读取文件后,我应该将文件写入与原始文件同名的相同位置。但我无法这样做。请帮助我,因为我是 Pyspark 的新手
我已经安装,现在我正在读取 azure blob 存储容器中的 CSV 文件存储。
覆盖的文件创建时名为“part-00000-tid-84371752119947096-333f1e37-6fdc-40d0-97f5-78cee0b108cf-31-1-c000.csv”
代码:
df = spark.read.csv("/mnt/ndemo/nsalman/addresses.csv", inferSchema = True)
df = df.toDF("firstName","lastName","street","town","city","code")
df.show()
file_location_new = "/mnt/ndemo/nsalman/new_location"
# write the dataframe as a single file to blob storage
df.write.format('com.databricks.spark.csv') \
.mode('overwrite').option("header", "true").save(file_location_new)
Spark 将为数据集的每个分区保存一个部分 csv 文件。要生成单个 csv 文件,可以将其转换为 pandas dataframe,然后将其写出。
尝试更改这些行:
df.write.format('com.databricks.spark.csv') \
.mode('overwrite').option("header", "true").save(file_location_new)
到这一行
df.toPandas().to_csv(file_location_new, header=True)
您可能需要在 file_location_new
前添加 "/dbfs/"
才能正常工作。
这是一个最小的独立示例,演示如何使用 pandas 编写 csv 文件:
df = spark.createDataFrame([(1,3),(2,2),(3,1)], ["Testing", "123"])
df.show()
df.toPandas().to_csv("/dbfs/" + "/mnt/ndemo/nsalman/" + "testfile.csv", header=True)
我正在使用 Databricks notebook 将文件读取和写入同一位置。但是当我写入文件时,我得到了很多不同名称的文件。 像这样:
我不确定为什么会在我指定的位置创建这些文件。 另外,在我执行写操作后创建了另一个名为“new_location”的文件
我想要的是,在从 Azure Blob 存储读取文件后,我应该将文件写入与原始文件同名的相同位置。但我无法这样做。请帮助我,因为我是 Pyspark 的新手 我已经安装,现在我正在读取 azure blob 存储容器中的 CSV 文件存储。 覆盖的文件创建时名为“part-00000-tid-84371752119947096-333f1e37-6fdc-40d0-97f5-78cee0b108cf-31-1-c000.csv”
代码:
df = spark.read.csv("/mnt/ndemo/nsalman/addresses.csv", inferSchema = True)
df = df.toDF("firstName","lastName","street","town","city","code")
df.show()
file_location_new = "/mnt/ndemo/nsalman/new_location"
# write the dataframe as a single file to blob storage
df.write.format('com.databricks.spark.csv') \
.mode('overwrite').option("header", "true").save(file_location_new)
Spark 将为数据集的每个分区保存一个部分 csv 文件。要生成单个 csv 文件,可以将其转换为 pandas dataframe,然后将其写出。
尝试更改这些行:
df.write.format('com.databricks.spark.csv') \
.mode('overwrite').option("header", "true").save(file_location_new)
到这一行
df.toPandas().to_csv(file_location_new, header=True)
您可能需要在 file_location_new
前添加 "/dbfs/"
才能正常工作。
这是一个最小的独立示例,演示如何使用 pandas 编写 csv 文件:
df = spark.createDataFrame([(1,3),(2,2),(3,1)], ["Testing", "123"])
df.show()
df.toPandas().to_csv("/dbfs/" + "/mnt/ndemo/nsalman/" + "testfile.csv", header=True)