Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据加载到pig的udf python中_Python_Apache Pig - Fatal编程技术网

将数据加载到pig的udf python中

将数据加载到pig的udf python中,python,apache-pig,Python,Apache Pig,我正在使用python为PIG编写一个UDF函数。我发现了一个问题,因为我需要从文件中加载一些数据来计算udf结果 我将举例说明我的想法。我的想法是编写两个函数,其中一个用于初始化: abbr = [] def set (filename): file = open(filename, "r") for i in file: abbr.append(i) @outputSchema("out:chararray") def get (line):

我正在使用python为PIG编写一个UDF函数。我发现了一个问题,因为我需要从文件中加载一些数据来计算udf结果

我将举例说明我的想法。我的想法是编写两个函数,其中一个用于初始化:

abbr = []   
def set (filename):
    file = open(filename, "r")
    for i in file:
        abbr.append(i)

@outputSchema("out:chararray")
def get (line):
    for i in abbr:
        if line.endswith(i):
            return "yes"
    return "no"
我的猪脚本应该在使用“get”之前调用一次“set”。但我不知道怎么做。我还尝试使用带有构造函数的类,但我没有成功调用成员函数“get”

有人能帮我吗

更新:

我现在“解决”了一个不太漂亮的解决方案:

abbr = []
@outputSchema("out:chararray")
def get (line):
    if len(abbr)==0:
        file = open("file.txt", "r")
        for i in file:
            abbr.append(i)
    for i in abbr:
        if line.endswith(i):
            return "yes"
        return "no"