How to define byte[] and LocalDateTime in avro schema?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Apache Avro is a data serialization system that relies on schemas for data structure definition. These schemas are defined in JSON, making Avro language-independent. Defining non-primitive data types such as byte arrays and date/time objects in Avro can be slightly complex given that Avro has a limited set of primitive type definitions.
Defining byte[] in Avro Schema
In Avro, a byte[] (byte array) is typically represented using the type bytes. This type is intended for storing sequences of 8-bit unsigned bytes. Here’s how you can define a byte array in an Avro schema:
This definition is straightforward as bytes is a primitive type in Avro's type system. These byte arrays are often used for encoding binary data such as file contents, encryption keys, or similar data types that require efficient size storage.
Handling LocalDateTime in Avro Schema
LocalDateTime from Java 8’s date-time API (java.time.LocalDateTime) isn't natively supported by Avro's built-in types. However, it's common to handle such types by converting them into a string or long integers, depending upon the precision required.
Representing LocalDateTime as String
Storing LocalDateTime as a string involves converting the date-time object into an ISO-8601 formatted string. Here's an example schema definition:
In this definition, although the data type is string, the logical type timestamp-millis can be used informatively (though it's not an official Avro logical type for strings) to indicate that the string contains timestamp data. Conversion back and forth from the LocalDateTime to the formatted string is necessary when serializing and deserializing.
Representing LocalDateTime as Long
Alternatively, you might choose to store LocalDateTime as the number of milliseconds since the Unix epoch (00:00:00 UTC, 1 January 1970). This representation is efficient for storage and comparison:
In this schema, timestamp-millis as a logical type rightfully applies to long, indicating the interpretation of the numeric value as a timestamp.
Summary Table
| Feature | Avro Type | Logical Type | Description |
| Byte Array | bytes | None | Used for binary data like images or files. |
| LocalDateTime | string | timestamp-millis (custom) | ISO-8601 string representation of date and time. |
| LocalDateTime | long | timestamp-millis | Milliseconds since Unix epoch. Efficient for storage and operations. |
Additional Details
When implementing serialization or deserialization for LocalDateTime in either string or long format, consider:
- Time zone:
LocalDateTimedoes not store any time zone information. When converting from a timestamp (in milliseconds), ensure consistency regarding the time zone. - Precision: When using the long format, determine whether milliseconds are sufficient or if more precision (such as microseconds) is needed.
In scenarios where direct Avro support for advanced data types is necessary, exploring extensions such as custom logical types or using complex types (like records for individual components of LocalDateTime) can be effective.
In conclusion, although Avro schema might not directly support some complex data types, the flexible nature of JSON combined with logical types and custom extensions allows for effective methods to define almost any data type, including byte arrays and date/time information.
Related reading
- How to define hash tables in Bash?
- How to define max_queue_size, workers and use_multiprocessing in keras fit_generator?
- How to define max_queue_size, workers and use_multiprocessing in keras fit_generator?
- How to define object in array in Mongoose schema correctly with 2d geo index
- How to delete a queue in rabbit mq
- How to delete an element from an array in C
- How to delete an item in a list if it exists?
- How to delete in a heap data structure?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.