简体   繁体   English

如何在 Snowflake 中的 Python UDF 上捕获 STDOUT 以作为字符串返回?

[英]How can I capture STDOUT on a Python UDF in Snowflake to return as string?

Some Python libraries output the result of their functions to STDOUT - and in a Python UDF in Snowflake I need to capture that value to a String to return it.一些 Python 库 output 将其函数的结果发送到 STDOUT - 并且在 Python UDF 中,我需要将其返回到一个字符串中的雪花。

For example the output of help() or numpy.show_config() .例如help()numpy.show_config()的 output 。

You can capture stdout using contextlib.redirect_stdout .您可以使用contextlib.redirect_stdout捕获标准输出。

For example, to capture the output of help(numpy) :例如,要捕获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();

Or to capture the output of numpy.show_config() you just need to replace the functions called inside the redirect_stdout() block:或者要捕获numpy.show_config()的 output,您只需替换在redirect_stdout()块中调用的函数:

    with redirect_stdout(f):
       numpy.show_config()

Note that to use help() we also had to from pydoc import help .请注意,要使用help()我们还必须from pydoc import help Otherwise you'll get the error NameError: name 'help' is not defined in function HELP with handler .否则你会得到错误NameError: name 'help' is not defined in function HELP with handler

That's a great idea for all UDF languages.这对所有 UDF 语言来说都是一个好主意。 Open source JavaScript is often filled with with alert , window.alert , console.log etc. To capture the alert and console out functions in JavaScript:开源 JavaScript 经常充满alertwindow.alertconsole.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM