GET_BLOB_METADATA
The GET_BLOB_METADATA() function retrieves the metadata of a blob. You can use JSON functions to extract specific fields from the returned JSON object.
Common usage
GET_JSON_OBJECT(GET_BLOB_METADATA(blob_expr), '$.field_name');Usage notes
The
GET_BLOB_METADATA()function takes a single blob parameter.You can extract fields from the returned JSON object.
Common fields include: content_type, content_length, checksum, is_compressed, content_encoding, storage_type, and storage_tier.
content_lengthrepresents the length in bytes and is suitable for validation withlengthb().is_compressedindicates whether the content is compressed, andcontent_encodingspecifies the compression encoding.When
blob_exprisNULL,GET_BLOB_METADATAreturnsNULL. For an empty BLOB, the function still returns metadata, withcontent_lengthset to0.
Syntax
JSON GET_BLOB_METADATA(blob_expr:BLOB)Parameters
blob_expr (Required): An expression of the BLOB type.
The function accepts a single
blobparameter.The parameter must be a
blobexpression.Common sources include the
to_blob(...)function, ablobcolumn in a table, or ablobcolumn generated by aCREATE TABLE AS SELECT(CTAS) orINSERT SELECTstatement.
Examples:
GET_BLOB_METADATA(to_blob('abcdef'))
GET_BLOB_METADATA(to_blob('hello world', 'text/plain'))
GET_BLOB_METADATA(b)Return value
Returns a JSON object containing the metadata. The following table describes the common fields in this object.
Typically, you retrieve the value of a specific field instead of the entire object.
Field name | Description |
content_type | The content type specified when the blob was created. Common use cases include:
|
content_length | The size of the BLOB content in bytes. For an empty BLOB, the value is
|
checksum | A checksum of the content. You can use this field to determine if the content or storage object is consistent. Returns |
is_compressed | Indicates whether the blob is stored in a compressed format. Current observations show:
Avoid inferring complete compression rules from these examples alone. |
content_encoding | The name of the compression encoding.
|
storage_type | The storage type of the blob. This field is useful for troubleshooting the blob's storage format. |
storage_tier | The storage tier of the blob. Returns |
NULL handling
When the input parameter is
NULL,GET_BLOB_METADATA(blob_expr)returnsNULL.-- Returns: NULL SELECT GET_BLOB_METADATA(to_blob(CAST(null as binary))); -- When blob_expr is NULL, GET_BLOB_METADATA returns NULL. -- Extracting a field from the result also returns NULL. SELECT GET_JSON_OBJECT(GET_BLOB_METADATA(to_blob(CAST(null as binary))), '$.content_length');When a field in the returned result is
NULLWhen
blob_exprisNULL,GET_BLOB_METADATA(blob_expr)returnsNULL; extracting a field from the result also returnsNULL.
For an empty BLOB, the function still returns metadata, withcontent_lengthset to0.Even if
GET_BLOB_METADATA(blob_expr)does not returnNULL, some of its fields may be null.Common scenarios include:
content_type: The value isNULLif a content type is not specified.checksum: The value isNULLif no checksum is provided, but the metadata may still containmd5.content_encoding: IsNULLwhen uncompressed.storage_tier: The storage tier of the BLOB. For a BLOB created withTO_BLOB, the value isSTANDARD. The field returnsNULLif the metadata does not contain storage tier information.
The two scenarios for NULL values have distinct meanings:
When
blob_expritself isNULL,GET_BLOB_METADATA(blob_expr)returnsNULL. Extracting a metadata field in this case also returnsNULL.A field is
NULL: The metadata exists, but the field currently has no value.
Examples
Read metadata directly
SELECT GET_BLOB_METADATA(to_blob('abcdef'));
-- Return value:
{
"md5":"e80b5017098950fc58aad83c8c14978e",
"checksum":null,
"filename":null,
"content_type":null,
"storage_tier":"STANDARD",
"storage_type":"MEMORY",
"is_compressed":false,
"content_length":6,
"content_encoding":null,
"create_timestamp":1795074187477
}Read the three most common fields
This is useful for quickly checking the content type, size in bytes, and compression status.
SELECT
get_json_object(GET_BLOB_METADATA(to_blob('abcdef')), '$.content_type'),
get_json_object(GET_BLOB_METADATA(to_blob('abcdef')), '$.content_length'),
get_json_object(GET_BLOB_METADATA(to_blob('abcdef')), '$.is_compressed');
-- Return value:
+------------+------------+------------+
| _c0 | _c1 | _c2 |
+------------+------------+------------+
| NULL | 6 | false |
+------------+------------+------------+Validate the application-written content type
SELECT get_json_object(
GET_BLOB_METADATA(to_blob('{"key":"value"}', 'application/json')),
'$.content_type'
);
-- When a content type is explicitly specified, the specified value is returned.
+--------------------+
| _c0 |
+--------------------+
| application/json |
+--------------------+Validate the content length
SELECT cast(
get_json_object(GET_BLOB_METADATA(to_blob('中文测试')), '$.content_length') as bigint
);
-- Return value:
+------------+
| _c0 |
+------------+
| 12 |
+------------+Validate consistency with LENGTHB()
SELECT
LENGTHB(TO_BLOB('abcdef')),
CAST(GET_JSON_OBJECT(GET_BLOB_METADATA(TO_BLOB('abcdef')), '$.content_length') AS BIGINT);
-- Return value:
+------------+------------+
| _c0 | _c1 |
+------------+------------+
| 6 | 6 |
+------------+------------+Check persisted blob compression
Prepare a test table.
-- Create a table that contains a BLOB column. CREATE TABLE blob_table2 ( col1 INT , col2 BLOB ) TBLPROPERTIES ( "table.format.version"="2" ); -- Write data to the table. INSERT INTO blob_table2 VALUES (1, to_blob(repeat('aaa', 1000), 'application/json')); -- Read data from the table. SELECT col1,read_blob(col2) FROM blob_table2;Query the compression status.
SELECT get_json_object(GET_BLOB_METADATA(col2), '$.is_compressed') FROM blob_table2; -- Return value: +------+ | _c0 | +------+ | true | +------+is_compressedindicates whether the blob is currently stored in a compressed format.
View the compression encoding
Prepare a test table.
-- Create a table that contains a BLOB column. CREATE TABLE blob_table2 ( col1 INT , col2 BLOB ) TBLPROPERTIES ( "table.format.version"="2" ); -- Write data to the table. INSERT INTO blob_table2 VALUES (1, to_blob(repeat('aaa', 1000), 'application/json')); -- Read data from the table. SELECT col1,read_blob(col2) FROM blob_table2;View the compression encoding.
SELECT get_json_object(GET_BLOB_METADATA(col2), '$.is_compressed'), get_json_object(GET_BLOB_METADATA(col2), '$.content_encoding') FROM blob_table2; -- Return value: +------+------+ | _c0 | _c1 | +------+------+ | true | ZSTD | +------+------+When uncompressed:
content_encodingisNULLWhen compressed:
content_encodingreturns the specific codec name.
View storage information
Prepare a test table.
-- Create a table that contains a BLOB column. CREATE TABLE blob_table2 ( col1 INT , col2 BLOB ) TBLPROPERTIES ( "table.format.version"="2" ); -- Write data to the table. INSERT INTO blob_table2 VALUES (1, to_blob(repeat('aaa', 1000), 'application/json')); -- Read data from the table. SELECT col1,read_blob(col2) FROM blob_table2;View the storage information.
SELECT get_json_object(GET_BLOB_METADATA(col2), '$.storage_type'), get_json_object(GET_BLOB_METADATA(col2), '$.storage_tier') FROM blob_table2; -- Return value: +-------+----------+ | _c0 | _c1 | +-------+----------+ | PANGU | STANDARD | +-------+----------+
Usage with other functions
Use with to_blob to verify that a created blob contains the expected metadata.
Use with LENGTHB to validate that the length in the metadata matches the actual content length in bytes.
Use with read_blob;
read_blobinspects the content, while GET_BLOB_METADATA inspects its metadata.Use with GET_JSON_OBJECT. This is the most common and recommended usage.