20

如何确定数据框大小?

现在我估计数据框的实际大小如下:

headers_size = key for key in df.first().asDict()
rows_size = df.map(lambda row: len(value for key, value in row.asDict()).sum()
total_size = headers_size + rows_size

它太慢了,我正在寻找更好的方法。

4

2 回答 2

16

来自 Tamas Szuromi 的好帖子http://metricbrew.com/how-to-estimate-rdd-or-dataframe-real-size-in-pyspark/

from pyspark.serializers import PickleSerializer, AutoBatchedSerializer
def _to_java_object_rdd(rdd):  
    """ Return a JavaRDD of Object by unpickling
    It will convert each Python object into Java object by Pyrolite, whenever the
    RDD is serialized in batch or not.
    """
    rdd = rdd._reserialize(AutoBatchedSerializer(PickleSerializer()))
    return rdd.ctx._jvm.org.apache.spark.mllib.api.python.SerDe.pythonToJava(rdd._jrdd, True)

JavaObj = _to_java_object_rdd(df.rdd)

nbytes = sc._jvm.org.apache.spark.util.SizeEstimator.estimate(JavaObj)
于 2017-07-20T19:04:57.993 回答
14

目前我正在使用以下方法,但不确定这是否是最好的方法:

df.persist(StorageLevel.Memory)
df.count()

Storage选项卡下的spark-web UI 上,您可以检查以 MB 为单位显示的大小,然后我会执行 unpersist 以清除内存:

df.unpersist()
于 2016-08-11T23:54:21.993 回答