如何从 Snowflake Javascript UDF / UDTF 返回包含多列的表?
问问题
481 次
1 回答
0
CREATE OR REPLACE FUNCTION HelloWorld()
RETURNS TABLE (NAME_COL VARCHAR, CITY_COL VARCHAR)
LANGUAGE JAVASCRIPT
AS '{
processRow: function f(row, rowWriter, context){
rowWriter.writeRow({NAME_COL: "Ram"}, {CITY_COL : "Vellore"});
rowWriter.writeRow({NAME_COL: "Krish"}, {CITY_COL : "Chennai"} );
}
}';
SELECT name_col, city_col FROM TABLE(HelloWorld());
假设您要处理列输入(例如字符串)并从中返回多列:
CREATE OR REPLACE FUNCTION HelloWorld(MESSAGE VARCHAR)
RETURNS TABLE (CLOUD VARCHAR, OPERATION VARCHAR, CALLTIME_MS double)
LANGUAGE JAVASCRIPT
AS '{
processRow: function f(row, rowWriter, context){
var str = row.MESSAGE;
var res = str.split(",");
var prefix = res[0].split(":");
var cloud = prefix[0].split(" ")[0];
var oper = prefix[1].substring(1);
var callTimeMs = parseFloat(res[1].split("=")[1].split("m")[0]);
rowWriter.writeRow({OPERATION : oper, CLOUD: cloud, CALLTIME_MS : callTimeMs});
}
}';
SELECT cloud, operation, calltime_ms FROM TABLE(HelloWorld('AWS Storage client library call end: putObject, callTime=40ms'));
假设您想从表中获取输出作为处理结果:
with stg as (
select gs.THREAD_CLASS as tc, gs.message AS msg
from logs_table as gs
where true
and timestamp between '2020-06-08 00:00:00' and '2020-06-08 01:00:00'
and contains(message, 'Storage client library call end:')
)
SELECT stg.tc, proc.operation, AVG(proc.calltime_ms)
FROM stg, table(ProcStorageCallEnd(stg.msg)) as proc
group by stg.tc, proc.operation;
于 2020-06-30T18:45:41.427 回答