繁体   English   中英

CSV中的Avro Python-avro.io.AvroTypeException:数据不是架构的示例

[英]Avro Python from CSV - avro.io.AvroTypeException: The datum is not an example of the schema

我是Avro的新手。 我正在尝试解析一个包含一个字符串值和一个int值的简单CSV文件,但出现错误: avro.io.AvroTypeException:该数据不是该模式的示例

我正在使用的架构是:

{"namespace": "paymenttransaction",
 "type": "record",
 "name": "Payment",
 "fields": [
     {"name": "TransactionId", "type": "string"},
     {"name": "Id",  "type": "int"}
 ]
}

CSV文件包含以下内容:

TransactionId,Id
2018040101000222749,1

我正在为生产者运行的Python代码是:

from confluent_kafka import avro
from confluent_kafka.avro import AvroProducer
import csv

value_schema = avro.load('/home/daniela/avro/example.avsc')

AvroProducerConf = {'bootstrap.servers': 'localhost:9092',
                    'schema.registry.url': 'http://localhost:8081',
                    }

avroProducer = AvroProducer(AvroProducerConf, default_value_schema=value_schema)

with open('/home/usertest/avro/data/paymenttransactions.csv') as file:
    reader = csv.DictReader(file, delimiter=",")
    for row in reader:

        avroProducer.produce(topic='test', value=row)
        print(row)
        avroProducer.flush()

我究竟做错了什么?

这是因为Id仍然是一个字符串,而架构需要一个int。

尝试:

with open('/home/usertest/avro/data/paymenttransactions.csv') as file:
    reader = csv.DictReader(file, delimiter=",")
    for row in reader:
        data_set = {"TransactionId": row["TransactionId"], "Id": int(row["Id"])}
        avroProducer.produce(topic='test', value=data_set)
        print(row)
        avroProducer.flush()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM