[英]How can I capture STDOUT on a Python UDF in Snowflake to return as string?
一些 Python 库 output 将其函数的结果发送到 STDOUT - 并且在 Python UDF 中,我需要将其返回到一个字符串中的雪花。
例如help()
或numpy.show_config()
的 output 。
您可以使用contextlib.redirect_stdout
捕获标准输出。
例如,要捕获help(numpy)
的 output :
create or replace function help_numpy()
returns string
language python
runtime_version = '3.8'
packages = ('numpy')
handler = 'x'
as $$
import io
from contextlib import redirect_stdout
from pydoc import help
import numpy
def x():
f = io.StringIO()
with redirect_stdout(f):
help(numpy)
return f.getvalue()
$$;
select help_numpy();
或者要捕获numpy.show_config()
的 output,您只需替换在redirect_stdout()
块中调用的函数:
with redirect_stdout(f):
numpy.show_config()
请注意,要使用help()
我们还必须from pydoc import help
。 否则你会得到错误NameError: name 'help' is not defined in function HELP with handler
。
这对所有 UDF 语言来说都是一个好主意。 开源 JavaScript 经常充满alert
, window.alert
, console.log
等。要捕获 JavaScript 中的警报和控制台输出功能:
create or replace function STDOUT()
returns array
language javascript
as
$$
//----- Start of standard out intercepts. Add to top of UDF
console.logs = [];
window = {};
console.log = function(){ console.logs.push({"type":"log", "timestamp":new Date().toISOString(), "out":Array.from(arguments)}) };
console.warn = function(){ console.logs.push({"type":"warn", "timestamp":new Date().toISOString(), "out":Array.from(arguments)}) };
console.error = function(){ console.logs.push({"type":"error", "timestamp":new Date().toISOString(), "out":Array.from(arguments)}) };
console.debug = function(){ console.logs.push({"type":"debug", "timestamp":new Date().toISOString(), "out":Array.from(arguments)}) };
alert = function(){ console.logs.push({"type":"alert", "timestamp":new Date().toISOString(), "out":Array.from(arguments)}) };
window.alert = function(){ console.logs.push({"type":"alert", "timestamp":new Date().toISOString(), "out":Array.from(arguments)}) };
//----- End of standard out intercepts
console.log("My log.");
console.warn("My warning");
console.error("My error");
alert("My alert");
window.alert("My window alert");
return console.logs;
$$;
select STDOUT();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.