【问题标题】:How to access content of a broadcast variable如何访问广播变量的内容
【发布时间】:2015-10-23 18:09:24
【问题描述】:

我需要在使用广播值的函数中进行一些计算

json_data = text.map(lambda x: json.loads(x))
 ....
# code to calculate average and generate tuple with json_data['jsontag'] and avgvalue
some rdd filtsubavg with tuples of (jsontag, avgvalue)
V = sc.broadcast(filtsubavg.collect())
com = json_data.map(lambda l:l['jsontag'],l) 
res = com.map(lambda (cmtag,cm): get_val(cmtag,cm,V))

如果我需要除以 avgvalue,如何在我的函数中访问 V。

def get_val(jsontag,cm,v):
    r1 = cm[jsontag]
    r2 = cm[value]/(get corresponding value for jsontag in v)
    return (r1,r2)

【问题讨论】:

    标签: python json apache-spark pyspark


    【解决方案1】:

    要访问广播变量的内容,您可以使用其value 属性:

    V.value
    

    如果您想将其用作查找表,则将其收集为地图(字典)是有意义的:

    V = sc.broadcast(filtsubavg.collectAsMap())
    

    那么你可以简单地使用:

    cm[value] / V.value.get(v)
    

    【讨论】:

    • 当我从 collect() 更改为 collectAsMap() 并使用 .get 功能时,它确实有效!谢谢
    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多