背景:我正在使用齐柏林飞艇笔记本进行分析。目标是使用而不是使用pySpark dataframe。熊猫有一些我用过的统计功能。编写.py文件来测试一个样本数据文件,所有的计算等等。
用例:我的数据文件( CSV格式)有模拟数据。通常列数为45,000列。目前,我有兴趣阅读前20,000篇专栏。行数通常为200行。
print('1 : ' , datetime.now())
sparkDF=spark.read.csv(filePath + '/' + filename,header=True,maxColumns=50000)
print('Built sparkDF with Rows: ' , sparkDF.count() , ' Column Count: ' , len(sparkDF.columns))
print('2 : ' , datetime.now())
sparkDF1 = sparkDF.select(sparkDF.columns[0:20000])
print('Built sparkDF1 with Rows: ' , sparkDF1.count() , ' Column Count: ' , len(sparkDF1.columns))
print('3 : ' , datetime.now())
exposures = sparkDF1.toPandas()
print('Built exposures with Rows: ' , exposures.shape[0] , ' Column Count: ' , exposures.shape[1])
print('4 : ' , datetime.now())
1 : 2019-10-17 16:34:06.725326
Built sparkDF with Rows: 107 Column Count: 40002
2 : 2019-10-17 16:40:04.171931
Built sparkDF1 with Rows: 107 Column Count: 20001
3 : 2019-10-17 16:51:12.465739
Built exposures with Rows: 107 Column Count: 20001
4 : 2019-10-17 16:58:25.65423问题:
。
构建数据帧的时间相当长。
我必须处理2900个这样的文件。需要一些建议和替代。
发布于 2019-10-20 20:53:00
我看到你在从一个文件中读取多个文件。要优化这个过程,您可以做的是读取.csv文件的整个目录,而不是一次读取一个文件(如果您的模式与所有类似于您的情况的文件相同):
file_path = "hdfs://path/to/multiple/files/"
df = (
spark.read.format("com.databricks.spark.csv")
.options(header="true", inferSchema="true")
.load(file_path)
)
pdf = df.toPandas()这将优化您的I/O的数量。即使您在同一个文件夹中有10个文件或200个文件或2900个文件,您的读取速度也会比单独读取一个文件的速度更快。
注意事项:上面的解决方案不适用于嵌套文件夹。
https://stackoverflow.com/questions/58476951
复制相似问题