2015-07-20 12 views
5
Class ProdsTransformer: 

    def __init__(self): 
     self.products_lookup_hmap = {} 
     self.broadcast_products_lookup_map = None 

    def create_broadcast_variables(self): 
     self.broadcast_products_lookup_map = sc.broadcast(self.products_lookup_hmap) 

    def create_lookup_maps(self): 
    // The code here builds the hashmap that maps Prod_ID to another space. 

pt = ProdsTransformer() 
pt.create_broadcast_variables() 

pairs = distinct_users_projected.map(lambda x: (x.user_id,  
         pt.broadcast_products_lookup_map.value[x.Prod_ID])) 

ottengo il seguente errore:Spark: variabili Broadcast: Sembra che si sta tentando di fare riferimento SparkContext da una variabile di trasmissione, azione o transforamtion

"Exception: It appears that you are attempting to reference SparkContext from a broadcast variable, action, or transforamtion. SparkContext can only be used on the driver, not in code that it run on workers. For more information, see SPARK-5063."

Qualsiasi aiuto con il modo di affrontare il le variabili trasmesse saranno fantastiche!

+0

Questo codice non è sufficiente e/o dati di esempio per consentire a qualcuno di provare a duplicare l'errore e/o correggerlo. Inoltre, nel caso non l'avessi notato, tutti i rientri vengono rimossi dal pitone. – Paul

+0

Ho aggiunto altro codice. – user3803714

+0

Mi chiedo se l'errore scompaia se spostate il file 'products_lookup_map' dalle proprietà delle istanze di' ProdsTransformer' e invece lo rendete globale. Hai bisogno di più di una mappa? – Paul

risposta

8

Facendo riferimento all'oggetto contenente la variabile di trasmissione nel lambda map, Spark tenterà di serializzare l'intero oggetto e spedirlo ai lavoratori. Poiché l'oggetto contiene un riferimento a SparkContext, si ottiene l'errore. Invece di questo:

pairs = distinct_users_projected.map(lambda x: (x.user_id, pt.broadcast_products_lookup_map.value[x.Prod_ID])) 

Prova questo:

bcast = pt.broadcast_products_lookup_map 
pairs = distinct_users_projected.map(lambda x: (x.user_id, bcast.value[x.Prod_ID])) 

Quest'ultima evita il riferimento all'oggetto (pt) in modo che Spark ha solo bisogno di spedire la variabile trasmissione.

Problemi correlati