The mongoid-avro
is a Ruby gem that allows you to convert a Mongoid
Model schema into an Avro schema. This can be useful if you want to use Avro serialization with your Mongoid data.
To install mongoid-avro, add it to your Gemfile:
gem 'mongoid-avro'
Then, execute the following command:
bundle install
- Include Mongoid::Avro in your Mongoid model:
class MyModel
include Mongoid::Document
include Mongoid::Avro
# ...
end
- Optionally, you can specify the Avro format for each field using the
avro_format
option:
class MyModel
include Mongoid::Document
include Mongoid::Avro
field :my_field, type: String, avro_format: 'my_custom_format'
field :my_field_2, type: String, avro_format: {
type: 'record',
name: 'Money',
fields: [
{ name: 'cents', type: 'int' },
{ name: 'currency_iso', type: 'string' }
]
}
# ...
end
The avro_format
option can be a String
, Symbol
, or Hash
.
- Optionally, you can specify the
avro_doc
option to add a description of the field. It will be used as thedoc
of the field.
class Person
include Mongoid::Document
include Mongoid::Avro
field :unique_name, type: String, avro_doc: 'The name of the person which is unique.'
end
- To generate the Avro schema for your model, call the
.generate_avro_schema method
:
schema = MyModel.generate_avro_schema(namespace: 'my.namespace')
You can pass an optional namespace parameter to specify the namespace for the Avro schema. The method returns an Avro::Schema
object.
The method returns an Avro::Schema
.
- (Optional) Generate Avro schema as JSON:
schema.to_avro.to_json
Mongoid | Avro |
---|---|
_id field | string |
Integer | int |
Float | double |
String | string |
Symbol | string |
Boolean | boolean |
BSON::ObjectId | string |
{ "type": "long", "logicalType": "timestamp-millis"}
{ "type": "int", "logicalType": "Date"}
{
"type": "record",
"name": "Money",
"fields": [
{
"name": "cents",
"type": "long"
},
{
"name": "currency_iso",
"type": "string"
}
]
}
{
"type": "array",
"items": "string",
"default": []
}
{
"type": "string",
"logicalType": "json"
}
{
"name": "unique_address",
"type": [
"null",
{
"type": "record",
"name": "unique_address",
"namespace": "ns1",
"fields": [
{
"name": "_id",
"type": "string"
},
{
"name": "address",
"type": "string"
},
{
"name": "number",
"type": "int"
}
]
}
]
}
{
"name": "multiple_address",
"type": [
"null",
{
"type": "array",
"items": {
"type": "record",
"name": "multiple_address",
"namespace": "ns1",
"fields": [
{
"name": "_id",
"type": "string"
},
{
"name": "address",
"type": "string"
},
{
"name": "number",
"type": "int"
}
]
}
}
]
}
{
"type": "array",
"items": "string",
"default": []
}
require 'mongoid-avro'
MyModel.include(Mongoid::Avro)
schema = MyModel.generate_avro_schema(namespace: 'my.namespace')
model = MyModel.find('id')
test_data = JSON.parse(model.attributes.to_json)
Avro::SchemaValidator.validate!(schema, data)
Bug reports and pull requests are welcome on GitHub at https://github.com/shoplineapp/mongoid-avro.
The gem is available as open source under the terms of the MIT License.