Database Reference
In-Depth Information
Sort Order
Avro defines a sort order for objects. For most Avro types, the order is the natural one you
would expect — for example, numeric types are ordered by ascending numeric value. Oth-
ers are a little more subtle. For instance, enums are compared by the order in which the
symbols are defined and not by the values of the symbol strings.
All types except record have preordained rules for their sort order, as described in the
Avro specification, that cannot be overridden by the user. For records, however, you can
control the sort order by specifying the order attribute for a field. It takes one of three
values: ascending (the default), descending (to reverse the order), or ignore (so
the field is skipped for comparison purposes).
For example, the following schema ( SortedStringPair.avsc ) defines an ordering of
StringPair records by the right field in descending order. The left field is ignored
for the purposes of ordering, but it is still present in the projection:
{
"type" : "record" ,
"name" : "StringPair" ,
"doc" : "A pair of strings, sorted by right field descending." ,
"fields" : [
{ "name" : "left" , "type" : "string" , "order" : "ignore" },
{ "name" : "right" , "type" : "string" , "order" : "descending" }
]
}
The record's fields are compared pairwise in the document order of the reader's schema.
Thus, by specifying an appropriate reader's schema, you can impose an arbitrary ordering
on data records. This schema ( SwitchedStringPair.avsc ) defines a sort order by the right
field, then the left :
{
"type" : "record" ,
"name" : "StringPair" ,
"doc" : "A pair of strings, sorted by right then left." ,
"fields" : [
{ "name" : "right" , "type" : "string" },
{ "name" : "left" , "type" : "string" }
]
}
Avro implements efficient binary comparisons. That is to say, Avro does not have to deseri-
alize binary data into objects to perform the comparison, because it can instead work direc-
Search WWH ::




Custom Search