適用於:
Databricks Runtime 16.0 和更新版本
傳回結構值為 avroBin 和 jsonSchemaStr 的字串。
語法
from_avro(avroBin, jsonSchemaStr, options )
引數
-
avroBin:BINARY運算式,指定 Avro 數據行。 -
avroSchemaSpec:JSON 格式的目標架構。 它必須符合以avroBin編碼的架構,如 to_avro()中所指定。 -
options:一個MAP<STRING,STRING>常值,指定指令。
退貨
STRUCT,具有以 schema_of_json(jsonStr) 結果為基礎的欄位名稱和型別。
avroBin 必須就 avroSchemaSpec 和 options 的格式良好,否則 Databricks 會引發例外狀況。
備註
以下是最常見的支持選項:
| 選項 | 值 | 描述 |
|---|---|---|
'mode' |
'PERMISSIVE'、'FAILFAST' |
在 PERMISSIVE 模式中,物件中的任何損毀物件或字段會設定為 NULL,而不是引發錯誤。 |
compression |
'uncompressed'、、 'snappy'' 'deflade、、 'bzip2'、 'xz'、 'zstandard' |
指定用來編碼 Avro 數據的壓縮編解碼器。 |
如需更多選項,請參閱 讀取和寫入串流 Avro 數據。
範例
> 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}