具有定义全局变量的文件:
#Globals.py--
def init():
global XYZ
XYZ='Some Variable'
print("GLobal Variables initialized Successfully ")
这是udf函数试图访问"XYZ"全局变量
import Globals
#udfs.py
def trans_thrp_cd():
try:
global xyz
print(xyz)
except Exception as e:
print("Error in fetching value from the globals "+ str(e))
#main.py
import Globals
spark and other modules import-initialization
register functions as pyspark-hive UDFs
df=hive_context.sql("select trans_thrp_cd from test.people")
df.show()
得到以下提到的错误:
#Error
module 'Globals' has no attribute 'XYZ'
在Globals.py:中
global XYZ
XYZ = 'Some variable'
在udfs.py中:
import Globals
print(Globals.XYZ)