我从 csv 文件向 kafka 写入消息。我的制作人说所有数据都生成到 Kafka 主题。
除此之外,我使用 apache nifi 作为消费者到 kafka 主题(ConsumeKafka_2_0 处理器)。
如果我将数据生成到 kafka 到一个流中 - 一切都很好,但是如果我尝试使用多个生产者并行处理多个文件,我会丢失很多行。
我的制片人的核心如下:
def produce(for_produce_file):
log.info('Producing started')
avro_schema = avro.schema.Parse(open(config.AVRO_SCHEME_FILE).read())
names_from_schema = [field.name for field in avro_schema.fields]
producer = Producer({'bootstrap.servers': config.KAFKA_BROKERS,
'security.protocol': config.SECURITY_PROTOCOL,
'ssl.ca.location': config.SSL_CAFILE,
'sasl.mechanism': config.SASL_MECHANISM,
'sasl.username': config.SASL_PLAIN_USERNAME,
'sasl.password': config.SASL_PLAIN_PASSWORD,
'queue.buffering.max.messages': 1000000,
'queue.buffering.max.ms': 5000})
try:
file = open(for_produce_file, 'r', encoding='utf-8')
except FileNotFoundError:
log.error(f'File {for_produce_file} not found')
else:
produced_str_count = 0
csv_reader = csv.DictReader(file, delimiter="|", fieldnames=names_from_schema)
log.info(f'File {for_produce_file} opened')
for row in csv_reader:
record = dict(zip(names_from_schema, row.values()))
while True:
try:
producer.produce(config.TOPIC_NAME, json.dumps(record).encode('utf8'))
producer.poll(0)
produced_str_count += 1
break
except BufferError as e:
log.info(e)
producer.poll(5)
except KafkaException as e:
log.error('Kafka error: {}, message was {} bytes'.format(e, sys.getsizeof(json.dumps(record))))
log.error(row)
break
producer.flush()
log.info(f'Producing ended. Produced {produced_str_count} rows')
Kafka 包含 3 个节点,主题复制因子 - 3
可能问题出在生产者写比消费者读快,某个块溢出时数据被删除?
请给我建议。