to_avro function
Applies to: Databricks Runtime 16.0 and above
Returns a Avro binary value with the specified input value.
Syntax
to_avro(expr [, avroSchemaSpec] )
Arguments
expr: An expression.avroSchemaSpec: An optional target schema in JSON format. If specified it must match theexprtype as specified in Notes.
Returns
An Avro encoded BINARY.
Notes
The mapping of SQL types to Avro types is as follows:
SQL type | Avro schema |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Not supported |
| Not supported |
Examples
> SELECT from_avro(to_avro(5), '{ "type" : "int" }');
5
> SELECT from_avro(to_avro(5, '{ "type" : "int" }'), '{ "type" : "int" }');
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"}]}');
{"num":5,"txt":"hello"}