Caché

Introducción a Spark SQL en Python

Mark Plutowski

Data Scientist

¿Qué es la caché?

  • Mantener datos en memoria
  • Spark tiende a liberar memoria agresivamente
Introducción a Spark SQL en Python

Política de expulsión

  • Least Recently Used (LRU)
  • La expulsión ocurre en cada worker de forma independiente
  • Depende de la memoria de cada worker
Introducción a Spark SQL en Python

Cachear un dataframe

Para cachear un dataframe:
df.cache()
Para quitarle la caché:
df.unpersist()
Introducción a Spark SQL en Python

Saber si un dataframe está en caché

df.is_cached
False
df.cache()
df.is_cached
True
Introducción a Spark SQL en Python

Quitar caché a un dataframe

df.unpersist()
df.is_cached()
False
Introducción a Spark SQL en Python

Nivel de almacenamiento

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

En el nivel de almacenamiento anterior se cumple:

  1. useDisk = True
  2. useMemory = True
  3. useOffHeap = False
  4. deserialized = True
  5. replication = 1
Introducción a Spark SQL en Python

Persistir un dataframe

Lo siguiente es equivalente en Spark 2.1+:

  • df.persist()

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

  • df.cache() es igual que df.persist()

Introducción a Spark SQL en Python

Cachear una tabla

df.createOrReplaceTempView('df')
spark.catalog.isCached(tableName='df')
False
spark.catalog.cacheTable('df')
spark.catalog.isCached(tableName='df')
True
Introducción a Spark SQL en Python

Quitar caché de una tabla

spark.catalog.uncacheTable('df')
spark.catalog.isCached(tableName='df')
False
spark.catalog.clearCache()
Introducción a Spark SQL en Python

Consejos

  • La caché es perezosa (lazy)
  • Cachea solo si harás más de una operación
  • Despersistir cuando ya no lo necesites
  • Cachea de forma selectiva
Introducción a Spark SQL en Python

¡Vamos a practicar!

Introducción a Spark SQL en Python

Preparing Video For Download...