如何访问广播变量的内容 [英] How to access content of a broadcast variable

查看:466
本文介绍了如何访问广播变量的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  json_data = text.map(波长X:json.loads(X))
 ....
#code计算平均并生成json_data ['jsontag']和AvgValue中的元组
一些RDD filtsubavg用的元组(jsontag,AvgValue中)
V = sc.broadcast(filtsubavg.collect())
COM = json_data.map(拉姆达1:1 ['jsontag'],L)
RES = com.map(拉姆达(cmtag,厘米):get_val(cmtag,厘米,V))

如何访问伏在我的功能,如果我需要在AvgValue中说鸿沟。

 高清get_val(jsontag,厘米,V):
    R1 =厘米[jsontag]
    R2 =厘米[值] /(获得第五jsontag对应的值)
    返回(R1,R2)


解决方案

要访问您可以使用它的值广播变量的内容属性:

  V.value

如果你想使用它作为一个查找表,它会是有意义的,收集其作为映射(字典):

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

然后,你可以简单地使用:

 厘米[值] / V.value.get(五)

I need do some calculation within a function that uses a broadcast value

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))

How can I access V in my function if I need to say divide by the avgvalue.

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

解决方案

To access content of the broadcast variable you can use its value property:

V.value

If you want to use it as a lookup table it would makes sense to collect it as map (dictionary):

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

Then you can simply use:

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

这篇关于如何访问广播变量的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆