SerializeAVRO

Description

Serializes data into an Avro binary format. 

Returns

Binary data (or a list containing binary data).

Syntax

serializeAVRO(data, writerSchema [, queryFormat, useCustomSerialization ]) 

Parameters

Parameter Required Description
data Yes Data represented as a struct or an array of structs.
writerSchema Yes The schema as a string or the absolute path to the schema. Because it is a record, it can define multiple fields which are organized in a JSON array. Each such field identifies the field's name as well as its type. The type can be something simple, like an integer, or something complex, like another record. It can be given as plain string or filepath directly.
queryFormat No This parameter is of type string with possible values as row, column or struct.
useCustomSerialization No True/false. Whether to use the customSerializer or not. The default value is true. Note that the custom serialize will always be used for serialization. If false, the Avro serialization will be done using the default ColdFusion behavior.

Example using inline schema

<cfscript>
    // define the Avro schema
    mySchema=   '{  
                    "namespace": "first.example",
                    "type": "record",
                    "name": "User",
                    "fields": [
                        {"name": "name", "type": "string"},
                        {"name": "favorite_number",  "type": ["int","null"]},
                        {"name": "favorite_color", "type": ["string","null"]}
                    ]
                }'
    // set the data that conforms the schema above
    data= {
            "name":"Jack Sparrow",
            "favorite_number":{"int":9},
            "favorite_color":{"string":"red"}
        }
 
    avroSerializeResponse = serializeAVRO(data, mySchema)
writedump(avroSerializeResponse)
</cfscript>

Example using external avsc schema

first.avsc

{  
                    "namespace": "first.example",
                    "type": "record",
                    "name": "User",
                    "fields": [
                        {"name": "name", "type": "string"},
                        {"name": "favorite_number",  "type": ["int","null"]},
                        {"name": "favorite_color", "type": ["string","null"]}
                    ]
  }

avro.cfm

<cfscript>
     //set the data that conforms the schema above
    data= {
            "name":"Jack Sparrow",
            "favorite_number":{"int":9},
            "favorite_color":{"string":"red"}
        }
 
    serializedBinaryAvroData = serializeAVRO(data,  "first.avsc")
    writedump(serializedBinaryAvroData)
 
    deserializedStructData = deSerializeAVRO(serializedBinaryAvroData, "first.avsc", true, true)
    writedump(deserializedStructData)
</cfscript>

avroDataAsAList.cfm

<cfscript>
     //set the data that conforms the schema above
    data= [{
            "name":"Jack Sparrow",
            "favorite_number":{"int":9},
            "favorite_color":{"string":"red"}
        },
        {
            "name":"Barbossa",
            "favorite_number":{"int":7},
            "favorite_color":{"string":"black"}
        }]
 
    serializedBinaryAvroData = serializeAVRO(data,  "a.avsc")
    writedump(serializedBinaryAvroData)
 
    deserializedStructData = deSerializeAVRO(serializedBinaryAvroData, "a.avsc", true, true)
    writedump(deserializedStructData)
</cfscript>

Get help faster and easier

New user?