Получение значений None при чтении данных AVRO из Kafka с использованием Python - PullRequest
0 голосов
/ 08 апреля 2020

У меня проблема с чтением данных AVRO из Kafka с использованием Python. Хотел узнать, сталкивался ли кто-нибудь с подобной проблемой раньше и имеет ли решение для той же самой. Вот мой python код и данные.

from kafka import KafkaConsumer  
import avro.schema  
import avro.io  
import io  
value = b’|\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x005\x00\x01\x00)com.apple.ist.gbi.WORKSHOP.DEPARTMENT_111\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1a\x02\x0ePhysics\x02\x0c\x01\xd3\xfdV\x04\x00\x02\xe8\x9c\xa8\x8d\x07\x00\x00\x00'  
schema = avro.schema.Parse(“”"{“type”: “record”, “name”: “DEPARTMENT_111", “namespace”: “com.apple.ist.gbi.WORKSHOP”, “fields”: [{“type”: [“null”, “string”], “name”: “DEPARTMENT_NAME”, “default”: null}, {“type”: [“null”, {“type”: “bytes”, “logicalType”: “decimal”, “precision”: 38, “scale”: 10}], “name”: “DEPARTMENT_ID”}, {“type”: [“null”, “long”], “name”: “ETL_BATCH_SK”, “default”: null}, {“type”: [“null”, “string”], “name”: “INSERT_TS”, “default”: null}, {“type”: [“null”, “string”], “name”: “OP_CODE”, “default”: null}, {“type”: [“null”, “string”], “name”: “PROCESSED_FLAG”, “default”: null}]}“”")  
print(schema)    

def parse_avro(value, schema):
raw_bytes = value[5:]
print(‘~~~~~~~~~~~~~~~~~~~~~~~~~~~~‘)
print(raw_bytes)
bytes_reader = io.BytesIO(raw_bytes)
decoder = avro.io.BinaryDecoder(bytes_reader)
reader = avro.io.DatumReader(schema)
decoded_data = reader.read(decoder)
print(‘.....................’)
print(decoded_data)
return decoded_data
parse_avro(value, schema)

Я пытаюсь проанализировать данные в «значение», но получить все значения как None.

{"type": "record", "name": "DEPARTMENT_111", "namespace": "com.apple.ist.gbi.WORKSHOP", "fields": [{"type": ["null", "string"], "name": "DEPARTMENT_NAME", "default": null}, {"type": ["null", {"type": "bytes", "logicalType": "decimal", "precision": 38, "scale": 10}], "name": "DEPARTMENT_ID"}, {"type": ["null", "long"], "name": "ETL_BATCH_SK", "default": null}, {"type": ["null", "string"], "name": "INSERT_TS", "default": null}, {"type": ["null", "string"], "name": "OP_CODE", "default": null}, {"type": ["null", "string"], "name": "PROCESSED_FLAG", "default": null}]}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
b'\x01\x00\x00\x00\x01\x00\x00\x005\x00\x01\x00)com.apple.ist.gbi.WORKSHOP.DEPARTMENT_111\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1a\x02\x0ePhysics\x02\x0c\x01\xd3\xfdV\x04\x00\x02\xe8\x9c\xa8\x8d\x07\x00\x00\x00'
.....................
{'DEPARTMENT_NAME': '', 'DEPARTMENT_ID': None, 'ETL_BATCH_SK': None, 'INSERT_TS': '', 'OP_CODE': None, 'PROCESSED_FLAG': None}

Выход имеет схему , raw_bytes и decoded_data. Я ожидаю, что DEPARTMENT_NAME будет физикой, однако я получаю нулевое значение.

Любая помощь приветствуется.

...