如何使用 Python 在 HDFS 中打开镶木地板文件?

How to open a parquet file in HDFS with Python?

我想读取存储在 HDFS 中的镶木地板文件,我正在使用 Python 来执行此操作。我在下面有这段代码,但它不会打开 HDFS 中的文件。你能帮我改一下代码吗?

sc = spark.sparkContext

from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)

df = sqlContext.read.parquet('path-to-file/commentClusters.parquet')

另外,我也希望将 Dataframe 保存为 CSV 文件。

试试

sqlContext.read.parquet("hdfs://<host:port>/path-to-file/commentClusters.parquet")

要找出主机和端口,只需搜索文件 core-site.xml 并查找 xml 元素 fs.defaultFS(例如 $HADOOP_HOME/etc/hadoop/core-site.xml)

为了简单起见,试试

sqlContext.read.parquet("hdfs:////path-to-file/commentClusters.parquet")

sqlContext.read.parquet("hdfs:/path-to-file/commentClusters.parquet")

引用 Cannot Read a file from HDFS using Spark

要另存为 csv,尝试

df_result.write.csv(path=res_path) # possible options: header=True, compression='gzip'