Registro

Introducción a Spark SQL en Python

Mark Plutowski

Data Scientist

Introducción a logging

import logging
logging.basicConfig(stream=sys.stdout, level=logging.INFO, 
                    format='%(asctime)s - %(levelname)s - %(message)s')
logging.info("Hello %s", "world")
logging.debug("Hello, take %d", 2)
2019-03-14 15:92:65,359 - INFO - Hello world
Introducción a Spark SQL en Python

Logging con nivel DEBUG

import logging
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, 
                    format='%(asctime)s - %(levelname)s - %(message)s')
logging.info("Hello %s", "world")
logging.debug("Hello, take %d", 2)
2018-03-14 12:00:00,000 - INFO - Hello world
2018-03-14 12:00:00,001 - DEBUG - Hello, take 2
Introducción a Spark SQL en Python

Depurar evaluación perezosa

  • evaluación perezosa
  • ejecución distribuida
Introducción a Spark SQL en Python

Un temporizador simple

t = timer()
t.elapsed()
1. transcurrido: 0.0 s 
t.elapsed() # Haz algo que tarda 2 segundos
2. transcurrido: 2.0 s 
t.reset() # Haz otra cosa que lleva tiempo: reset
t.elapsed()
3. transcurrido: 0.0 s 
Introducción a Spark SQL en Python

clase timer

class timer:
    start_time = time.time()
    step = 0

    def elapsed(self, reset=True):
        self.step += 1
        print("%d. elapsed: %.1f sec %s" 
              % (self.step, time.time() - self.start_time))
        if reset:
            self.reset()

    def reset(self):
        self.start_time = time.time()
Introducción a Spark SQL en Python

Desperdicio oculto de CPU

import logging
logging.basicConfig(level=logging.INFO, 
                    format='%(asctime)s - %(levelname)s - %(message)s')

# < create dataframe df here >

t = timer()
logging.info("Sin acción aquí.")
t.elapsed()
logging.debug("df tiene %d filas.", df.count())
t.elapsed()
2018-12-23 22:24:20,472 - INFO - Sin acción aquí.
1. transcurrido: 0.0 s 
2. transcurrido: 2.0 s
Introducción a Spark SQL en Python

Desactivar acciones

ENABLED = False

t = timer()
logger.info("Sin acción aquí.")
t.elapsed()
if ENABLED:
    logger.info("df tiene %d filas.", df.count())
t.elapsed()
2019-03-14 12:34:56,789 - Pyspark - INFO - Sin acción aquí.
1. transcurrido: 0.0 s 
2. transcurrido: 0.0 s
Introducción a Spark SQL en Python

Activar acciones

Al reejecutar el ejemplo anterior con ENABLED = True se dispara la acción:

2019-03-14 12:34:56,789 - INFO - Sin acción aquí.
1. transcurrido: 0.0 s 
2019-03-14 12:34:58,789 - INFO - df tiene 1107014 filas.
2. transcurrido: 2.0 s 
Introducción a Spark SQL en Python

¡Vamos a practicar!

Introducción a Spark SQL en Python

Preparing Video For Download...