Search code examples
pythonavro

Avro export - The datum datetime is not an example of the schema


Im trying to export my data to avro file in python3 with avro-python3 library. I have problem with datetime type.

Here is my code:

import json
from datetime import datetime
import avro
from avro.datafile import DataFileWriter
from avro.io import DatumWriter

schema = {
        'name': 'test',
        'type': 'record',
        'fields': [{'name': 'updated_at', 'type': {"type": "long", "logicalType": "timestamp-millis"}}]
    }

schema_parsed = avro.schema.parse(json.dumps(schema))

with open('test.avro', 'wb') as f:
    writer = DataFileWriter(f, DatumWriter(), schema_parsed)

    writer.append({'updated_at': datetime(2022, 3, 25, 2, 39, 20, 736)})

    writer.close()

I get following error:

avro.io.AvroTypeException: The datum {'updated_at': datetime.datetime(2022, 3, 25, 2, 39, 20, 736)} is not an example of the schema {
  "type": "record",
  "name": "test",
  "fields": [
    {
      "type": {
        "type": "long",
        "logicalType": "timestamp-millis"
      },
      "name": "updated_at"
    }
  ]
}

Can someone more knowleable help me what I'm doing wrong?


Solution

  • Solved mystery by debugging avro package.

    datetime value must be timezone aware to be properly assigned to the field format.

    So this works:

    datetime(2022, 3, 25, 2, 39, 20, 736, tzinfo=timezone.utc)