我有多个zip文件,有数百个zip文件:
Parent_1.zip: [child1.zip, ..., childM.zip]
Parent_..zip: [child1.zip, ..., childN.zip]
Parent_P.zip: [child1.zip, ..., childL.zip]
我通过将二进制文件加载到rdd中解压缩它们,然后使用flatmap应用一个函数来提取csv文件作为字符串foreach child.zip块。
zips = sc.binaryFiles(data_files)
files_data = zips.flatMap(zip_extract_stores)
一旦提取了所有的zip块,我总共获得了748个块。
print(files_data.count()) # => 748 chunks
现在,我正在尝试将files_data
rdd内的csv字符串转换为单个数据帧。但我面临的问题很少。
这是我尝试过的。
collect()
提取内存中的列表,如下所示:files_data = zips.flatMap(zip_extract_stores).collect()
dataframes = [get_dataframe(data) for data in files_data]
merge_list_of_dataframes(dataframes)
在local
模式下运行时效果很好,但由于内存问题导致cluster
模式失败。
files_data = zips.flatMap(zip_extract_stores)
我尝试使用files_data
toLocalIterator()
for idx, data in enumerate(files_data.toLocalIterator()):
if idx % 100 == 0:
print("Loaded {} dataframes".format(idx))
dataframes.append(get_dataframe(data))
然后将数据框合并为一个merge_list_of_dataframes(dataframes)
这在local
模式下工作正常,但在cluster
模式下,最终数据框的总计数从一次运行到另一次运行是不同的。例如,一次运行总计数为100万,而另一次运行总计数为500万,尽管总预期计数为800万。
files_data.toLocalIterator()
以便消耗所有数据?