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 blob_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.The function returns
NULLif the input blob isNULLor empty.
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 example:
|
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:
These examples do not represent all possible compression rules. |
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. |
blob_storage_tier | The storage tier of the blob. Returns |
NULL handling
When the input parameter is
NULLor an empty blobThe
GET_BLOB_METADATA(blob_expr)function returnsNULLin the following scenarios:The input parameter itself is
NULL.The blob is empty.
The blob does not contain valid readable content.
-- Returns: NULL SELECT GET_BLOB_METADATA(to_blob(CAST(null as binary))); -- In this case, if you try to extract a field, the result is also NULL. SELECT get_json_object(GET_BLOB_METADATA(to_blob(CAST(null as binary))), '$.content_length');When a field in the returned result is
NULLEven 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: IsNULLif no corresponding checksum is available.content_encoding: IsNULLwhen uncompressed.blob_storage_tier: IsNULLif no corresponding storage tier information is available.
The two scenarios for NULL values have distinct meanings:
The function returns
NULLbecause the blob has no metadata to return.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:
{
"checksum": null,
"content_type": null,
"storage_type": "MEMORY",
"is_compressed": false,
"content_length": 6,
"content_encoding": null,
"blob_storage_tier": "STANDARD"
}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'
);
-- This confirms that the specified content type was correctly set.
+--------------------+
| _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_encodingisNULLDuring compression:
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), '$.blob_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.