随笔 - 321  文章 - 0  评论 - 6  阅读 - 34万

python kafka发送中文的编码问题

项目中需要构造带有中文字符非json的测试数据,格式如下:

{'userid': 0, 'ts': '2022-08-03 16:33:38.487973', 'user_name': '中国人'}

 

发过去之后发现消费出来的都是unicode的编码,且指定了utf-8也没用,一开始以为是kafka producer的value_serializer序列化器用的不对,后面发现其实是代码里json.dumps没用好的原因

复制代码
# -*- coding: utf-8 -*-
import time
from kafka import KafkaConsumer, KafkaProducer
import json

from kafka.errors import KafkaError
import datetime


producer = KafkaProducer(sasl_mechanism='PLAIN',
                        security_protocol='SASL_PLAINTEXT',
                        sasl_plain_username='xxxxx',
                        sasl_plain_password='xxxxxxxx',
                        bootstrap_servers=['xxxxxxxxxxx'],
                        #这里的dumps可以指定ensure_ascii=False
                        value_serializer=lambda m: json.dumps(m,ensure_ascii=False).encode(),
                        api_version="2.0.0")

try:
    # produce asynchronously
    for i in range(100):
        now_time = str(datetime.datetime.now())
        send_json={
            "userid": i,
            "ts":now_time,
            "user_name":"中国人"
        }
        print(send_json)
        future = producer.send('xxxxxxxxxxx', send_json)

        try:
            record_metadata = future.get(timeout=2)
        except KafkaError:
            # Decide what to do if produce request failed...
            print("send error!")
            pass
        time.sleep(1)

    print(record_metadata.partition)
    print(record_metadata.offset)

finally:
    producer.close()
复制代码

这样就可以把原来的{"userid": 1, "ts": "2022-08-03 16:12:26.595478", "user_name": "\u4e2d\u56fd\u4eba"}改成{"userid": 1, "ts": "2022-08-03 16:33:39.576068", "user_name": "中国人"}

 

另外1个新手容易犯的错误

1、pyhton中通过str将json强行转换成str类型时,key和value的引号是单引号的,这样发送到kafka,对下游不是很友好,比如下游用java或者flinksql消费的时候可能会出问题,建议用标准序列化json.dumps来转

posted on   该用户很懒  阅读(1301)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示