Caching

Introduktion till Spark SQL i Python

Mark Plutowski

Data Scientist

Vad är caching?

  • Håller data i minnet
  • Spark tenderar att frigöra minne aggressivt
Introduktion till Spark SQL i Python

Eviction-policy

  • Least Recently Used (LRU)
  • Uteviction sker oberoende på varje worker
  • Beror på tillgängligt minne per worker
Introduktion till Spark SQL i Python

Cacha en dataframe

Cacha en dataframe:
df.cache()
Ta bort cache:
df.unpersist()
Introduktion till Spark SQL i Python

Kontrollera om en dataframe är cachad

df.is_cached
False
df.cache()
df.is_cached
True
Introduktion till Spark SQL i Python

Ta bort cache från en dataframe

df.unpersist()
df.is_cached()
False
Introduktion till Spark SQL i Python

Lagringsnivå

df.unpersist()
df.cache()
df.storageLevel
StorageLevel(True, True, False, True, 1)

I lagringsnivån ovan gäller följande:

  1. useDisk = True
  2. useMemory = True
  3. useOffHeap = False
  4. deserialized = True
  5. replication = 1
Introduktion till Spark SQL i Python

Persistera en dataframe

Följande är ekvivalenta i Spark 2.1+ :

  • df.persist()

  • df.persist(storageLevel=pyspark.StorageLevel.MEMORY_AND_DISK)

  • df.cache() är samma sak som df.persist()

Introduktion till Spark SQL i Python

Cacha en tabell

df.createOrReplaceTempView('df')
spark.catalog.isCached(tableName='df')
False
spark.catalog.cacheTable('df')
spark.catalog.isCached(tableName='df')
True
Introduktion till Spark SQL i Python

Ta bort cache från en tabell

spark.catalog.uncacheTable('df')
spark.catalog.isCached(tableName='df')
False
spark.catalog.clearCache()
Introduktion till Spark SQL i Python

Tips

  • Caching är lazy
  • Cacha bara om fler än en operation ska utföras
  • Kör unpersist när objektet inte längre behövs
  • Cacha selektivt
Introduktion till Spark SQL i Python

Nu kör vi en övning!

Introduktion till Spark SQL i Python

Preparing Video For Download...