- Podcasts
- Programmers
- 💥 Spark DataFrame Cache
About this episodeIn Apache Spark, you can cache a DataFrame in memory using the cache() or persist() method. The cache() method is a shorthand for…
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
💥 Spark DataFrame Cache
When exactly. Are we going to use it? And how to basically, If we do multiple operations on a spark that find out from that filter, that went to the doctor to find out filter that a frame dot whatever. Then at a certain point we can stuck their data frame dot cash and later on we would have their action that a DOT count.
Then this only then this would get cached and we could really use this. Okay. So we already see that. We can cash. Schmoopy put that frames and the cash under the hood, try to fit that, that framing pole both memory and also disk it's important to note that the data frame do not persist will accept the parameter where we would tell it, whether we want it to be cached only to marry only today's off to a win this and there are few couple of flour more options there.
So, basically whenever we do this, Hey, we want to do this on parts of that frames. That would repeat themselves. And if this part is a huge gigantic a part, we probably don't want to do this because it could possibly not fit our memory and and disc, okay? So maybe there are other way of optimizing, our stock that friends walking such that we would not need to reuse this data frame.
At all. So when we can share that frame, we create a new variable for it, it's best to call to create a new variable for it because then we could reference it later. If we don't do this, we would not have a way to reference this data file, unless we use create a holy Place, Global late term for you. In this case, we could also catch it and then reference it in Sparky scale as cash.
Also, it's important to Persist data frame after it no longer needed. So we call data frame that we have cashed previously, Dot and persist, this will prevent from having the caching layer becoming pull. If it's pulled spark, will be able to manage it. It would start removing evicting, the data from memory in and allowing you style least recently used strategy, but it is Good part is when you know that you don't would need anymore. The cash to just collect explicitly the and persist and unload and the memory and the disk and this would save us from all this ilario has next tip is to just cash whatever we exactly actually needing to queries and not just cash everything including things that we don't need. This includes both data frames and columns. If we have 100 columns, coming returning for Macquarie and we'll catch this data frame, because we need only one column out of them.
This does not make sense. We could make better use of our memory and first do that offended. Select column, one, and only. Then cash. This, the that frame at this point I want to repeat what do we cache? We do that. Defy him dot filter that front of Select. Advantage filter that will framework to add whatever.
And before we actually make the action, we call that fine dot cash which would not cash at this point. Only at a point where we actually make an action like that from dot count that front of collect and Etc. There is an overhead to the cash show. So as we said, if we have small that frames it and we reuse them over and over, it might make sense to catch them, but if we have gigantic tens of gigabytes of data frame, then converting them to a data structure that we want to fit in memory and then placing them in memory and then later on a victim then all this with consume some time. So we should take note of when we want to cash in one. Don't want to Cache want occasion smaller that frames on specific columns that we are going to use over and over. Check.
