Downloads a file from a URI and returns its content as a byte array.
FETCH_CONTENT and TRY_FETCH_CONTENT asynchronously download a file from the specified URI and return its content as VARBINARY. They differ in behavior when a download fails:
Function |
Download succeeds |
Download fails after retries are exhausted |
Applicable scenario |
|
Returns the file content |
Throws an exception and the job fails |
The file content is a required input for downstream processing. Failed records must not be skipped. |
|
Returns the file content |
Returns NULL and the job continues processing other records |
Failed records can be skipped, such as invalid URIs, nonexistent files, and network errors. |
Engine requirement
Only Realtime Compute engine VVR 11.7.0 and later support the
FETCH_CONTENTfunction.Only Realtime Compute engine VVR 11.9.0-preview.1 and later support the
TRY_FETCH_CONTENTfunction.
Syntax
VARBINARY FETCH_CONTENT(VARCHAR uri)
VARBINARY FETCH_CONTENT(VARCHAR uri, INTEGER concurrency)
VARBINARY TRY_FETCH_CONTENT(VARCHAR uri)
VARBINARY TRY_FETCH_CONTENT(VARCHAR uri, INTEGER concurrency)
Parameters
|
Parameter |
Type |
Description |
|
uri |
VARCHAR |
The file URI. Supported schemes include HTTP, HTTPS, and any scheme supported by the Flink FileSystem:
|
|
concurrency |
INTEGER |
Optional. Size of the dedicated I/O thread pool for each FETCH_CONTENT function instance. The pool serves file-system reads and asynchronous HTTP client callbacks. When omitted, defaults to max(8, number of available JVM processors). For example, with 4 processors the default is 8; with 16 processors the default is 16. |
-
Returns NULL if
uriis NULL. For a non-NULLuri, a failed download throws an exception. -
If the
urispecifies an OSS path, configure access credentials as described in Configure bucket authentication.
Return value
|
Type |
Description |
|
VARBINARY |
File content |
TRY_FETCH_CONTENT tolerates only content-fetching failures at runtime. SQL validation errors, such as a wrong number or types of arguments or a concurrency value out of range, still cause the job submission to fail.
Retry and timeout configuration
FETCH_CONTENT and TRY_FETCH_CONTENT use the unified retry and timeout configuration of async scalar functions. Request concurrency, timeout, and retries are managed by the Flink Async Scalar operator. You can change the related parameters with SET statements in your SQL job.
The following example sets the retry strategy to fixed-delay retry with a maximum of 2 retries, waits 1 s between two requests, and sets the async call timeout of a single record to 30 s.
SET 'table.exec.async-scalar.retry-strategy' = 'FIXED_DELAY';
SET 'table.exec.async-scalar.max-attempts' = '2';
SET 'table.exec.async-scalar.retry-delay' = '1 s';
SET 'table.exec.async-scalar.timeout' = '30 s';
When table.exec.async-scalar.max-attempts is set to 2, the function sends up to 3 requests: the first request plus 2 retries. If the timeout does not leave enough time for all retries, the timeout result applies: FETCH_CONTENT throws an exception and TRY_FETCH_CONTENT returns NULL.
The following table describes all parameters.
|
Parameter |
Default value |
Description |
|
table.exec.async-scalar.timeout |
3 min |
Maximum time to wait for the async operation to complete for a single input record. |
|
table.exec.async-scalar.max-attempts |
3 |
Maximum retries excluding the first call. By default the call is attempted up to 4 times. |
|
table.exec.async-scalar.max-concurrent-operations |
10 |
Maximum number of in-flight async operations allowed per subtask. Combined with |
|
table.exec.async-scalar.retry-strategy |
FIXED_DELAY |
Retry strategy. Valid values:
|
|
table.exec.async-scalar.retry-delay |
100 ms |
Fixed delay between two retry attempts. |
Example 1: Download file content
-
Test data
Table 1. T1
input
uri (VARCHAR)
1
http://example.com/image_url
2
oss://example-bucket/example.pdf
3
NULL
-
Test query
SELECT id, FETCH_CONTENT(uri) AS `value` FROM T1; -
Test result
id (INT)
value (VARBINARY)
1
x'ffd8ffe00010......'
2
x'aaffd8ffe000......'
3
NULL
Example 2: Return NULL when a download fails
-
Test data
Table 1. T1
input
uri (VARCHAR)
1
http://example.com/image_url
2
oss://example-bucket/example.pdf
3
invalid://path
-
Use
TRY_FETCH_CONTENTto download files. If a download fails, thecontentof that record returnsNULL, and the job is not interrupted by the failure.SELECT id, TRY_FETCH_CONTENT(uri) AS content FROM T2; -
Test result
id (INT)
value (VARBINARY)
1
x'ffd8ffe00010......'
2
NULL
3
NULL
-
You can filter out records that failed to download to avoid passing
NULLto downstream multimodal inference functions.SELECT id, content FROM ( SELECT id, TRY_FETCH_CONTENT(uri) AS content FROM T2 ) WHERE content IS NOT NULL;
Example 3: Specify the concurrency
The following example sets the content-fetching concurrency of each operator instance to 8. The 8 must be an integer literal in the SQL statement.
SELECT
id,
TRY_FETCH_CONTENT(uri, 8) AS content
FROM T2;
The following calls are invalid and cause errors at the SQL validation stage.
-- concurrency cannot reference a column.
SELECT TRY_FETCH_CONTENT(uri, concurrency_column) FROM T3;
-- Valid values of concurrency: 1 to 1024.
SELECT TRY_FETCH_CONTENT(uri, 0) FROM T3;
SELECT TRY_FETCH_CONTENT(uri, 1025) FROM T3;
References
-
Built-in functions: Supported functions.
-
User-defined functions: UDFs and Manage user-defined functions (UDFs).