from_protobuf 函数

适用于:选中标记为“是” Databricks SQL 选中标记为“是” Databricks Runtime 16.1 及更高版本

利用指定的Protobuf消息定义将二进制Protobuf值转换为。STRUCT

Syntax

from_protobuf(data, messageName [, descFilePath [, options] ])
from_protobuf(data, messageName, options)

Arguments

  • data: 包含 BINARY Protobuf 消息的表达式。
  • messageName:一个常数 STRING 表达式,将 Protobuf 消息命名为要解码。
  • descFilePath: 一个可选的常数 STRING 路径指向Protobuf描述符文件或包含描述符文件的常数 BINARY 值。 生成描述符文件,且 protoc --descriptor_set_out--include_imports。 只有在配置模式注册表时options才省略descFilePath。
  • options:一个包含转换选项的可选常数 MAP<STRING, STRING> 表达式。

Returns

一个 STRUCT 字段和类型与 Protobuf 消息定义相符的算法。

如果 data 为 NULL,则结果为 NULL。

Protobuf 消息的定义必须与 的 data模式相匹配。 如果模式不匹配,函数可能会失败或返回任意结果。

选项

有关选项的完整列表,请参阅 Protobuf。

示例

以下示例使用了从该Protobuf消息生成的描述符文件:

message Person {
  string name = 1;
  int32 id = 2;
}

解码二进制Protobuf值:

> SELECT from_protobuf(
    unhex('0A05416C696365102A'),
    'Person',
    '/path/to/person.desc');
  {"name":"Alice","id":42}