Binary Type (BINARY)
Description
In Lakehouse, the BINARY type is used to store binary data, suitable for storing images, audio, video, and other binary files. The BINARY type ensures data integrity and consistency, making it suitable for various scenarios. The maximum write length limit is 16MB. Length validation is performed on fields during batch and real-time imports. If you have data larger than 16MB during import, you can modify the table's properties to set the binary length to 32MB as follows:
Syntax
The declaration of the BINARY type is very simple, just add the keyword BINARY
before the data type. For example:
BINARY Constant Values
In SQL statements, you can use the X
prefix to construct BINARY constant values. For example:
The above statement will return a byte sequence [4]
. The num
following X
is one or more hexadecimal characters, ranging from 0 to F, supporting both uppercase and lowercase. For example:
Will return [0a 41 3f]
.
Conversion Functions
Lakehouse provides various functions to handle BINARY type data, including:
CAST()
function: Converts data of other types to BINARY type.BASE64()
function: Converts binary data to a BASE64 encoded string.UNBASE64()
function: Converts a BASE64 encoded string to binary data.BINARY()
function: Converts a string to a BINARY type byte stream.
Constraints
- The maximum storage length for BINARY type is 16MB
Examples
Here are some examples of using BINARY type and related functions:
- Create a table with a BINARY type column:
- Insert data into the table:
- Convert the string to BINARY type and insert it into the table:
- Query data in the table:
- Use the
CAST()
function to convert a string to BINARY type:
The return will be [43 6c 69 63 6b 5a 65 74 74 61]
.
- Use the
base64()
andunbase64()
functions for encoding and decoding:
Will return Q2xpY2taZXR0YQ==
, then use the unbase64()
function to decode:
The original string ClickZetta
will be returned.