Ваш код в том виде, в котором он реализован в настоящее время, вообще не выполняет никакого кэширования. Вы должны помнить, что метод .persist()
не оказывает никакого побочного эффекта на Dataframe
, он просто возвращает новый Dataframe
с возможностью сохранения.
При вызове dcRawAll.persist()
вы не назначаете результат, поэтому у вас нет ссылки на Dataframe
, что может сохраниться. Исправляя эту (очень распространенную) ошибку, кэширование все еще не помогает, как вы надеетесь. Ниже я прокомментирую ваш код, объясняя более подробно, что, вероятно, происходит во время выполнения.
//dcRawAll will contian a Dataframe, that will be cached after its next action
val dcRawAll = dataframe.select("C1","C2","C3","C4").persist()
//after this line, dcRawAll is calculated, then cached
val statsdcRawAll = dcRawAll.count()
//dc will contain a Dataframe that will be cached after its next action
val dc = dcRawAll.where(col("c1").isNotNull).persist()
//at this point, you've removed the dcRawAll cache never having used it
//since dc has never had an action performed yet
//if you want to make use of this cache, move the unpersist _after_ the
//dc.count()
dcRawAll.unpersist(false)
//dcRawAll is recalculated from scratch, and then dc is calculated from that
//and then cached
val statsdc = dc.count()
//dcclean will contain a dataframe that will be cached after its next action
val dcclean = dc.where(col("c2")=="SomeValue").persist()
//at this point, you've removed the dc cache having never used it
//if you perform a dcclean.count() before this, it will utilize the dc cache
//and stage the cache for dcclean, to be used on some other dcclean action
dc.unpersist()
По сути, вы должны убедиться, что не .unpersist()
a Dataframe
до тех пор, пока после Dataframe
зависит от того, было ли выполнено действие. Прочитайте этот ответ (и связанные документы), чтобы лучше понять разницу между преобразованием и действием.