错误:SparkContext 只能在驱动程序上使用,不能在工作程序上运行的代码中使用。有关更多信息,请参阅 SPARK-5063

Rah*_*hul 3 python lambda python-2.7 pyspark pyasn1

我目前正在使用 ASN 1 解码器。我将从生产者那里获取十六进制十进制代码,并将其收集到消费者中。然后,我将十六进制代码转换为 RDD,然后将十六进制值 RDD 传递给同一类 Decode_Module 中的另一个函数,并将使用 python asn1 解码器来解码十六进制数据并将其返回并打印。我不明白我的代码出了什么问题。我也已经在工作节点中安装了我的 asn1 解析器依赖项。我调用 lambda 表达式或其他内容的方式有任何问题。

我的错误:异常:您似乎正在尝试从广播变量、操作或转换引用 SparkContext。SparkContext 只能在驱动程序上使用,不能在工作程序上运行的代码中使用。有关更多信息,请参阅 SPARK-5063

请帮助我,谢谢

我的代码:

class telco_cn:

 def __init__(self,sc):
    self.sc = sc
    print ('in init function')
    logging.info('eneterd into init function')

 def decode_module(self,msg):
        try:
            logging.info('Entered into generate module')
            ### Providing input for module we need to load
            load_module(config_values['load_module'])
            ### Providing Value for Type of Decoding
            ASN1.ASN1Obj.CODEC = config_values['PER_DECODER']
            ### Providing Input for Align/UnAlign
            PER.VARIANT = config_values['PER_ALIGNED']
            ### Providing Input for pdu load
            pdu = GLOBAL.TYPE[config_values['pdu_load']]
            ### Providing Hex value to buf
            buf = '{}'.format(msg).decode('hex')
            return val
        except Exception as e:
            logging.debug('error in decode_module function %s' %str(e))


 def consumer_input(self,sc,k_topic):
            logging.info('entered into consumer input');print(k_topic)
            consumer = KafkaConsumer(ip and other values given)
            consumer.subscribe(k_topic)
            for msg in consumer:
                print(msg.value);
                a = sc.parallelize([msg.value])
                d = a.map(lambda x: self.decode_module(x)).collect()
                print d

if __name__ == "__main__":
    logging.info('Entered into main')
    conf = SparkConf()
    conf.setAppName('telco_consumer')
    conf.setMaster('yarn-client')
    sc = SparkContext(conf=conf)
    sqlContext = HiveContext(sc)
    cn = telco_cn(sc)
    cn.consumer_input(sc,config_values['kafka_topic'])
Run Code Online (Sandbox Code Playgroud)

Zha*_*ong 6

这是因为self.decode_module包含 SparkContext 的实例。

要修复您的代码,您可以使用@staticmethod

class telco_cn:
    def __init__(self, sc):
        self.sc = sc

    @staticmethod
    def decode_module(msg):
        return msg

    def consumer_input(self, sc, k_topic):
        a = sc.parallelize(list('abcd'))
        d = a.map(lambda x: telco_cn.decode_module(x)).collect()
        print d


if __name__ == "__main__":
    conf = SparkConf()
    sc = SparkContext(conf=conf)
    cn = telco_cn(sc)
    cn.consumer_input(sc, '')
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息:

http://spark.apache.org/docs/latest/programming-guide.html#passing-functions-to-spark