from_avro 函数

适用于:勾选“是” Databricks Runtime 16.0 及更高版本

返回具有 avroBinjsonSchemaStr 的结构值。

语法

from_avro(avroBin, jsonSchemaStr, options )

参数

  • avroBin:指定 Avro 数据行的 BINARY 表达式。
  • avroSchemaSpec:JSON 格式中的目标架构。 它必须与 avroBin 中指定的 中编码的架构匹配。
  • options:指定指令的 MAP<STRING,STRING> 文本。

返回

基于 STRUCT 结果的字段名称和类型的

avroBin 必须相对于 avroSchemaSpecoptions 格式标准,否则 Databricks 会引发异常。

选项

选项 价值观 说明
mode %> 错误处理模式。 默认值:FAILFAST。 在 PERMISSIVE 模式下,损坏的记录设置为 NULL 而不是引发错误。
compression uncompressedsnappydeflatebzip2xzzstandard 用于编码 Avro 数据的压缩编解码器。
avroSchemaEvolutionMode %> 架构演变模式。 默认值:none。 设置为 /> 时,查询将引发架构更改时。 重启作业以使用新架构。 请参阅 将架构演变模式与from_avro配合使用
recursiveFieldMaxDepth 范围: -115 单个递归路径的最大递归深度。 默认值: -1不限制递归深度。
当可从许多不同的架构路径访问共享类型时,架构扩展可能会导致驱动程序内存不足,因为此选项仅绑定一个路径的深度。 解决方法:

示例

> SELECT from_avro(to_avro(5), '{ "type" : "int" }', NULL:MAP<STRING, STRING>);
  5

> SELECT from_avro(to_avro(5, '{ "type" : "int" }'), '{ "type" : "int" }', NULL:MAP<STRING, STRING>);
  5

> SELECT from_avro(to_avro(named_struct('num', 5, 'txt', 'hello')), '{ "type" : "record", "name": "my_record", "fields": [{ "name": "num", "type": "int"}, { "name": "txt", "type": "string"}]}', NULL:MAP<STRING, STRING>);
  {"num":5,"txt":"hello"}

> SELECT from_avro(to_avro(named_struct('num', 5, 'txt', 'hello')),
                   '{ "type" : "record", "name": "my_record", "fields": [{ "name": "num", "type": "int"}, { "name": "txt", "type": "double"}]}',
                   map('mode', 'failfast'));
  Error: Avro data is not valid for the specified schema.

> SELECT from_avro(to_avro(named_struct('num', 5, 'txt', 'hello')),
                   '{ "type" : "record", "name": "my_record", "fields": [{ "name": "num", "type": "int"}, { "name": "txt", "type": "double"}]}',
                   map('mode', 'permissive'));
  {"num":null,"txt":null}