在这里,我试图在用户定义的python函数中导入一个库
我创建了一个名为nltk的库,如下所示
[CREATE OR REPLACE LIBRARY nltk LANGUAGE plpythonu FROM 's3://nltk.zip' CREDENTIALS 'aws_access_key_id=*****;aws_secret_access_key=****';]创建后,我尝试将其导入一个函数中,如
CREATE OR REPLACE FUNCTION f_function (sentence varchar)
RETURNS VARCHAR STABLE AS $$
from nltk import tokenize
token = nltk.word_tokenize(sentence)
return token $$ LANGUAGE plpythonu;tokenize是nltk库中的子目录。
但是,当我试图通过调用表上的函数来运行该函数时,
SELECT f_function(text) from table_txt;我正收到这样的错误
亚马逊无效操作: ImportError:没有名为nltk的模块。有关更多信息,请访问svl_udf_log 详细信息:----------------------------------------------- error: ImportError: No module named nltk. Please look at svl\_udf\_log for more information code: 10000 context: UDF query: 69145 location: udf\_client.cpp:298 process: query0\_21 [pid=3165]
有人能帮我一下我在哪里做错了吗?
发布于 2016-05-13 05:03:47
我仍然在为上面的说明而挣扎,所以当我终于找到它时,我想我会把它传递到工作中去。
首先,创建库:
create or replace library stem
language plpythonu
from 's3://[Your Bucket Here]/stem.zip'
credentials 'aws_access_key_id=[aws key];aws_secret_access_key=[aws secret key]';下面是我编辑的根据地nltk库(我在compat中提取它以使其自包含),然后将其上传到S3:https://drive.google.com/file/d/0BzNI6AJdNrJCVThoSXVHY1NyUGM/view?usp=sharing,为了使用它,我必须编辑init.py库来引用上面创建的Redshift创建的UDF库("Stem")。
然后我在Redshift中创建了python函数:
create or replace function f_lancaster_stem (text varchar)
returns varchar
immutable as $$
from stem import LancasterStemmer
st = LancasterStemmer()
return st.stem(text)
$$ LANGUAGE plpythonu;那就打电话给UDF!
select f_lancaster_stem('resting') from dual;发布于 2016-03-14 20:31:27
首先,Python代码有一个明显的问题:您从不导入nltk,然后调用nltk.word_tokenize。
第二,下载nltk包后,您需要压缩包中的模块文件夹,并将此zip上传到RedShift。
nltk-X.Y.zip
├─ setup.py
├─ requirements.txt
├─ nltk <- This is the folder that should be zipped and uploaded to S3
... ├─ __init__.py
├─ tokenize.pyRedShift只能加载模块--您的根文件夹应该有一个__init__.py文件。http://docs.aws.amazon.com/redshift/latest/dg/udf-python-language-support.html
https://stackoverflow.com/questions/35836762
复制相似问题