通过 CLI 在 BigQuery 中调用外部 UDF
External UDF call in BigQuery through CLI
我正在尝试在 BigQuery 中使用 UDF [或通过 Talend] 在我首选的 JSON 中查询 table 作为输出。我已经阅读了 link,它解释了内联和外部 UDF 的用法。但是我想不出从 CLI 执行 UDF 的方法。
是否可以从 CLI [bq 或 gsutil] 执行外部 UDF,我可以通过 Talend Data Integerator 使用它tool.Can有人建议指向这个吗?
您可以通过 "bq" 命令行工具通过指定 --udf_resource
标志 运行 UDF。您可以将标志值设置为 gs://
URL 或本地文件的名称。
例如,您可以 运行 来自 UDF documentation 的 urlDecode
UDF,如下所示:
$ cat urldecode.js
// UDF definition
function urlDecode(row, emit) {
emit({title: decodeHelper(row.title),
requests: row.num_requests});
}
// Helper function with error handling
function decodeHelper(s) {
try {
return decodeURI(s);
} catch (ex) {
return s;
}
}
// UDF registration
bigquery.defineFunction(
'urlDecode', // Name used to call the function from SQL
['title', 'num_requests'], // Input column names
// JSON representation of the output schema
[{name: 'title', type: 'string'},
{name: 'requests', type: 'integer'}],
urlDecode // The function reference
);
$ cat query.sql
SELECT requests, title
FROM
urlDecode(
SELECT
title, sum(requests) AS num_requests
FROM
[fh-bigquery:wikipedia.pagecounts_201504]
WHERE language = 'fr'
GROUP EACH BY title
)
WHERE title LIKE '%ç%'
ORDER BY requests DESC
LIMIT 100
$ bq query --udf_resource=urldecode.js "$(cat query.sql)"
我正在尝试在 BigQuery 中使用 UDF [或通过 Talend] 在我首选的 JSON 中查询 table 作为输出。我已经阅读了 link,它解释了内联和外部 UDF 的用法。但是我想不出从 CLI 执行 UDF 的方法。
是否可以从 CLI [bq 或 gsutil] 执行外部 UDF,我可以通过 Talend Data Integerator 使用它tool.Can有人建议指向这个吗?
您可以通过 "bq" 命令行工具通过指定 --udf_resource
标志 运行 UDF。您可以将标志值设置为 gs://
URL 或本地文件的名称。
例如,您可以 运行 来自 UDF documentation 的 urlDecode
UDF,如下所示:
$ cat urldecode.js
// UDF definition
function urlDecode(row, emit) {
emit({title: decodeHelper(row.title),
requests: row.num_requests});
}
// Helper function with error handling
function decodeHelper(s) {
try {
return decodeURI(s);
} catch (ex) {
return s;
}
}
// UDF registration
bigquery.defineFunction(
'urlDecode', // Name used to call the function from SQL
['title', 'num_requests'], // Input column names
// JSON representation of the output schema
[{name: 'title', type: 'string'},
{name: 'requests', type: 'integer'}],
urlDecode // The function reference
);
$ cat query.sql
SELECT requests, title
FROM
urlDecode(
SELECT
title, sum(requests) AS num_requests
FROM
[fh-bigquery:wikipedia.pagecounts_201504]
WHERE language = 'fr'
GROUP EACH BY title
)
WHERE title LIKE '%ç%'
ORDER BY requests DESC
LIMIT 100
$ bq query --udf_resource=urldecode.js "$(cat query.sql)"