正在学,代码是给的
kafka也打开了
from kafka import KafkaProducer
import json
import pymysql.cursors
producer = KafkaProducer(bootstrap_servers='localhost:9092', value_serializer=lambda v: json.dumps(v).encode('utf-8'))
connect = pymysql.Connect(
host='localhost',
port=3306,
user='root',
passwd='自己的',
db='school',
charset='utf8'
)
cursor = connect.cursor()
sql = "select sno,sname,ssex,sage from student;"
cursor.execute(sql)
data = cursor.fetchall()
connect.commit()
for msg in data:
res = {}
res['sno'] = msg[0]
res['name'] = msg[1]
res['sex'] = msg[2]
res['age'] = msg[3]
producer.send('mysql_topic', res)
connect.close()
producer.close()
出现的问题是
C:\Users\86178\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:/Users/86178/PycharmProjects/pythonProject2/mysql_producer.py
Traceback (most recent call last):
File "C:\Users\86178\PycharmProjects\pythonProject2\mysql_producer.py", line 1, in
from kafka import KafkaProducer
File "C:\Users\86178\PycharmProjects\pythonProject2\venv\lib\site-packages\kafka_init.py", line 23, in
from kafka.producer import KafkaProducer
File "C:\Users\86178\PycharmProjects\pythonProject2\venv\lib\site-packages\kafka\producer_init.py", line 4, in
from .simple import SimpleProducer
File "C:\Users\86178\PycharmProjects\pythonProject2\venv\lib\site-packages\kafka\producer\simple.py", line 54
return '' % self.async
SyntaxError: invalid syntax
进程已结束,退出代码1
想了一个下午了