You can run a UDF via the "bq" command line tool by specifying the --udf_resource
flag. You can set the flag value to a gs://
URL or to the name of a local file.
For example, you can run the urlDecode
UDF from the UDF documentation as follows:
$ 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)"