Apache Thrift Format
Description
Apache Thrift is a cross-language serialization framework. It allows you to define data types and service interfaces in a definition file, then generate code for various languages. Thrift provides efficient binary serialization.
File Extensions
.thrift- Thrift files (schema definition files)- Binary Thrift data files (no standard extension)
Implementation Details
Reading
The Thrift implementation:
- Uses
thriftlibrary for reading - Requires
struct_classparameter (the generated Thrift struct class) - Reads binary Thrift data
- Converts Thrift structs to Python dictionaries
- Supports multiple serialized structs in one file
Writing
Writing support:
- Serializes Python dictionaries to Thrift format
- Requires
struct_classparameter - Writes binary Thrift data
Key Features
- Schema-based: Requires generated struct classes from .thrift files
- Binary format: Efficient binary serialization
- Cross-language: Compatible with many programming languages
- Nested data: Supports complex nested structures
- Type preservation: Maintains data types
Usage
from iterable import open_iterable
from my_thrift_module import MyStruct # Generated from .thrift file
# Reading
source = open_iterable('data.thrift', iterableargs={
'struct_class': MyStruct
})
for row in source:
print(row) # row is a dict
source.close()
# Writing
dest = open_iterable('output.thrift', mode='w', iterableargs={
'struct_class': MyStruct
})
dest.write({'field1': 'value1', 'field2': 123})
dest.close()
Parameters
struct_class: Required - The generated Thrift struct class from .thrift file
Limitations
- thrift dependency: Requires
thriftpackage - Schema required: Must have generated struct classes from .thrift files
- Binary format: Not human-readable
- Struct class required: Cannot work without the struct class
- File structure: Expects sequential Thrift structs
Compression Support
Thrift files can be compressed with all supported codecs:
- GZip (
.thrift.gz) - BZip2 (
.thrift.bz2) - LZMA (
.thrift.xz) - LZ4 (
.thrift.lz4) - ZIP (
.thrift.zip) - Brotli (
.thrift.br) - ZStandard (
.thrift.zst)
Use Cases
- Cross-language services: Inter-service communication
- RPC frameworks: Remote procedure calls
- Data serialization: Efficient data exchange
- Microservices: Service-to-service data exchange
Related Formats
- Protocol Buffers - Similar schema-based format
- Cap'n Proto - Another schema-based format
- Avro - Another serialization format