to_protobuf function
Applies to: Databricks SQL
Databricks Runtime 16.1 and above
Converts a STRUCT into a Protobuf-encoded BINARY value using the specified Protobuf message definition.
Syntax
to_protobuf(data, messageName [, descFilePath [, options] ])
to_protobuf(data, messageName, options)
Arguments
data: ASTRUCTexpression to encode.messageName: A constantSTRINGexpression naming the Protobuf message to encode.descFilePath: An optional constantSTRINGpath to a Protobuf descriptor file or a constantBINARYvalue containing the descriptor file. Generate the descriptor file withprotoc --descriptor_set_outand--include_imports. OmitdescFilePathonly whenoptionsconfigures a Schema Registry.options: An optional constantMAP<STRING, STRING>expression containing conversion options.
Returns
A Protobuf-encoded BINARY value.
If data is NULL, the result is NULL.
The schema of data must match the specified Protobuf message definition.
Options
For a complete list of options, see Protobuf.
Examples
The following example uses a descriptor file generated from this Protobuf message:
Protobuf
message Person {
string name = 1;
int32 id = 2;
}
Encode a struct as a binary Protobuf value:
SQL
> SELECT hex(to_protobuf(
named_struct('name', 'Alice', 'id', 42),
'Person',
'/path/to/person.desc'));
0A05416C696365102A